|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.mj.impl.rt; |
|
20 |
|
21 |
|
22 import com.nokia.mj.impl.installer.utils.InstallerMain; |
|
23 import com.nokia.mj.impl.rt.support.JvmInternal; |
|
24 |
|
25 import j2meunit.framework.Test; |
|
26 import j2meunit.framework.TestCase; |
|
27 import j2meunit.framework.TestMethod; |
|
28 import j2meunit.framework.TestSuite; |
|
29 |
|
30 /** |
|
31 * SystemProperty unit tests. |
|
32 */ |
|
33 public class SystemPropertyTests extends TestCase implements InstallerMain |
|
34 { |
|
35 |
|
36 // Begin j2meunit test framework setup |
|
37 public void installerMain(String[] args) |
|
38 { |
|
39 TestSuite suite = new TestSuite(this.getClass().getName()); |
|
40 |
|
41 suite.addTest(new SystemPropertyTests("setPropTests", new TestMethod() |
|
42 { |
|
43 public void run(TestCase tc) |
|
44 { |
|
45 ((SystemPropertyTests)tc).setPropTests(); |
|
46 } |
|
47 })); |
|
48 |
|
49 suite.addTest(new SystemPropertyTests("staticPropTests", new TestMethod() |
|
50 { |
|
51 public void run(TestCase tc) |
|
52 { |
|
53 ((SystemPropertyTests)tc).staticPropTests(); |
|
54 } |
|
55 })); |
|
56 |
|
57 suite.addTest(new SystemPropertyTests("dynamicPropTests", new TestMethod() |
|
58 { |
|
59 public void run(TestCase tc) |
|
60 { |
|
61 ((SystemPropertyTests)tc).dynamicPropTests(); |
|
62 } |
|
63 })); |
|
64 |
|
65 |
|
66 com.nokia.mj.impl.utils.OmjTestRunner.run(suite); |
|
67 |
|
68 } |
|
69 |
|
70 public SystemPropertyTests() |
|
71 { |
|
72 } |
|
73 |
|
74 public SystemPropertyTests(String aTestName, TestMethod aTestMethod) |
|
75 { |
|
76 super(aTestName, aTestMethod); |
|
77 } |
|
78 |
|
79 // End j2meunit test framework setup |
|
80 |
|
81 protected void setUp() |
|
82 { |
|
83 } |
|
84 |
|
85 protected void tearDown() |
|
86 { |
|
87 } |
|
88 |
|
89 // Test cases |
|
90 |
|
91 private static final String testPropertyKey1 = "test.property.key1"; |
|
92 private static final String testPropertyKey2 = "test.property.key2"; |
|
93 private static final String testPropertyKey3 = "test.property.key3"; |
|
94 private static final String testPropertyKey4 = "test.property.key4"; |
|
95 private static final String testPropertyVal1 = "test.property.val1"; |
|
96 private static final String testPropertyVal2 = "test.property.val2"; |
|
97 private static final String testPropertyVal3 = "test.property.val3"; |
|
98 private static final String testPropertyVal4 = "test.property.val4"; |
|
99 private static final String testPropertyVal5 = "test.property.val5"; |
|
100 private static final String testPropertyVal6 = "test.property.val6"; |
|
101 |
|
102 private void setPropTests() |
|
103 { |
|
104 try |
|
105 { |
|
106 // Check that the system property is null in the beginning of the |
|
107 // test. |
|
108 String res = System.getProperty(testPropertyKey1); |
|
109 assertTrue("Fail1, got: "+ res, res == null); |
|
110 |
|
111 // Set the system property and check that it was correctly set. |
|
112 JvmInternal.setSystemProperty(testPropertyKey1, testPropertyVal1); |
|
113 res = System.getProperty(testPropertyKey1); |
|
114 assertTrue("Fail2, got: "+ res, testPropertyVal1.equals(res)); |
|
115 |
|
116 // Set a new value to same system property and check that it was correctly set. |
|
117 JvmInternal.setSystemProperty(testPropertyKey1, testPropertyVal2); |
|
118 res = System.getProperty(testPropertyKey1); |
|
119 assertTrue("Fail3, got: "+ res, testPropertyVal2.equals(res)); |
|
120 |
|
121 // Set a null value to same system property and check that an |
|
122 // exception is thrown and the value is not changed. |
|
123 try |
|
124 { |
|
125 JvmInternal.setSystemProperty(testPropertyKey1, null); |
|
126 assertTrue("No exception1: "+ res, false); |
|
127 } |
|
128 catch (NullPointerException ne) |
|
129 { |
|
130 } |
|
131 res = System.getProperty(testPropertyKey1); |
|
132 assertTrue("Fail4, got: "+ res, testPropertyVal2.equals(res)); |
|
133 |
|
134 // Check that the user property is null in the beginning of the |
|
135 // test. |
|
136 res = System.getProperty(testPropertyKey2); |
|
137 assertTrue("Fail5, got: "+ res, res == null); |
|
138 |
|
139 // Set the user property and check that it was correctly set. |
|
140 JvmInternal.setUserProperty(testPropertyKey2, testPropertyVal3); |
|
141 res = System.getProperty(testPropertyKey2); |
|
142 assertTrue("Fail6, got: "+ res, testPropertyVal3.equals(res)); |
|
143 |
|
144 // Set a new value to same user property and check that it was correctly set. |
|
145 JvmInternal.setUserProperty(testPropertyKey2, testPropertyVal4); |
|
146 res = System.getProperty(testPropertyKey2); |
|
147 assertTrue("Fail7, got: "+ res, testPropertyVal4.equals(res)); |
|
148 |
|
149 // Set a null value to same user property and check that an |
|
150 // exception is thrown and the value is not changed. |
|
151 try |
|
152 { |
|
153 JvmInternal.setSystemProperty(testPropertyKey2, null); |
|
154 assertTrue("No exception2: "+ res, false); |
|
155 } |
|
156 catch (NullPointerException ne) |
|
157 { |
|
158 } |
|
159 res = System.getProperty(testPropertyKey2); |
|
160 assertTrue("Fail8, got: "+ res, testPropertyVal4.equals(res)); |
|
161 |
|
162 // Set the same system and user property and check that the user property |
|
163 // doesn't override the system property. |
|
164 JvmInternal.setSystemProperty(testPropertyKey3, testPropertyVal5); |
|
165 JvmInternal.setUserProperty(testPropertyKey3, testPropertyVal6); |
|
166 res = System.getProperty(testPropertyKey3); |
|
167 assertTrue("Fail9, got: "+ res, testPropertyVal5.equals(res)); |
|
168 |
|
169 } |
|
170 catch (Throwable t) |
|
171 { |
|
172 t.printStackTrace(); |
|
173 assertTrue(t.toString(), false); |
|
174 } |
|
175 } |
|
176 |
|
177 private void staticPropTests() |
|
178 { |
|
179 try |
|
180 { |
|
181 // Check some random static system properties. |
|
182 String res = System.getProperty("microedition.jtwi.version"); |
|
183 assertTrue("Fail01, got: "+ res, "1.0".equals(res)); |
|
184 |
|
185 res = System.getProperty("microedition.msa.version"); |
|
186 assertTrue("Fail01, got: "+ res, "1.1-SUBSET".equals(res)); |
|
187 |
|
188 res = System.getProperty("microedition.profiles"); |
|
189 assertTrue("Fail01, got: "+ res, "MIDP-2.1".equals(res)); |
|
190 } |
|
191 catch (Throwable t) |
|
192 { |
|
193 t.printStackTrace(); |
|
194 assertTrue(t.toString(), false); |
|
195 } |
|
196 } |
|
197 |
|
198 |
|
199 private static final String testDynPropertyKey1 = "test.dyn.property.key1"; |
|
200 private static final String testDynPropertyKey2 = "test.dyn.property.key2"; |
|
201 private static final String testDynPropertyVal1 = testDynPropertyKey1 + " DynamicValue ("; |
|
202 private static final String testDynPropertyVal2 = testDynPropertyKey2 + " DynamicValue2 (1)"; |
|
203 |
|
204 private void dynamicPropTests() |
|
205 { |
|
206 try |
|
207 { |
|
208 JvmInternal.setSystemProperty(testDynPropertyKey1, ":test.Property"); |
|
209 JvmInternal.setSystemProperty(testDynPropertyKey2, ":test.Property2"); |
|
210 |
|
211 // Check that dynamic property is changing per each call. |
|
212 String res = System.getProperty(testDynPropertyKey1); |
|
213 assertTrue("Fail_21, got: "+ res, (testDynPropertyVal1+"1)").equals(res)); |
|
214 res = System.getProperty(testDynPropertyKey1); |
|
215 assertTrue("Fail_22, got: "+ res, (testDynPropertyVal1+"2)").equals(res)); |
|
216 res = System.getProperty(testDynPropertyKey1); |
|
217 assertTrue("Fail_23, got: "+ res, (testDynPropertyVal1+"3)").equals(res)); |
|
218 res = System.getProperty(testDynPropertyKey1); |
|
219 assertTrue("Fail_24, got: "+ res, (testDynPropertyVal1+"4)").equals(res)); |
|
220 |
|
221 // Check that static dynamic property is set in first call, but doesn't change |
|
222 // in leading calls. |
|
223 res = System.getProperty(testDynPropertyKey2); |
|
224 assertTrue("Fail_25, got: "+ res, testDynPropertyVal2.equals(res)); |
|
225 res = System.getProperty(testDynPropertyKey2); |
|
226 assertTrue("Fail_26, got: "+ res, testDynPropertyVal2.equals(res)); |
|
227 res = System.getProperty(testDynPropertyKey2); |
|
228 assertTrue("Fail_27, got: "+ res, testDynPropertyVal2.equals(res)); |
|
229 res = System.getProperty(testDynPropertyKey2); |
|
230 assertTrue("Fail_28, got: "+ res, testDynPropertyVal2.equals(res)); |
|
231 |
|
232 } |
|
233 catch (Throwable t) |
|
234 { |
|
235 t.printStackTrace(); |
|
236 assertTrue(t.toString(), false); |
|
237 } |
|
238 } |
|
239 } |