Path: blob/master/test/jdk/java/lang/StackWalker/EmbeddedStackWalkTest.java
41149 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/**25* @test26* @bug 814321427* @summary Verify StackWalker works well when embedded in another28* StackWalker's functions.29* @run testng/othervm EmbeddedStackWalkTest30*/3132import java.lang.StackWalker.StackFrame;33import static java.lang.StackWalker.Option.*;34import java.lang.invoke.MethodHandle;35import java.lang.invoke.MethodHandles;36import java.lang.invoke.MethodType;37import java.util.Arrays;38import java.util.EnumSet;3940import org.testng.annotations.*;41import static org.testng.Assert.*;4243public class EmbeddedStackWalkTest {44static final StackWalker WALKERS[] = new StackWalker[] {45StackWalker.getInstance(RETAIN_CLASS_REFERENCE),46StackWalker.getInstance(EnumSet.of(SHOW_REFLECT_FRAMES, RETAIN_CLASS_REFERENCE)),47StackWalker.getInstance(EnumSet.of(SHOW_HIDDEN_FRAMES, RETAIN_CLASS_REFERENCE))48};4950static final int BIG_LOOP = 30;51static final int SMALL_LOOP = 5;5253@DataProvider54public StackWalker[][] walkerProvider() {55return new StackWalker[][] {56new StackWalker[] { WALKERS[0] },57new StackWalker[] { WALKERS[1] },58new StackWalker[] { WALKERS[2] }59};60}6162@Test(dataProvider = "walkerProvider")63public void test(StackWalker walker) {64C1.call(walker, BIG_LOOP);65}6667// line numbers are hardcoded for now.68// Should annotate the line numbers and auto-generated these constants69// for test verification instead70static final int BEGIN_LINE = 71; // the begin line number of approximate range.71static final int END_LINE = 136; // the end line number of approximate range.72static class C1 { // here is the begin line number of approximate range, L71.73public static void call(StackWalker walker, int loops) {74if (loops == 0) {75String caller = walker.walk(s ->76s.map(StackFrame::getClassName)77.filter(cn -> !cn.startsWith("jdk.internal.reflect.") && !cn.startsWith("java.lang.invoke"))78.skip(2).findFirst()79).get();80assertEquals(caller, C1.class.getName());8182walker.forEach(f -> C2.testEmbeddedWalker());83} else {84call(walker, --loops);85}86}87}8889static class C2 {90static final StackWalker embeddedWalkers[] = new StackWalker[] {91StackWalker.getInstance(),92StackWalker.getInstance(SHOW_REFLECT_FRAMES),93StackWalker.getInstance(SHOW_HIDDEN_FRAMES)94};9596public static void testEmbeddedWalker() {97walk(SMALL_LOOP);98}99100static void walk(int loops) {101if (loops == 0) {102Arrays.stream(embeddedWalkers)103.forEach(walker -> run(walker));104} else {105walk(--loops);106}107}108109static void run(StackWalker walker) {110MethodHandles.Lookup lookup = MethodHandles.lookup();111MethodHandle handle = null;112try {113handle = lookup.findStatic(C2.class, "call",114MethodType.methodType(void.class, StackWalker.class));115handle.invoke(walker);116} catch(Throwable t) {117throw new RuntimeException(t);118}119}120121static void call(StackWalker walker) {122String caller = walker.walk(s ->123s.map(StackFrame::getClassName)124.filter(cn -> !cn.startsWith("jdk.internal.reflect.") && !cn.startsWith("java.lang.invoke"))125.skip(2).findFirst()126).get();127assertEquals(caller, C2.class.getName());128129verify(walker, C1.class, "call");130verify(walker, C2.class, "call");131verify(walker, C2.class, "run");132verify(walker, C2.class, "walk");133verify(walker, C2.class, "testEmbeddedWalker");134} // here is the end line number of approximate range, L136.135136static void verify(StackWalker walker, Class<?> c, String mn) {137final String fileName = "EmbeddedStackWalkTest.java";138walker.walk(s -> {139s.limit(BIG_LOOP)140.filter(f -> c.getName().equals(f.getClassName()) && mn.equals(f.getMethodName()))141.forEach(f -> {142assertEquals(f.getFileName(), fileName);143int line = f.getLineNumber();144assertTrue(line >= BEGIN_LINE && line <= END_LINE);145146StackTraceElement st = f.toStackTraceElement();147assertEquals(c.getName(), st.getClassName());148assertEquals(mn, st.getMethodName());149assertEquals(st.getFileName(), fileName);150line = st.getLineNumber();151assertTrue(line >= BEGIN_LINE && line <= END_LINE);152});153return null;154});155}156}157}158159160