KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > jvm > TestJPF


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.jvm;
20
21 import gov.nasa.jpf.Config;
22 import gov.nasa.jpf.Error;
23 import gov.nasa.jpf.ErrorList;
24 import gov.nasa.jpf.JPF;
25
26 import java.io.PrintStream JavaDoc;
27
28 import junit.framework.TestCase;
29
30
31 /**
32  * base class for JPF unit tests. TestJPF mostly includes JPF invocations
33  * that check for occurrence or absence of certain execution results
34  */

35 public abstract class TestJPF extends TestCase {
36   static PrintStream JavaDoc out = System.out;
37
38   protected TestJPF (String JavaDoc name) {
39     super(name);
40   }
41
42   protected void runJPFDeadlock (String JavaDoc[] args) {
43     report(args);
44     
45     try {
46       Config conf = JPF.createConfig(args);
47       
48       if (conf.getTargetArg() != null) {
49         JPF jpf = new JPF(conf);
50         jpf.run();
51         
52         ErrorList errors = jpf.getSearchErrors();
53         
54         if (errors != null) {
55           for (int i = 0; i < errors.size(); i++) {
56             Error JavaDoc e = (Error JavaDoc) errors.get(i);
57             
58             if ("Deadlock".equals(e.getMessage())) {
59               System.out.println("found Deadlock");
60               
61               return; // success, we got the sucker
62
}
63           }
64         }
65       }
66     } catch (Throwable JavaDoc x) {
67       x.printStackTrace();
68       fail("JPF internal exception executing: " + args[0] + "." + args[1] + " : " + x);
69     }
70     
71     fail("JPF failed to detect deadlock");
72   }
73
74   /**
75    * run JPF and case the test to fail if NO AssertionsError is encountered
76    */

77   protected void runJPFassertionError (String JavaDoc[] args) {
78     runJPFException(args, "java.lang.AssertionError");
79   }
80
81   /**
82    * It runs JPF and cause the test to fail if an AssertionError is encountered
83    */

84   protected void runJPFnoAssertionError (String JavaDoc[] args) {
85     runJPFnoException(args);
86   }
87
88   protected void runJPFnoException (String JavaDoc[] args) {
89     ExceptionInfo xi = null;
90     
91     report(args);
92
93     try {
94       // run JPF on our target test func
95
gov.nasa.jpf.JPF.main(args);
96       
97       xi = JVM.getVM().getPendingException();
98       if (xi == null){
99         return; // success
100
}
101     } catch (Throwable JavaDoc t) {
102       // we get as much as one little hickup and we declare it failed
103
fail("JPF internal exception executing: " + args[0] + "." + args[1] +
104            " : " + t);
105     }
106
107     fail("JPF caught exception executing: " + args[0] + "." + args[1] +
108          " : " + xi.getExceptionClassname());
109   }
110
111   protected void runJPFException (String JavaDoc[] args, String JavaDoc xClassName) {
112     ExceptionInfo xi = null;
113     
114     report(args);
115
116     try {
117       // run JPF on our target test func
118
gov.nasa.jpf.JPF.main(args);
119
120       xi = JVM.getVM().getPendingException();
121       if (xi == null){
122         fail("JPF failed to catch exception executing: " + args[0] + "." + args[1] +
123             " , expected: " + xClassName);
124       } else if (!xClassName.equals(xi.getExceptionClassname())) {
125         fail("JPF caught wrong exception: " + xi.getExceptionClassname() +
126                    ", expected: " + xClassName);
127       }
128     } catch (Throwable JavaDoc x) {
129       fail("JPF internal exception executing: " + args[0] + "." + args[1] +
130            " : " + x);
131     }
132   }
133   
134   void report (String JavaDoc[] args) {
135     out.print(" running jpf with args:");
136
137     for (int i = 0; i < args.length; i++) {
138       out.print(' ');
139       out.print(args[i]);
140     }
141
142     out.println();
143   }
144 }
145
Popular Tags