2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.test.mc.basic;
21 import gov.nasa.jpf.Config;
22 import gov.nasa.jpf.ListenerAdapter;
23 import gov.nasa.jpf.search.Search;
24 import gov.nasa.jpf.util.test.TestJPF;
25 import gov.nasa.jpf.vm.ElementInfo;
26 import gov.nasa.jpf.vm.ThreadInfo;
27 import gov.nasa.jpf.vm.VM;
28 import gov.nasa.jpf.vm.MethodInfo;
30 import java.util.ArrayList;
32 import org.junit.Test;
35 * test of the VMListener method notifications
37 public class MethodListenerTest extends TestJPF {
39 // avoid loading JPF classes when running under JPF (specify classnames explicitly)
40 static String CLSNAME = "gov.nasa.jpf.test.mc.basic.MethodListenerTest";
41 static String LISTENER = "+listener=gov.nasa.jpf.test.mc.basic.MethodListenerTest$Listener";
43 public static class Listener extends ListenerAdapter {
46 boolean traceActive = false;
49 public Listener (Config config){
50 startMthName = config.getString("_start");
53 String levelPrefix (int lvl){
55 for (int i=0; i<lvl; i++){
62 public void searchStarted(Search search) {
67 public void methodEntered (VM vm, ThreadInfo ti, MethodInfo mi){
68 assertSame(mi, ThreadInfo.getCurrentThread().getTopFrameMethodInfo());
70 if (CLSNAME.equals(mi.getClassName())){
71 String mthName = mi.getName();
72 if (mthName.equals(startMthName)) {
78 String prefix = levelPrefix(level);
79 trace.add(prefix + "> " + mthName);
81 System.out.println(prefix + "> " + mthName);
89 public void methodExited (VM vm, ThreadInfo ti, MethodInfo mi){
91 assertSame(mi, ThreadInfo.getCurrentThread().getTopFrameMethodInfo());
93 if (CLSNAME.equals(mi.getClassName())){
96 String prefix = levelPrefix(level);
97 trace.add(prefix + "< " + mi.getName());
99 System.out.println(prefix + "< " + mi.getName());
110 public void exceptionThrown (VM vm, ThreadInfo ti, ElementInfo ei){
112 String xCls = ei.getClassInfo().getName();
113 trace.add("X " + xCls);
114 System.out.println("X " + xCls);
119 static ArrayList<String> trace = new ArrayList<String>();
121 static boolean traceEquals(String... expected){
122 if (expected.length != trace.size()){
123 System.err.println("wrong trace size, found: " + trace.size() + ", expected: " + expected.length);
128 for (String s : trace){
129 if (!s.equals(expected[i])){
130 System.err.println("wrong trace entry, found: " + s + ", expected: " + expected[i]);
138 //--- internal test stuff
152 throw new RuntimeException("I blow up");
156 System.currentTimeMillis();
160 @Test public void testBasicInvocation() {
161 if (verifyNoPropertyViolation(LISTENER, "+_start=testBasicInvocation")){
165 assertTrue(traceEquals(
166 "> testBasicInvocation",
171 "< testBasicInvocation"));
175 @Test public void testException() {
176 if (verifyNoPropertyViolation(LISTENER, "+_start=testException")){
180 } catch (RuntimeException x){
184 assertTrue(traceEquals(
188 "X java.lang.RuntimeException",