Initial import
[jpf-core.git] / src / tests / gov / nasa / jpf / ConfigTest.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
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
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
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.
17  */
18
19 package gov.nasa.jpf;
20
21 import gov.nasa.jpf.util.test.TestJPF;
22
23 import java.io.File;
24 import java.util.regex.Matcher;
25
26 import org.junit.Test;
27
28
29 /**
30  * unit test for Config
31  */
32 public class ConfigTest extends TestJPF {
33
34   @Test
35   public void testDefaultAppPropertyInit () {
36
37     String dir = "src/tests/gov/nasa/jpf";
38     String[] args = {dir + "/configTestApp.jpf"};
39
40     Config conf = new Config(args);
41
42     String val = conf.getString("vm.class");
43     assert "gov.nasa.jpf.vm.SingleProcessVM".equals(val);
44
45     val = conf.getString("target"); // from configTest.jpf
46     assert "urgh.org.MySystemUnderTest".equals(val);
47
48     // that's testing key expansion and the builtin "config_path"
49     val = conf.getString("mySUT.location");
50     
51     assert val != null;
52     
53     if (!File.separator.equals("/"))
54        dir = dir.replaceAll("/", Matcher.quoteReplacement(File.separator));  // On UNIX Config returns / and on Windows Config returns \\
55
56     assert val.endsWith(dir);
57   }
58
59   @Test
60   public void testDefaultExplicitTargetInit ()  {
61     String[] args = {"urgh.org.MySystemUnderTest"};
62
63     Config conf = new Config( args);
64     String[] freeArgs = conf.getFreeArgs();
65     assertTrue( freeArgs.length == 1);
66     assertTrue( "urgh.org.MySystemUnderTest".equals(freeArgs[0]));
67   }
68
69   @Test
70   public void testExplicitLocations () {
71     String dir = "src/tests/gov/nasa/jpf/";
72     String[] args = {"+site=" + dir + "configTestSite.properties",
73                      "+app=" + dir + "configTestApp.jpf" };
74
75     Config conf = new Config( args);
76     conf.printEntries();
77
78     assert "urgh.org.MySystemUnderTest".equals(conf.getString("target"));
79   }
80
81   @Test
82   public void testTargetArgsOverride () {
83
84     String dir = "src/tests/gov/nasa/jpf/";
85     String[] args = { dir + "configTestApp.jpf",
86                       "x", "y"};
87
88     Config conf = new Config(args);
89     conf.printEntries();
90
91     String[] ta = conf.getStringArray("target.args");
92     assert ta != null;
93     assert ta.length == 3;
94     assert "a".equals(ta[0]);
95     assert "b".equals(ta[1]);
96     assert "c".equals(ta[2]);
97     
98     String[] freeArgs = conf.getFreeArgs();
99     assert freeArgs != null;
100     assert freeArgs.length == 2;
101     assert "x".equals(freeArgs[0]);
102     assert "y".equals(freeArgs[1]);
103   }
104
105   @Test
106   public void testClassPaths () {
107     String dir = "src/tests/gov/nasa/jpf/";
108     String[] args = {"+site=" + dir + "configTestSite.properties",
109                      "+app=" + dir + "configTestApp.jpf" };
110
111     Config conf = new Config( args);
112     conf.printEntries();
113
114     // those properties are very weak!
115     String[] bootCpEntries = conf.asStringArray("boot_classpath");
116     assert bootCpEntries.length > 0;
117
118     String[] nativeCpEntries = conf.asStringArray("native_classpath");
119     assert nativeCpEntries.length > 0;
120   }
121
122   @Test
123   public void testRequiresOk () {
124     String dir = "src/tests/gov/nasa/jpf/";
125     String[] args = { "+site=" + dir + "configTestSite.properties",
126                       dir + "configTestRequires.jpf" };
127
128     Config.enableLogging(true);
129     Config conf = new Config( args);
130     String v = conf.getString("whoa");
131     System.out.println("got whoa = " + v);
132     
133     assert (v != null) && v.equals("boa");
134   }
135
136   @Test
137   public void testRequiresFail () {
138     String dir = "src/tests/gov/nasa/jpf/";
139     String[] args = { "+site=" + dir + "configTestSite.properties",
140                       dir + "configTestRequiresFail.jpf" };
141
142     Config.enableLogging(true);
143     Config conf = new Config( args);
144     String v = conf.getString("whoa");
145     System.out.println("got whoa = " + v);
146
147     assert (v == null);
148   }
149
150   @Test
151   public void testIncludes () {
152     String dir = "src/tests/gov/nasa/jpf/";
153     String[] args = { "+site=" + dir + "configTestSite.properties",
154                       dir + "configTestIncludes.jpf" };
155
156     Config.enableLogging(true);
157     Config conf = new Config( args);
158     String v = conf.getString("my.common");
159     System.out.println("got my.common = " + v);
160
161     assert (v != null) && v.equals("whatever");
162   }
163
164   @Test
165   public void testIntArray(){
166     String dir = "src/tests/gov/nasa/jpf/";
167     String[] args = { "+site=" + dir + "configTestSite.properties",
168                       "+arr=-42,0xff,0" };
169
170     Config.enableLogging(true);
171     Config conf = new Config( args);
172     int[] a = conf.getIntArray("arr");
173     
174     assertTrue(a != null);
175     assertTrue(a.length == 3);
176     assertTrue(a[0] == -42 && a[1] == 0xff && a[2] == 0);
177     
178   }
179 }