Renaming /doc to /docs for use with GitHub Pages
[jpf-core.git] / docs / user / run.md
1 # Running JPF #
2 There are five general ways to run JPF, depending on your execution environment (command prompt or IDE) and desired level of configuration support. This page has to cover quite some ground, so bear with us
3
4  1. [from a command prompt (operating system shell)](#command-line)
5  2. [from an IDE (NetBeans, Eclipse) without using JPF plugins](#running-jpf-from-within-ide-without-plugins)
6  3. [from an IDE with JPF plugins installed](#running-jpf-from-within-ide-with-plugins)
7  4. [from within a JUnit test class](#launching-jpf-from-junit-tests)
8  5. [single tests from command line](#explicitly-running-tests-from-the-command-line)
9  6. [explicitly from an arbitrary Java program](#explicitly-launching-jpf-from-a-java-program)
10
11 ## Command Line ##
12
13 There are several ways to run JPF from the command line, using varying degrees of its runtime infrastructure. The most simple way is to use the provided `bin/jpf` script of the jpf-core distribution. Go to the directory where your system under test (SUT) classes reside, and do a
14
15 ~~~~~~~~ {.bash}
16 > <jpf-core-dir>/bin/jpf +classpath=. <application-main-class>
17 ~~~~~~~~  
18
19 or preferably
20
21 ~~~~~~~~ {.bash}
22 > <jpf-core-dir>/bin/jpf <application-property-file>.jpf
23 ~~~~~~~~  
24
25 (see target specification below). If you want to avoid platform specific scripts, you only have to slightly expand this to
26
27 ~~~~~~~~ {.bash}
28 > java -jar <jpf-core-dir>/build/RunJPF.jar +classpath=. <application-main-class>
29 ~~~~~~~~
30
31 This makes use of the small RunJPF.jar startup jar that is part of the jpf-core distribution, which only includes the classes that are required to start the JPF bootstrapping process (esp. the JPF classloader). These classes automatically process the various [JPF configuration files](config). If your SUT is not trivial, it is also recommended to add a "-Xmx1024m" host VM option, to avoid running out of memory.
32
33 Last (and probably most rarely), you can directly start JPF and give it an explicit classpath. This amounts to something like
34
35 ~~~~~~~~ {.bash}
36 > java -classpath <jpf-core-dir>/build/jpf.jar gov.nasa.jpf.JPF +classpath=. <application-main-class>
37 ~~~~~~~~
38
39 Of course, this gets quickly more complicated if you use JPF extensions, which require to add to both the host VM and the JPF classpath, which is completely automated if you use the RunJPF.jar method. Explicitly setting paths is only for rare occasions if you develop JPF components yourself.
40
41 There are three different argument groups that are processed by JPF:
42
43 #### (1) JPF command line options ####
44
45 These options should come first (after RunJPF.jar), and all start with a hyphen ("-").  The set of currently supported options is:
46
47  * -help : show usage information and exit
48  * -log  : print the configuration steps
49  * -show : print the configuration dictionary after configuration is complete
50
51 The last two options are mostly used to debug if the JPF configuration does not work as expected. Usually you start with `-show`, and if you don't see the values you expect, continue with `-log` to find out how the values got set.
52
53
54 #### (2) JPF properties ####
55
56 This is the second group of options, which all start with a plus ("+") marker, and consist of "`+<key>=<value>`" pairs like
57
58 ~~~~~~~~ {.bash}
59 .. +cg.enumerate_random=true
60 ~~~~~~~~
61
62 All properties from the various JPF properties [configuration files](config) can be overridden from the command-line, which means there is no limit regarding number and values of options. If you want to extend an existing value, you can use any of the following notations
63
64  * `+<key>+=<value>` - which appends <value>
65  * `++<key>=<value>` - which prepends <value>
66  * `+<key>=..${<key>}..` - which gives explicit control over extension positions
67
68 Normal JPF properties `${<key>}` expansion is supported.
69
70 If the `=<value>` part is omitted, a default value of `true` is assumed. If you want to set a value to null (i.e. remove a key), just skip the `<value>` part, as in `+<key>=`
71
72 #### (3) target specification ####
73
74 There are two ways to specify what application JPF should analyze
75  
76  * explicit classname and arguments
77
78 ~~~~~~~~ {.bash}
79 > jpf ...  x.y.MyApplication arg1 arg2 ..
80 ~~~~~~~~
81
82  * application property file (*.jpf)
83
84 ~~~~~~~~ {.bash}
85 > jpf ... MyApplication.jpf
86 ~~~~~~~~
87
88 We recommend using the second way, since it enables you to store all required settings in a text file that can be kept together with the SUT sources, and also allows you to start JPF from within !NetBeans or Eclipse just by selecting the *.jpf file (this is mainly what the IDE plugins are for). Please note that application property files require a "`target`" entry, as in
89
90 ~~~~~~~~ {.bash}
91 # JPF application property file to verify x.y.MyApplication
92 target = x.y.MyApplication
93 target.args = arg1,arg2
94 # Note that target_args in JPF 6 changed to target.args in JPF 7.
95 ...
96 ~~~~~~~~
97
98 ## Running JPF from within IDE without plugins ##
99
100 You can start JPF from within !NetBeans or Eclipse without having the IDE specific JPF plugins installed. In this case, JPF uses the standard IDE consoles to report verification results. For details, please refer to the following pages:
101
102  * [Running JPF from within NetBeans without plugin](run_nb)
103  * [Running JPF from Eclipse without plugin](run_eclipse)
104
105 Note that this is **not** the recommended way to run JPF from within an IDE, unless you want to debug JPF or your classes.
106
107 ## Running JPF from within IDE with plugins ##
108
109 You can simplify launching JPF from within !NetBeans or Eclipse by using the respective plugins that are available from this server. In this case, you just have to create/select an application property (*.jpf) file within your test project, and use the IDE context menu to start a graphical JPF user interface. These so called "JPF shells" are separate applications (that can be configured through normal JPF properties), i.e. appear in a separate window, but can still communicate with the IDE, e.g. to position editor windows. You can find more details on
110
111  * [Running JPF from within NetBeans with netbeans-jpf plugin](run_nb_plugin)
112  * [Running JPF from Eclipse with eclipse-jpf plugin](run_eclipse_plugin)
113
114 This is becoming the primary method of running JPF. The benefits are twofold: (1) this is executed outside of the IDE process, i.e. it doesn't crash the IDE if JPF runs out of memory, and (2) it makes use of all your standard JPF configuration (site.properties and jpf.properties), in the same way like running JPF from a command-line. 
115
116 ## Launching JPF from JUnit tests ##
117
118 JPF comes with [JUnit](http://www.junit.org) based testing infrastructure that is used for its own regression test suite. This mechanism can also be used to create your own test drivers that are executed by JUnit, e.g. through an [Ant](http://ant.apache.org) build script. The source structure of your tests is quite simple
119
120 ~~~~~~~~ {.java}
121 import gov.nasa.jpf.util.test.JPFTestSuite;
122 import org.junit.Test;
123
124 public class MyTest extends TestJPF {
125
126   @Test
127   public void testSomeFunction() {
128     if (verifyNoPropertyViolation(jpfOptions)) {  // specifies the test goal, "jpfOptions" are optional 
129        someFuntction(); ..                        // this section is verified by JPF
130     } 
131   }
132
133   //.. more @Test methods
134 ~~~~~~~~
135
136 From a JUnit perspective, this is a completely normal test class. You can therefore execute such a test with the standard `<junit>` [Ant](http://ant.apache.org) task, like
137
138 ~~~~~~~~ {.xml}
139     <property file="${user.home}/.jpf/site.properties"/>
140     <property file="${jpf-core}/jpf.properties"/>
141     ...
142     <junit printsummary="on" showoutput="off" haltonfailure="yes"
143            fork="yes" forkmode="perTest" maxmemory="1024m">
144       ...
145       <classpath>
146         ...
147         <pathelement location="${jpf-core}/build/jpf.jar"/>
148       </classpath>
149
150       <batchtest todir="build/tests">
151         <fileset dir="build/tests">
152           ...
153           <include name="**/*Test.class"/>
154         </fileset>
155       </batchtest>
156     </junit>
157     ...
158 ~~~~~~~~
159
160 Only jpf.jar needs to be in the host VM classpath when compiling and running the test, since `gov.nasa.jpf.util.test.TestJPF` will use the normal JPF configuration (site.properties and configured jpf.properties) to set up the required `native_classpath`, `classpath`, 'test_classpath` and `sourcepath` settings at runtime. Please refer to the [JPF configuration](config) page for details. 
161
162 If you don't have control over the build.xml because of the IDE specific project type (e.g. if your SUT is configured as a NetBeans "Class Library Project"), you have to add jpf.jar as an external jar to your IDE project configuration.
163
164 In addition to adding jpf.jar to your build.xml or your IDE project configuration, you might want to add a jpf.properties file to the root directory of your project, to set up things like where JPF finds classes and sources it should analyze (i.e. settings that should be common for all your tests). A generic example could be 
165
166 ~~~~~~~~ {.bash}
167   # example of JPF project properties file to set project specific paths
168
169   # no native classpath required if this is not a JPF project itself
170   myproject.native_classpath=...
171
172   # where does JPF find the classfiles to execute
173   myproject.classpath=build/classes
174
175   # where do test classes reside
176   myproject.test_classpath=build/test/classes
177
178   # other project common JPF settings like autoloaders etc.
179   listener.autoload+=,javax.annotation.Nonnull
180   listener.javax.annotation.Nonnull=.aprop.listener.NonnullChecker
181   ...
182 ~~~~~~~~
183
184 You can find project examples here
185
186  * [standard NetBeans project](../projects/standardnbproject) ("Java Class Library" or "Java Application")
187  * Freeform NetBeans project (with user supplied build.xml)
188  * standard Eclipse project (with user supplied build.xml)
189
190 Please refer to the [Verify API](api) and the [JPF tests](../devel/jpf_tests) pages for details about JPF APIs (like `verifyNoPropertyViolation(..)` or `Verify.getInt(min,max)`) you can use within your test classes.
191
192 Since JPF projects use the same infrastructure for their regression tests, you can find a wealth of examples under the `src/tests` directories of your installed JPF projects. 
193
194 ## Explicitly Running Tests from the command line ##
195
196 You can also run your `TestJPF` derived test drivers by using the `bin/test` script (which in turn just a short for "`java -jar tools/RunTest.jar`", i.e. is platform independent):
197
198 ~~~~~~~~ {.bash}
199 bin/test <test-class> [<test-method>]
200 ~~~~~~~~ 
201
202 Note that each `verify..(jpfArgs)` uses its own `Config` instance in this case. If you want to specify temporary JPF options from the command-line when running `RunTest`, prefix them with `test` like in the following example
203
204 ~~~~~~~~ {.bash}
205 bin/test +test.listener=.listener.ExecTracker gov.nasa.jpf.test.mc.basic.AttrsTest
206 ~~~~~~~~
207
208 ## Explicitly Launching JPF from a Java Program ##
209 Since JPF is a pure Java application, you can also run it from your own application. The corresponding pattern looks like this:
210
211 ~~~~~~~~ {.java}
212 public class MyJPFLauncher {
213   ...
214   public static void main(String[] args){
215     ..
216     try {
217
218       // this initializes the JPF configuration from default.properties, site.properties
219       // configured extensions (jpf.properties), current directory (jpf.properies) and
220       // command line args ("+<key>=<value>" options and *.jpf)
221       Config conf = JPF.createConfig(args);
222
223       // ... modify config according to your needs
224       conf.setProperty("my.property", "whatever");
225
226       // ... explicitly create listeners (could be reused over multiple JPF runs)
227       MyListener myListener = ... 
228
229       JPF jpf = new JPF(conf);
230
231       // ... set your listeners
232       jpf.addListener(myListener);
233
234       jpf.run();
235       if (jpf.foundErrors()){
236         // ... process property violations discovered by JPF
237       }
238     } catch (JPFConfigException cx){
239       // ... handle configuration exception
240       // ...  can happen before running JPF and indicates inconsistent configuration data
241     } catch (JPFException jx){
242       // ... handle exception while executing JPF, can be further differentiated into
243       // ...  JPFListenerException - occurred from within configured listener
244       // ...  JPFNativePeerException - occurred from within MJI method/native peer
245       // ...  all others indicate JPF internal errors
246     }
247   ...
248 ~~~~~~~~ 
249
250 Please refer to the [Embedding JPF](../devel/embedded) developers documentation for details. If you start JPF through your own launcher application, you have to take care of setting up the required `CLASSPATH` entries so that it finds your (and JPFs) classes, or you can use the generic `gov.nasa.jpf.Main` to load and start your launcher class, which makes use of all the path settings you have in your [site.properties](../install/site-properties) and the directories holding project properties (jpf.properties) referenced therein (details on [how to configure JPF](../user/config). This brings us back to the command line at the top of this page, only that you specify which class should be loaded through `Main`:
251
252 ~~~~~~~~ {.bash}
253 > java -jar .../RunJPF.jar -a MyJPFLauncher ...
254 ~~~~~~~~ 
255
256 (note that `gov.nasa.jpf.Main` is the `Main-Class` entry of the executable RunJPF.jar, which also holds the `JPFClassLoader`). 
257
258 Just for the sake of completeness, there is another way to start JPF explicitly through a `gov.nasa.jpf.JPFShell` implementation, which is using the normal `JPF.main()` to load your shell, which in turn instantiates and runs a `JPF` object. This is specified in your application property (*.jpf) file with the `shell=<your-shell-class>` option. Use this if your way to start JPF is optional, i.e. JPF could also be run normally with your *.jpf.