6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 8140450
27 * @summary Basic test for the StackWalker::walk method
28 * @run testng Basic
29 */
30
31 import java.lang.StackWalker.StackFrame;
32 import java.util.List;
33 import java.util.stream.Collectors;
34 import static java.lang.StackWalker.Option.*;
35
36 import org.testng.annotations.DataProvider;
37 import org.testng.annotations.Test;
38
39 public class Basic {
40 private static boolean verbose = false;
41
42 @DataProvider(name = "stackDepths")
43 public static Object[][] stackDepths() {
44 return new Object[][] {
45 { new int[] { 12 }, new int[] { 4, 8, 12} },
46 { new int[] { 18 }, new int[] { 8, 16, 20} },
47 { new int[] { 32 }, new int[] { 16, 32, 64} },
48 };
49 }
50
51 /**
52 * For a stack of a given depth, it creates a StackWalker with an estimate.
53 * Test walking different number of frames
54 */
55 @Test(dataProvider = "stackDepths")
56 public static void test(int[] depth, int[] estimates) {
57 Basic test = new Basic(depth[0]);
58 for (int estimate : estimates) {
59 test.walk(estimate);
60 }
61 }
62
63 private final int depth;
64 Basic(int depth) {
65 this.depth = depth;
66 }
67
68 /*
69 * Setup a stack builder with the expected stack depth
70 * Walk the stack and count the frames.
71 */
72 void walk(int estimate) {
73 int limit = Math.min(depth, 16);
74 List<StackFrame> frames = new StackBuilder(depth, limit).build();
75 System.out.format("depth=%d estimate=%d expected=%d walked=%d%n",
76 depth, estimate, limit, frames.size());
77 assertEquals(limit, frames.size());
78 }
79
80 class StackBuilder {
81 private final int stackDepth;
82 private final int limit;
83 private int depth = 0;
84 private List<StackFrame> result;
85 StackBuilder(int stackDepth, int limit) {
86 this.stackDepth = stackDepth; // build method;
87 this.limit = limit;
88 }
89 List<StackFrame> build() {
90 trace("build");
91 m1();
92 return result;
93 }
94 void m1() {
95 trace("m1");
96 m2();
97 }
98 void m2() {
99 trace("m2");
119 else
120 filler(--i);
121 }
122
123 void walk() {
124 StackWalker walker = StackWalker.getInstance(RETAIN_CLASS_REFERENCE);
125 result = walker.walk(s -> s.limit(limit).collect(Collectors.toList()));
126 }
127 void trace(String methodname) {
128 ++depth;
129 if (verbose)
130 System.out.format("%2d: %s%n", depth, methodname);
131 }
132 }
133
134 static void assertEquals(int x, int y) {
135 if (x != y) {
136 throw new RuntimeException(x + " != " + y);
137 }
138 }
139 }
|
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 8140450 8173898
27 * @summary Basic test for the StackWalker::walk method
28 * @run testng Basic
29 */
30
31 import java.lang.StackWalker.StackFrame;
32 import java.util.List;
33 import java.util.Objects;
34 import java.util.stream.Collectors;
35 import java.util.stream.Stream;
36 import static java.lang.StackWalker.Option.*;
37
38 import org.testng.annotations.DataProvider;
39 import org.testng.annotations.Test;
40
41 public class Basic {
42 private static boolean verbose = false;
43
44 @DataProvider(name = "stackDepths")
45 public static Object[][] stackDepths() {
46 return new Object[][] {
47 { new int[] { 12 }, new int[] { 4, 8, 12} },
48 { new int[] { 18 }, new int[] { 8, 16, 20} },
49 { new int[] { 32 }, new int[] { 16, 32, 64} },
50 };
51 }
52
53 /**
54 * For a stack of a given depth, it creates a StackWalker with an estimate.
55 * Test walking different number of frames
56 */
57 @Test(dataProvider = "stackDepths")
58 public static void test(int[] depth, int[] estimates) {
59 Basic test = new Basic(depth[0]);
60 for (int estimate : estimates) {
61 test.walk(estimate);
62 }
63 }
64
65 @Test
66 /**
67 * @bug 8173898
68 */
69 public static void testConstructor() throws Exception {
70 System.out.println("testConstructor:");
71 List<String> found = ((Dummy)Dummy.class.getMethod("create")
72 .invoke(null)).found;
73 assertEquals(List.of(Dummy.class.getName()+"::<init>",
74 Dummy.class.getName()+"::create",
75 Basic.class.getName()+"::testConstructor"),
76 found);
77 }
78
79 private final int depth;
80 Basic(int depth) {
81 this.depth = depth;
82 }
83
84 /*
85 * Setup a stack builder with the expected stack depth
86 * Walk the stack and count the frames.
87 */
88 void walk(int estimate) {
89 int limit = Math.min(depth, 16);
90 List<StackFrame> frames = new StackBuilder(depth, limit).build();
91 System.out.format("depth=%d estimate=%d expected=%d walked=%d%n",
92 depth, estimate, limit, frames.size());
93 assertEquals(limit, frames.size());
94 }
95
96 static class Dummy {
97 static final StackWalker walker =
98 StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
99 List<String> found;
100 public Dummy() {
101 found = walker.walk(this::parse);
102 }
103 public boolean accept(StackFrame f) {
104 if (!f.getDeclaringClass().getName().contains(".")) {
105 System.out.println(" " + f);
106 return true;
107 }
108 return false;
109 }
110 public String frame(StackFrame f) {
111 return f.getDeclaringClass().getName() + "::" + f.getMethodName();
112 }
113 List<String> parse(Stream<StackFrame> s) {
114 return s.filter(this::accept)
115 .map(this::frame)
116 .collect(Collectors.toList());
117 }
118 public static Dummy create() throws Exception {
119 return Dummy.class.getConstructor().newInstance();
120 }
121 }
122
123 class StackBuilder {
124 private final int stackDepth;
125 private final int limit;
126 private int depth = 0;
127 private List<StackFrame> result;
128 StackBuilder(int stackDepth, int limit) {
129 this.stackDepth = stackDepth; // build method;
130 this.limit = limit;
131 }
132 List<StackFrame> build() {
133 trace("build");
134 m1();
135 return result;
136 }
137 void m1() {
138 trace("m1");
139 m2();
140 }
141 void m2() {
142 trace("m2");
162 else
163 filler(--i);
164 }
165
166 void walk() {
167 StackWalker walker = StackWalker.getInstance(RETAIN_CLASS_REFERENCE);
168 result = walker.walk(s -> s.limit(limit).collect(Collectors.toList()));
169 }
170 void trace(String methodname) {
171 ++depth;
172 if (verbose)
173 System.out.format("%2d: %s%n", depth, methodname);
174 }
175 }
176
177 static void assertEquals(int x, int y) {
178 if (x != y) {
179 throw new RuntimeException(x + " != " + y);
180 }
181 }
182
183 static void assertEquals(Object x, Object y) {
184 if (!Objects.equals(x,y)) {
185 throw new RuntimeException(x + " != " + y);
186 }
187 }
188 }
|