80
|
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 |
package com.nokia.mj.impl.rt.support;
|
|
19 |
|
|
20 |
import com.nokia.mj.impl.rt.test.UnitTestSuiteCreator;
|
|
21 |
import com.nokia.mj.impl.rt.test.JvmPortTest;
|
|
22 |
import com.nokia.mj.impl.rt.support.Jvm;
|
|
23 |
import com.nokia.mj.impl.rt.support.JvmInternal;
|
|
24 |
import j2meunit.framework.Test;
|
|
25 |
import j2meunit.framework.TestCase;
|
|
26 |
import j2meunit.framework.TestMethod;
|
|
27 |
import j2meunit.framework.TestSuite;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* JvmPort basic unit tests. Testing that the delegation works.
|
|
31 |
*/
|
|
32 |
public class JvmPortBasicTest extends TestCase implements UnitTestSuiteCreator
|
|
33 |
{
|
|
34 |
|
|
35 |
// Begin j2meunit test framework setup
|
|
36 |
public TestSuite createTestSuite(String[] args)
|
|
37 |
{
|
|
38 |
TestSuite suite = new TestSuite(this.getClass().getName());
|
|
39 |
|
|
40 |
suite.addTest(new JvmPortBasicTest("testJvm", new TestMethod()
|
|
41 |
{
|
|
42 |
public void run(TestCase tc)
|
|
43 |
{
|
|
44 |
((JvmPortBasicTest)tc).testJvm();
|
|
45 |
}
|
|
46 |
}));
|
|
47 |
|
|
48 |
suite.addTest(new JvmPortBasicTest("testVmPortNotExist", new TestMethod()
|
|
49 |
{
|
|
50 |
public void run(TestCase tc)
|
|
51 |
{
|
|
52 |
((JvmPortBasicTest)tc).testVmPortNotExist();
|
|
53 |
}
|
|
54 |
}));
|
|
55 |
|
|
56 |
|
|
57 |
return suite;
|
|
58 |
|
|
59 |
}
|
|
60 |
|
|
61 |
public JvmPortBasicTest()
|
|
62 |
{
|
|
63 |
}
|
|
64 |
|
|
65 |
public JvmPortBasicTest(String aTestName, TestMethod aTestMethod)
|
|
66 |
{
|
|
67 |
super(aTestName, aTestMethod);
|
|
68 |
}
|
|
69 |
|
|
70 |
// End j2meunit test framework setup
|
|
71 |
|
|
72 |
protected void setUp()
|
|
73 |
{
|
|
74 |
}
|
|
75 |
|
|
76 |
protected void tearDown()
|
|
77 |
{
|
|
78 |
}
|
|
79 |
|
|
80 |
private void checkCallCounts(String info, int ind)
|
|
81 |
{
|
|
82 |
|
|
83 |
assertTrue(info + " failed " + JvmPortTest.print(),
|
|
84 |
JvmPortTest.checkCalls(ind));
|
|
85 |
|
|
86 |
}
|
|
87 |
|
|
88 |
private void checkObjects(String info, Object obj1, Object obj2, Object res)
|
|
89 |
{
|
|
90 |
|
|
91 |
boolean ok = JvmPortTest.mObj1 == obj1;
|
|
92 |
if (!ok && obj1 != null)
|
|
93 |
{
|
|
94 |
ok = obj1.equals(JvmPortTest.mObj1);
|
|
95 |
}
|
|
96 |
assertTrue(info + " failed. obj1 " + obj1 + " != " + " mObj1 " + JvmPortTest.mObj1, ok);
|
|
97 |
|
|
98 |
ok = JvmPortTest.mObj2 == obj2;
|
|
99 |
if (!ok && obj2 != null)
|
|
100 |
{
|
|
101 |
ok = obj2.equals(JvmPortTest.mObj2);
|
|
102 |
}
|
|
103 |
assertTrue(info + " failed. obj2 " + obj2 + " != " + " mObj2 " + JvmPortTest.mObj2, ok);
|
|
104 |
|
|
105 |
ok = JvmPortTest.mReturnObj == res;
|
|
106 |
if (!ok && res != null)
|
|
107 |
{
|
|
108 |
ok = res.equals(JvmPortTest.mReturnObj);
|
|
109 |
}
|
|
110 |
assertTrue(info + " failed. res " + res + " != " + " mReturnObj " + JvmPortTest.mReturnObj, ok);
|
|
111 |
|
|
112 |
}
|
|
113 |
// Test cases
|
|
114 |
|
|
115 |
private static final String sysProp = "com.nokia.jvm.port";
|
|
116 |
|
|
117 |
private void setPort(String port)
|
|
118 |
{
|
|
119 |
JvmPortTest.setSystemPropertyImpl(sysProp, port);
|
|
120 |
JvmPortTest.resetInstance();
|
|
121 |
}
|
|
122 |
private void testJvm()
|
|
123 |
{
|
|
124 |
final String testProp = "test.JvmPortTest";
|
|
125 |
String origPort = System.getProperty(sysProp);
|
|
126 |
try
|
|
127 |
{
|
|
128 |
Object testObject1 = new String("testlib");
|
|
129 |
Object testObject2 = null;
|
|
130 |
Object returnObject = null;
|
|
131 |
String methodName;
|
|
132 |
//************ Jvm.loadSystemLibrary ***************************
|
|
133 |
setPort(testProp);
|
|
134 |
Jvm.loadSystemLibrary((String)testObject1);
|
|
135 |
setPort(origPort);
|
|
136 |
methodName = "Jvm.loadSystemLibrary()";
|
|
137 |
checkCallCounts(methodName, JvmPortTest.LOADSYSTEMLIBRARY_CALL);
|
|
138 |
checkObjects(methodName, testObject1, null, null);
|
|
139 |
JvmPortTest.clear();
|
|
140 |
|
|
141 |
//************ Jvm.loadApplicationClass ***************************
|
|
142 |
testObject1 = new String("testClass");
|
|
143 |
JvmPortTest.mReturnObj = getClass();
|
|
144 |
setPort(testProp);
|
|
145 |
returnObject = Jvm.loadApplicationClass((String)testObject1);
|
|
146 |
methodName = "Jvm.loadApplicationClass()";
|
|
147 |
setPort(origPort);
|
|
148 |
checkCallCounts(methodName, JvmPortTest.LOADAPPLICATIONCLASS_CALL);
|
|
149 |
checkObjects(methodName, testObject1, null, returnObject);
|
|
150 |
JvmPortTest.clear();
|
|
151 |
|
|
152 |
JvmPortTest.mThrowable = new ClassNotFoundException();
|
|
153 |
setPort(testProp);
|
|
154 |
try
|
|
155 |
{
|
|
156 |
returnObject = Jvm.loadApplicationClass((String)testObject1);
|
|
157 |
setPort(origPort);
|
|
158 |
assertTrue(methodName + ", no Excpetion", false);
|
|
159 |
}
|
|
160 |
catch (ClassNotFoundException cnfe)
|
|
161 |
{
|
|
162 |
setPort(origPort);
|
|
163 |
assertTrue(methodName + ", wrong exception: " + cnfe,
|
|
164 |
cnfe == JvmPortTest.mThrowable);
|
|
165 |
}
|
|
166 |
setPort(origPort);
|
|
167 |
checkCallCounts(methodName, JvmPortTest.LOADAPPLICATIONCLASS_CALL);
|
|
168 |
JvmPortTest.clear();
|
|
169 |
|
|
170 |
//************ Jvm.setThreadAsDaemon ***************************
|
|
171 |
testObject1 = new Thread(new Runnable()
|
|
172 |
{
|
|
173 |
public void run() {}
|
|
174 |
});
|
|
175 |
testObject2 = new Boolean(true);
|
|
176 |
setPort(testProp);
|
|
177 |
Jvm.setThreadAsDaemon((Thread)testObject1, ((Boolean)testObject2).booleanValue());
|
|
178 |
methodName = "Jvm.setThreadAsDaemon()";
|
|
179 |
setPort(origPort);
|
|
180 |
checkCallCounts(methodName, JvmPortTest.SETTHREADASDAEMON_CALL);
|
|
181 |
checkObjects(methodName, testObject1, testObject2, null);
|
|
182 |
JvmPortTest.clear();
|
|
183 |
|
|
184 |
JvmPortTest.mThrowable = new IllegalThreadStateException();
|
|
185 |
setPort(testProp);
|
|
186 |
try
|
|
187 |
{
|
|
188 |
Jvm.setThreadAsDaemon((Thread)testObject1, ((Boolean)testObject2).booleanValue());
|
|
189 |
setPort(origPort);
|
|
190 |
assertTrue(methodName + ", no Excpetion", false);
|
|
191 |
}
|
|
192 |
catch (IllegalThreadStateException itse)
|
|
193 |
{
|
|
194 |
setPort(origPort);
|
|
195 |
assertTrue(methodName + ", wrong exception: " + itse,
|
|
196 |
itse == JvmPortTest.mThrowable);
|
|
197 |
}
|
|
198 |
setPort(origPort);
|
|
199 |
checkCallCounts(methodName, JvmPortTest.SETTHREADASDAEMON_CALL);
|
|
200 |
JvmPortTest.clear();
|
|
201 |
|
|
202 |
JvmPortTest.mThrowable = new SecurityException();
|
|
203 |
setPort(testProp);
|
|
204 |
try
|
|
205 |
{
|
|
206 |
Jvm.setThreadAsDaemon((Thread)testObject1, ((Boolean)testObject2).booleanValue());
|
|
207 |
setPort(origPort);
|
|
208 |
assertTrue(methodName + ", no Excpetion", false);
|
|
209 |
}
|
|
210 |
catch (SecurityException se)
|
|
211 |
{
|
|
212 |
setPort(origPort);
|
|
213 |
assertTrue(methodName + ", wrong exception: " + se,
|
|
214 |
se == JvmPortTest.mThrowable);
|
|
215 |
}
|
|
216 |
setPort(origPort);
|
|
217 |
checkCallCounts(methodName, JvmPortTest.SETTHREADASDAEMON_CALL);
|
|
218 |
JvmPortTest.clear();
|
|
219 |
|
|
220 |
//************ Jvm.getResourceAsNativeMemory ***************************
|
|
221 |
setPort(testProp);
|
|
222 |
testObject1 = new String("jarPath");
|
|
223 |
testObject2 = new String("resName");
|
|
224 |
JvmPortTest.mReturnObj = new NativeMemoryBlock()
|
|
225 |
{
|
|
226 |
public long getPointer()
|
|
227 |
{
|
|
228 |
return 0;
|
|
229 |
}
|
|
230 |
public int getSize()
|
|
231 |
{
|
|
232 |
return 0;
|
|
233 |
}
|
|
234 |
public void freeMemory() {}
|
|
235 |
};
|
|
236 |
returnObject = Jvm.getResourceAsNativeMemory((String)testObject1, (String)testObject2);
|
|
237 |
methodName = "Jvm.getResourceAsNativeMemory()";
|
|
238 |
setPort(origPort);
|
|
239 |
checkCallCounts(methodName, JvmPortTest.GETRESOURCEASNATIVEMEMORY_CALL);
|
|
240 |
checkObjects(methodName, testObject1, testObject2, returnObject);
|
|
241 |
JvmPortTest.clear();
|
|
242 |
|
|
243 |
//************ JvmInternal.enableRuntimeExit ***************************
|
|
244 |
setPort(testProp);
|
|
245 |
JvmInternal.enableRuntimeExit();
|
|
246 |
methodName = "JvmInternal.enableRuntimeExit()";
|
|
247 |
setPort(origPort);
|
|
248 |
checkCallCounts(methodName, JvmPortTest.ENABLERUNTIMEEXIT_CALL);
|
|
249 |
checkObjects(methodName, null, null, null);
|
|
250 |
JvmPortTest.clear();
|
|
251 |
|
|
252 |
//************ JvmInternal.disableRuntimeExit ***************************
|
|
253 |
setPort(testProp);
|
|
254 |
JvmInternal.disableRuntimeExit();
|
|
255 |
methodName = "JvmInternal.disableRuntimeExit()";
|
|
256 |
setPort(origPort);
|
|
257 |
checkCallCounts(methodName, JvmPortTest.DISABLERUNTIMEEXIT_CALL);
|
|
258 |
checkObjects(methodName, null, null, null);
|
|
259 |
JvmPortTest.clear();
|
|
260 |
|
|
261 |
//************ JvmInternal.exitVm ***************************
|
|
262 |
setPort(testProp);
|
|
263 |
testObject1 = new Integer(42);
|
|
264 |
JvmInternal.exitVm(((Integer)testObject1).intValue());
|
|
265 |
methodName = "JvmInternal.exitVm()";
|
|
266 |
setPort(origPort);
|
|
267 |
checkCallCounts(methodName, JvmPortTest.EXITVM_CALL);
|
|
268 |
checkObjects(methodName, testObject1, null, null);
|
|
269 |
JvmPortTest.clear();
|
|
270 |
|
|
271 |
//************ JvmInternal.enableFinalization ***************************
|
|
272 |
setPort(testProp);
|
|
273 |
testObject1 = getClass();
|
|
274 |
JvmInternal.enableFinalization((Class)testObject1);
|
|
275 |
methodName = "JvmInternal.enableFinalization()";
|
|
276 |
setPort(origPort);
|
|
277 |
checkCallCounts(methodName, JvmPortTest.ENABLEFINALIZATION_CALL);
|
|
278 |
checkObjects(methodName, testObject1, null, null);
|
|
279 |
JvmPortTest.clear();
|
|
280 |
|
|
281 |
//************ JvmInternal.runFinalization ***************************
|
|
282 |
setPort(testProp);
|
|
283 |
JvmInternal.runFinalization();
|
|
284 |
methodName = "JvmInternal.runFinalization()";
|
|
285 |
setPort(origPort);
|
|
286 |
checkCallCounts(methodName, JvmPortTest.RUNFINALIZATION_CALL);
|
|
287 |
checkObjects(methodName, null, null, null);
|
|
288 |
JvmPortTest.clear();
|
|
289 |
|
|
290 |
//************ JvmInternal.runYoungGenerationGc ***************************
|
|
291 |
|
|
292 |
setPort(testProp);
|
|
293 |
JvmPortTest.mReturnObj = new Boolean(true);
|
|
294 |
boolean res = JvmInternal.runYoungGenerationGc();
|
|
295 |
returnObject = new Boolean(res);
|
|
296 |
methodName = "JvmInternal.runYoungGenerationGc()";
|
|
297 |
setPort(origPort);
|
|
298 |
checkCallCounts(methodName, JvmPortTest.RUNYOUNGGENERATIONGC_CALL);
|
|
299 |
checkObjects(methodName, null, null, returnObject);
|
|
300 |
JvmPortTest.clear();
|
|
301 |
|
|
302 |
|
|
303 |
//************ JvmInternal.setThreadEventListener ***************************
|
|
304 |
setPort(testProp);
|
|
305 |
testObject1 = new ThreadEventListener()
|
|
306 |
{
|
|
307 |
public void threadStarting(Thread newThread, Thread parentThread) {}
|
|
308 |
public void threadDied(Thread thread) {}
|
|
309 |
public void uncaughtException(Thread thread, Throwable e) {}
|
|
310 |
};
|
|
311 |
JvmInternal.setThreadEventListener((ThreadEventListener)testObject1);
|
|
312 |
methodName = "JvmInternal.setThreadEventListener()";
|
|
313 |
setPort(origPort);
|
|
314 |
checkCallCounts(methodName, JvmPortTest.SETTHREADEVENTLISTENER_CALL);
|
|
315 |
checkObjects(methodName, testObject1, null, null);
|
|
316 |
JvmPortTest.clear();
|
|
317 |
|
|
318 |
//************ JvmInternal.addRestrictedPackagePrefixes ***************************
|
|
319 |
setPort(testProp);
|
|
320 |
testObject1 = new String[] {"package1, package2, package3"};
|
|
321 |
JvmInternal.addRestrictedPackagePrefixes((String[])testObject1);
|
|
322 |
methodName = "JvmInternal.addRestrictedPackagePrefixes()";
|
|
323 |
setPort(origPort);
|
|
324 |
checkCallCounts(methodName, JvmPortTest.ADDRESTRICTEDPACKAGEPREFIXES_CALL);
|
|
325 |
checkObjects(methodName, testObject1, null, null);
|
|
326 |
JvmPortTest.clear();
|
|
327 |
|
|
328 |
JvmPortTest.mThrowable = new SecurityException();
|
|
329 |
setPort(testProp);
|
|
330 |
try
|
|
331 |
{
|
|
332 |
JvmInternal.addRestrictedPackagePrefixes((String[])testObject1);
|
|
333 |
setPort(origPort);
|
|
334 |
assertTrue(methodName + ", no Excpetion", false);
|
|
335 |
}
|
|
336 |
catch (SecurityException se)
|
|
337 |
{
|
|
338 |
setPort(origPort);
|
|
339 |
assertTrue(methodName + ", wrong exception: " + se,
|
|
340 |
se == JvmPortTest.mThrowable);
|
|
341 |
}
|
|
342 |
setPort(origPort);
|
|
343 |
checkCallCounts(methodName, JvmPortTest.ADDRESTRICTEDPACKAGEPREFIXES_CALL);
|
|
344 |
JvmPortTest.clear();
|
|
345 |
|
|
346 |
//************ JvmInternal.addProtectedPackagePrefixes ***************************
|
|
347 |
setPort(testProp);
|
|
348 |
testObject1 = new String[] {"package4, package5, package6"};
|
|
349 |
JvmInternal.addProtectedPackagePrefixes((String[])testObject1);
|
|
350 |
methodName = "JvmInternal.addProtectedPackagePrefixes()";
|
|
351 |
setPort(origPort);
|
|
352 |
checkCallCounts(methodName, JvmPortTest.ADDPROTECTEDPACKAGEPREFIXES_CALL);
|
|
353 |
checkObjects(methodName, testObject1, null, null);
|
|
354 |
JvmPortTest.clear();
|
|
355 |
|
|
356 |
JvmPortTest.mThrowable = new SecurityException();
|
|
357 |
setPort(testProp);
|
|
358 |
try
|
|
359 |
{
|
|
360 |
JvmInternal.addProtectedPackagePrefixes((String[])testObject1);
|
|
361 |
setPort(origPort);
|
|
362 |
assertTrue(methodName + ", no Excpetion", false);
|
|
363 |
}
|
|
364 |
catch (SecurityException se)
|
|
365 |
{
|
|
366 |
setPort(origPort);
|
|
367 |
assertTrue(methodName + ", wrong exception: " + se,
|
|
368 |
se == JvmPortTest.mThrowable);
|
|
369 |
}
|
|
370 |
setPort(origPort);
|
|
371 |
checkCallCounts(methodName, JvmPortTest.ADDPROTECTEDPACKAGEPREFIXES_CALL);
|
|
372 |
JvmPortTest.clear();
|
|
373 |
|
|
374 |
//************ JvmInternal.appendToClassPath ***************************
|
|
375 |
setPort(testProp);
|
|
376 |
testObject1 = new String("class to append");
|
|
377 |
JvmInternal.appendToClassPath((String)testObject1);
|
|
378 |
methodName = "JvmInternal.appendToClassPath()";
|
|
379 |
setPort(origPort);
|
|
380 |
checkCallCounts(methodName, JvmPortTest.APPENDTOCLASSPATH_CALL);
|
|
381 |
checkObjects(methodName, testObject1, null, null);
|
|
382 |
JvmPortTest.clear();
|
|
383 |
|
|
384 |
//************ JvmInternal.shrinkJavaHeapToMinimum ***************************
|
|
385 |
setPort(testProp);
|
|
386 |
JvmPortTest.mReturnObj = new Integer(22);
|
|
387 |
int res2 = JvmInternal.shrinkJavaHeapToMinimum();
|
|
388 |
returnObject = new Integer(res2);
|
|
389 |
methodName = "JvmInternal.shrinkJavaHeapToMinimum()";
|
|
390 |
setPort(origPort);
|
|
391 |
checkCallCounts(methodName, JvmPortTest.SHRINKJAVAHEAPTOMINIMUM_CALL);
|
|
392 |
checkObjects(methodName, null, null, returnObject);
|
|
393 |
JvmPortTest.clear();
|
|
394 |
|
|
395 |
//************ JvmInternal.expandJavaHeap ***************************
|
|
396 |
setPort(testProp);
|
|
397 |
testObject1 = new Integer(33);
|
|
398 |
JvmPortTest.mReturnObj = new Integer(44);
|
|
399 |
int res3 = JvmInternal.expandJavaHeap(((Integer)testObject1).intValue());
|
|
400 |
returnObject = new Integer(res3);
|
|
401 |
methodName = "JvmInternal.expandJavaHeap()";
|
|
402 |
setPort(origPort);
|
|
403 |
checkCallCounts(methodName, JvmPortTest.EXPANDJAVAHEAP_CALL);
|
|
404 |
checkObjects(methodName, testObject1, null, returnObject);
|
|
405 |
JvmPortTest.clear();
|
|
406 |
|
|
407 |
//************ END***************************
|
|
408 |
assertTrue("Calls missed", JvmPortTest.allCalled());
|
|
409 |
|
|
410 |
}
|
|
411 |
catch (Throwable t)
|
|
412 |
{
|
|
413 |
setPort(origPort);
|
|
414 |
t.printStackTrace();
|
|
415 |
assertTrue(t.toString(), false);
|
|
416 |
}
|
|
417 |
setPort(origPort);
|
|
418 |
}
|
|
419 |
|
|
420 |
private void testVmPortNotExist()
|
|
421 |
{
|
|
422 |
System.out.println("NOTE!! IT IS EXPECTED THAT THIS TEST WILL LEAD TO SOME ERROR LOGS.");
|
|
423 |
final String testProp = "test.JvmPortTest";
|
|
424 |
String origPort = System.getProperty(sysProp);
|
|
425 |
try
|
|
426 |
{
|
|
427 |
setPort("TestPort");
|
|
428 |
JvmInternal.enableRuntimeExit();
|
|
429 |
setPort(origPort);
|
|
430 |
assertTrue("No exception.", false);
|
|
431 |
}
|
|
432 |
catch (RuntimeException re)
|
|
433 |
{
|
|
434 |
setPort(origPort);
|
|
435 |
int ind = re.toString().indexOf("Not able to instantiate class com.nokia.mj.impl.rt.TestPort");
|
|
436 |
boolean ok = ind >= 0;
|
|
437 |
if (!ok)
|
|
438 |
{
|
|
439 |
// Accept also java.lang.ExceptionInInitializerError.
|
|
440 |
ok = re.toString().equals("java.lang.ExceptionInInitializerError");
|
|
441 |
}
|
|
442 |
assertTrue(re.toString(), ok);
|
|
443 |
}
|
|
444 |
catch (Throwable t)
|
|
445 |
{
|
|
446 |
t.printStackTrace();
|
|
447 |
assertTrue(t.toString(), false);
|
|
448 |
}
|
|
449 |
|
|
450 |
try
|
|
451 |
{
|
|
452 |
setPort(origPort);
|
|
453 |
}
|
|
454 |
catch (Throwable t)
|
|
455 |
{
|
|
456 |
}
|
|
457 |
}
|
|
458 |
}
|