Path: blob/master/test/jdk/java/lang/StackWalker/StackRecorderUtil.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*/2223import java.lang.StackWalker.Option;24import java.lang.StackWalker.StackFrame;25import java.util.*;2627/**28* Utility class for recording a stack trace for later comparison to29* StackWalker results.30*31* StackTraceElement comparison does not include line number, isNativeMethod32*/33public class StackRecorderUtil implements Iterable<StackRecorderUtil.TestFrame> {34private List<TestFrame> testFrames = new LinkedList();3536private boolean compareClasses;37private boolean compareClassNames = true;38private boolean compareMethodNames = true;39private boolean compareSTEs;4041public StackRecorderUtil(Set<StackWalker.Option> swOptions) {42compareClasses = swOptions.contains(Option.RETAIN_CLASS_REFERENCE);43compareSTEs = true;44}4546/**47* Add a method call to this recorded stack.48*/49public void add(Class declaringClass, String methodName, String fileName) {50testFrames.add(0, new TestFrame(declaringClass, methodName, fileName));51}5253public int frameCount() { return testFrames.size(); }5455/**56* Compare the given StackFrame returned from the StackWalker to the57* recorded frame at the given index.58*59* Tests for equality, as well as functional correctness with respect to60* the StackWalker's options (e.g. throws or doesn't throw exceptions)61*/62public void compareFrame(int index, StackFrame sf) {63TestFrame tf = testFrames.get(index);64if (compareClasses) {65if (!tf.declaringClass.equals(sf.getDeclaringClass())) {66throw new RuntimeException("Expected class: " +67tf.declaringClass.toString() + ", but got: " +68sf.getDeclaringClass().toString());69}70} else {71boolean caught = false;72try {73sf.getDeclaringClass();74} catch (UnsupportedOperationException e) {75caught = true;76}77if (!caught) {78throw new RuntimeException("StackWalker did not have " +79"RETAIN_CLASS_REFERENCE Option, but did not throw " +80"UnsupportedOperationException");81}82}8384if (compareClassNames && !tf.className().equals(sf.getClassName())) {85throw new RuntimeException("Expected class name: " + tf.className() +86", but got: " + sf.getClassName());87}88if (compareMethodNames && !tf.methodName.equals(sf.getMethodName())) {89throw new RuntimeException("Expected method name: " + tf.methodName +90", but got: " + sf.getMethodName());91}92if (compareSTEs) {93StackTraceElement ste = sf.toStackTraceElement();94if (!(ste.getClassName().equals(tf.className()) &&95ste.getMethodName().equals(tf.methodName)) &&96ste.getFileName().equals(tf.fileName)) {97throw new RuntimeException("Expected StackTraceElement info: " +98tf + ", but got: " + ste);99}100if (!Objects.equals(ste.getClassName(), sf.getClassName())101|| !Objects.equals(ste.getMethodName(), sf.getMethodName())102|| !Objects.equals(ste.getFileName(), sf.getFileName())103|| !Objects.equals(ste.getLineNumber(), sf.getLineNumber())104|| !Objects.equals(ste.isNativeMethod(), sf.isNativeMethod())) {105throw new RuntimeException("StackFrame and StackTraceElement differ: " +106"sf=" + sf + ", ste=" + ste);107}108}109}110111public Iterator<TestFrame> iterator() {112return testFrames.iterator();113}114115/**116* Class used to record stack frame information.117*/118public static class TestFrame {119public Class declaringClass;120public String methodName;121public String fileName = null;122123public TestFrame (Class declaringClass, String methodName, String fileName) {124this.declaringClass = declaringClass;125this.methodName = methodName;126this.fileName = fileName;127}128public String className() {129return declaringClass.getName();130}131public String toString() {132return "TestFrame: " + className() + "." + methodName +133(fileName == null ? "" : "(" + fileName + ")");134}135}136}137138139