|
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 |
|
21 |
|
22 import com.nokia.mj.impl.rt.test.UnitTestSuiteCreator; |
|
23 import com.nokia.mj.impl.rt.support.ShutdownListener; |
|
24 import com.nokia.mj.impl.rt.support.Jvm; |
|
25 import com.nokia.mj.impl.rt.support.JvmInternal; |
|
26 import j2meunit.framework.Test; |
|
27 import j2meunit.framework.TestCase; |
|
28 import j2meunit.framework.TestMethod; |
|
29 import j2meunit.framework.TestSuite; |
|
30 |
|
31 import com.nokia.mj.impl.rt.support.prot.ProtectedTest; |
|
32 /** |
|
33 * JvmPort unit tests. |
|
34 */ |
|
35 public class JvmPortTest extends TestCase implements UnitTestSuiteCreator |
|
36 { |
|
37 |
|
38 // Begin j2meunit test framework setup |
|
39 public TestSuite createTestSuite(String[] args) |
|
40 { |
|
41 TestSuite suite = new TestSuite(this.getClass().getName()); |
|
42 String testSelector = "0000000000000000"; |
|
43 if (args.length > 1 && args[1] != null) |
|
44 { |
|
45 testSelector = args[1] + "0000000000000000"; |
|
46 } |
|
47 |
|
48 if (testSelector.charAt(0) != '0') |
|
49 { |
|
50 suite.addTest(new JvmPortTest("testLoadSystemLibrary", new TestMethod() |
|
51 { |
|
52 public void run(TestCase tc) |
|
53 { |
|
54 ((JvmPortTest)tc).testLoadSystemLibrary(); |
|
55 } |
|
56 })); |
|
57 } |
|
58 |
|
59 |
|
60 if (testSelector.charAt(0) != '0') |
|
61 { |
|
62 suite.addTest(new JvmPortTest("testLoadApplicationClass", new TestMethod() |
|
63 { |
|
64 public void run(TestCase tc) |
|
65 { |
|
66 ((JvmPortTest)tc).testLoadApplicationClass(); |
|
67 } |
|
68 })); |
|
69 } |
|
70 |
|
71 if (testSelector.charAt(1) != '0') |
|
72 { |
|
73 suite.addTest(new JvmPortTest("testSetThreadAsDaemon", new TestMethod() |
|
74 { |
|
75 public void run(TestCase tc) |
|
76 { |
|
77 ((JvmPortTest)tc).testSetThreadAsDaemon(); |
|
78 } |
|
79 })); |
|
80 } |
|
81 |
|
82 |
|
83 if (testSelector.charAt(0) != '0') |
|
84 { |
|
85 suite.addTest(new JvmPortTest("testGetResourceAsNativeMemory", new TestMethod() |
|
86 { |
|
87 public void run(TestCase tc) |
|
88 { |
|
89 ((JvmPortTest)tc).testGetResourceAsNativeMemory(); |
|
90 } |
|
91 })); |
|
92 } |
|
93 |
|
94 if (testSelector.charAt(2) != '0') |
|
95 { |
|
96 suite.addTest(new JvmPortTest("testEnableRuntimeExit", new TestMethod() |
|
97 { |
|
98 public void run(TestCase tc) |
|
99 { |
|
100 ((JvmPortTest)tc).testEnableRuntimeExit(); |
|
101 } |
|
102 })); |
|
103 } |
|
104 |
|
105 |
|
106 |
|
107 if (testSelector.charAt(0) != '0') |
|
108 { |
|
109 suite.addTest(new JvmPortTest("testDisableRuntimeExit", new TestMethod() |
|
110 { |
|
111 public void run(TestCase tc) |
|
112 { |
|
113 ((JvmPortTest)tc).testDisableRuntimeExit(); |
|
114 } |
|
115 })); |
|
116 } |
|
117 |
|
118 |
|
119 if (testSelector.charAt(3) != '0') |
|
120 { |
|
121 suite.addTest(new JvmPortTest("testExitVm", new TestMethod() |
|
122 { |
|
123 public void run(TestCase tc) |
|
124 { |
|
125 ((JvmPortTest)tc).testExitVm(); |
|
126 } |
|
127 })); |
|
128 } |
|
129 |
|
130 if (testSelector.charAt(0) != '0') |
|
131 { |
|
132 suite.addTest(new JvmPortTest("testYoungGenerationGc", new TestMethod() |
|
133 { |
|
134 public void run(TestCase tc) |
|
135 { |
|
136 ((JvmPortTest)tc).testYoungGenerationGc(); |
|
137 } |
|
138 })); |
|
139 } |
|
140 |
|
141 if (testSelector.charAt(4) != '0') |
|
142 { |
|
143 suite.addTest(new JvmPortTest("testFinalization", new TestMethod() |
|
144 { |
|
145 public void run(TestCase tc) |
|
146 { |
|
147 ((JvmPortTest)tc).testFinalization(); |
|
148 } |
|
149 })); |
|
150 } |
|
151 |
|
152 if (testSelector.charAt(0) != '0') |
|
153 { |
|
154 suite.addTest(new JvmPortTest("testSetThreadEventListener", new TestMethod() |
|
155 { |
|
156 public void run(TestCase tc) |
|
157 { |
|
158 ((JvmPortTest)tc).testSetThreadEventListener(); |
|
159 } |
|
160 })); |
|
161 } |
|
162 |
|
163 if (testSelector.charAt(8) != '0') |
|
164 { |
|
165 suite.addTest(new JvmPortTest("testAddRestrictedPackagePrefixes", new TestMethod() |
|
166 { |
|
167 public void run(TestCase tc) |
|
168 { |
|
169 ((JvmPortTest)tc).testAddRestrictedPackagePrefixes(); |
|
170 } |
|
171 })); |
|
172 } |
|
173 |
|
174 if (testSelector.charAt(0) != '0') |
|
175 { |
|
176 suite.addTest(new JvmPortTest("testAddProtectedPackagePrefixes", new TestMethod() |
|
177 { |
|
178 public void run(TestCase tc) |
|
179 { |
|
180 ((JvmPortTest)tc).testAddProtectedPackagePrefixes(); |
|
181 } |
|
182 })); |
|
183 } |
|
184 |
|
185 |
|
186 if (testSelector.charAt(5) != '0') |
|
187 { |
|
188 suite.addTest(new JvmPortTest("testAppendToClassPath", new TestMethod() |
|
189 { |
|
190 public void run(TestCase tc) |
|
191 { |
|
192 ((JvmPortTest)tc).testAppendToClassPath(); |
|
193 } |
|
194 })); |
|
195 } |
|
196 |
|
197 |
|
198 if (testSelector.charAt(6) != '0') |
|
199 { |
|
200 suite.addTest(new JvmPortTest("testShrinkJavaHeapToMinimum", new TestMethod() |
|
201 { |
|
202 public void run(TestCase tc) |
|
203 { |
|
204 ((JvmPortTest)tc).testShrinkJavaHeapToMinimum(); |
|
205 } |
|
206 })); |
|
207 } |
|
208 |
|
209 if (testSelector.charAt(7) != '0') |
|
210 { |
|
211 suite.addTest(new JvmPortTest("testExpandJavaHeap", new TestMethod() |
|
212 { |
|
213 public void run(TestCase tc) |
|
214 { |
|
215 ((JvmPortTest)tc).testExpandJavaHeap(); |
|
216 } |
|
217 })); |
|
218 } |
|
219 |
|
220 |
|
221 return suite; |
|
222 |
|
223 } |
|
224 |
|
225 public JvmPortTest() |
|
226 { |
|
227 } |
|
228 |
|
229 public JvmPortTest(String aTestName, TestMethod aTestMethod) |
|
230 { |
|
231 super(aTestName, aTestMethod); |
|
232 } |
|
233 |
|
234 // End j2meunit test framework setup |
|
235 |
|
236 protected void setUp() |
|
237 { |
|
238 } |
|
239 |
|
240 protected void tearDown() |
|
241 { |
|
242 } |
|
243 |
|
244 private static class ShutdownListenerImpl implements ShutdownListener |
|
245 { |
|
246 public int mShutDownCallCount = 0; |
|
247 public void shuttingDown() |
|
248 { |
|
249 mShutDownCallCount++; |
|
250 } |
|
251 } |
|
252 // Test cases |
|
253 |
|
254 private void testLoadSystemLibrary() |
|
255 { |
|
256 try |
|
257 { |
|
258 //************ Jvm.loadSystemLibrary *************************** |
|
259 Jvm.loadSystemLibrary("javacommonutilstest"); |
|
260 |
|
261 try |
|
262 { |
|
263 Jvm.loadSystemLibrary("nonexisting"); |
|
264 assertTrue("Call should have failed", false); |
|
265 } |
|
266 catch (Error e) |
|
267 { |
|
268 int ind = e.toString().indexOf("Not able to load library nonexisting."); |
|
269 boolean ok = ind >= 0; |
|
270 if (!ok) |
|
271 { |
|
272 // Accept also java.lang.ExceptionInInitializerError. |
|
273 ok = e.toString().startsWith("java.lang.UnsatisfiedLinkError"); |
|
274 // ok = e instanceof UnsatisfiedLinkError; |
|
275 } |
|
276 assertTrue(e.toString(), ok); |
|
277 } |
|
278 } |
|
279 catch (Throwable t) |
|
280 { |
|
281 t.printStackTrace(); |
|
282 assertTrue(t.toString(), false); |
|
283 } |
|
284 } |
|
285 |
|
286 private void testLoadApplicationClass() |
|
287 { |
|
288 try |
|
289 { |
|
290 //************ Jvm.loadApplicationClass *************************** |
|
291 javax.microedition.rms.RecordComparator obj = (javax.microedition.rms.RecordComparator) |
|
292 Jvm.loadApplicationClass("com.nokia.mj.impl.test.jvmporttest.TestClass").newInstance(); |
|
293 assertTrue("Object was null", obj != null); |
|
294 int res = obj.compare(null, null); |
|
295 assertTrue("Incorrect result: " + res, res == 42); |
|
296 } |
|
297 catch (Throwable t) |
|
298 { |
|
299 t.printStackTrace(); |
|
300 assertTrue(t.toString(), false); |
|
301 } |
|
302 } |
|
303 |
|
304 private boolean isCldc() |
|
305 { |
|
306 String cldcProp = System.getProperty("com.nokia.jvm.port"); |
|
307 if (cldcProp != null && cldcProp.equals("j9.JvmPortCldc")) |
|
308 { |
|
309 return true; |
|
310 } |
|
311 return false; |
|
312 |
|
313 } |
|
314 private void testSetThreadAsDaemon() |
|
315 { |
|
316 try |
|
317 { |
|
318 //************ Jvm.setThreadAsDaemon *************************** |
|
319 Thread th = new Thread(new Runnable() |
|
320 { |
|
321 public void run() |
|
322 { |
|
323 try |
|
324 { |
|
325 Thread.sleep(2000); |
|
326 } |
|
327 catch (Exception re) |
|
328 { |
|
329 } |
|
330 } |
|
331 }); |
|
332 if (isCldc()) |
|
333 { |
|
334 try |
|
335 { |
|
336 Jvm.setThreadAsDaemon(th, false); |
|
337 } |
|
338 catch (RuntimeException re) |
|
339 { |
|
340 assertTrue(re.toString(), re.toString().indexOf("Setting thread as non-daemon not supported") >= 0); |
|
341 } |
|
342 } |
|
343 else |
|
344 { |
|
345 Jvm.setThreadAsDaemon(th, false); |
|
346 } |
|
347 Jvm.setThreadAsDaemon(th, true); |
|
348 th.start(); |
|
349 } |
|
350 catch (Throwable t) |
|
351 { |
|
352 t.printStackTrace(); |
|
353 assertTrue(t.toString(), false); |
|
354 } |
|
355 } |
|
356 |
|
357 private static final String jarFile = "c:\\java\\jvmportapp.jar"; |
|
358 private static final String jarFile2 = "c:\\java\\jvmportapp2.jar"; |
|
359 |
|
360 private void testGetResourceAsNativeMemory() |
|
361 { |
|
362 if (!isCldc()) |
|
363 { |
|
364 return; |
|
365 } |
|
366 try |
|
367 { |
|
368 //************ Jvm.getResourceAsNativeMemory *************************** |
|
369 NativeMemoryBlock nativeMemory = Jvm.getResourceAsNativeMemory(null, "myres/info.txt"); |
|
370 assertTrue("NativeMemoryBlock was null", nativeMemory != null); |
|
371 |
|
372 NativeMemoryBlock nativeMemory2 = Jvm.getResourceAsNativeMemory(null, "myres/info2.txt"); |
|
373 assertTrue("NativeMemoryBlock2 was null", nativeMemory2 != null); |
|
374 |
|
375 String res = _getResource(nativeMemory.getPointer(), nativeMemory.getSize()); |
|
376 assertTrue("Incorrect content1: " + res, res.equals("jvmPort(info): The quick brown fox jumps over the lazy dog")); |
|
377 |
|
378 res = _getResource(nativeMemory2.getPointer(), nativeMemory2.getSize()); |
|
379 assertTrue("Incorrect content2 " + res, res.equals("jvmPort(info2): The quick brown fox jumps over the lazy dog")); |
|
380 |
|
381 nativeMemory.freeMemory(); |
|
382 nativeMemory2.freeMemory(); |
|
383 |
|
384 NativeMemoryBlock nativeMemory3 = Jvm.getResourceAsNativeMemory(jarFile2, "myres/info.txt"); |
|
385 assertTrue("NativeMemoryBlock3 was null", nativeMemory3 != null); |
|
386 |
|
387 NativeMemoryBlock nativeMemory4 = Jvm.getResourceAsNativeMemory(jarFile2, "myres/info2.txt"); |
|
388 assertTrue("NativeMemoryBlock4 was null", nativeMemory4 != null); |
|
389 |
|
390 res = _getResource(nativeMemory3.getPointer(), nativeMemory3.getSize()); |
|
391 assertTrue("Incorrect content3: " + res, res.equals("jvmPort2(info): The quick brown fox jumps over the lazy dog")); |
|
392 |
|
393 res = _getResource(nativeMemory4.getPointer(), nativeMemory4.getSize()); |
|
394 assertTrue("Incorrect content4 " + res, res.equals("jvmPort2(info2): The quick brown fox jumps over the lazy dog")); |
|
395 |
|
396 nativeMemory3.freeMemory(); |
|
397 nativeMemory4.freeMemory(); |
|
398 |
|
399 NativeMemoryBlock nativeMemory5 = Jvm.getResourceAsNativeMemory(jarFile2 + "t", "myres/info.txt"); |
|
400 assertTrue("NativeMemoryBlock5 was not null", nativeMemory5 == null); |
|
401 |
|
402 NativeMemoryBlock nativeMemory6 = Jvm.getResourceAsNativeMemory(jarFile2 + "t", "myres/info2.txt"); |
|
403 assertTrue("NativeMemoryBlock6 was not null", nativeMemory6 == null); |
|
404 |
|
405 |
|
406 } |
|
407 catch (Throwable t) |
|
408 { |
|
409 t.printStackTrace(); |
|
410 assertTrue(t.toString(), false); |
|
411 } |
|
412 } |
|
413 |
|
414 private void testEnableRuntimeExit() |
|
415 { |
|
416 try |
|
417 { |
|
418 //************ JvmInternal.enableRuntimeExit *************************** |
|
419 if (isCldc()) |
|
420 { |
|
421 JvmInternal.enableRuntimeExit(); |
|
422 } |
|
423 System.exit(40); |
|
424 } |
|
425 catch (Throwable t) |
|
426 { |
|
427 t.printStackTrace(); |
|
428 assertTrue(t.toString(), false); |
|
429 } |
|
430 } |
|
431 |
|
432 private void testDisableRuntimeExit() |
|
433 { |
|
434 if (!isCldc()) |
|
435 { |
|
436 return; |
|
437 } |
|
438 |
|
439 try |
|
440 { |
|
441 //************ JvmInternal.disableRuntimeExit *************************** |
|
442 JvmInternal.disableRuntimeExit(); |
|
443 try |
|
444 { |
|
445 System.exit(-123); |
|
446 assertTrue("System exit should have failed", false); |
|
447 } |
|
448 catch (SecurityException se) |
|
449 { |
|
450 } |
|
451 } |
|
452 catch (Throwable t) |
|
453 { |
|
454 t.printStackTrace(); |
|
455 assertTrue(t.toString(), false); |
|
456 } |
|
457 JvmInternal.enableRuntimeExit(); |
|
458 } |
|
459 |
|
460 private void testExitVm() |
|
461 { |
|
462 try |
|
463 { |
|
464 //************ JvmInternal.exitVm *************************** |
|
465 JvmInternal.exitVm(42); |
|
466 } |
|
467 catch (Throwable t) |
|
468 { |
|
469 t.printStackTrace(); |
|
470 assertTrue(t.toString(), false); |
|
471 } |
|
472 } |
|
473 |
|
474 private void testYoungGenerationGc() |
|
475 { |
|
476 try |
|
477 { |
|
478 //************ JvmInternal.runYoungGenerationGc *************************** |
|
479 Runtime rt = Runtime.getRuntime(); |
|
480 { |
|
481 new FinalizeClass(); |
|
482 new FinalizeClass(); |
|
483 new FinalizeClass(); |
|
484 } |
|
485 long startFree = rt.freeMemory(); |
|
486 boolean supported = JvmInternal.runYoungGenerationGc(); |
|
487 long endFree = rt.freeMemory(); |
|
488 if (supported) |
|
489 { |
|
490 assertTrue("endFree (" + endFree + ") <= startFree (" + startFree + ")", startFree < endFree); |
|
491 } |
|
492 } |
|
493 catch (Throwable t) |
|
494 { |
|
495 t.printStackTrace(); |
|
496 assertTrue(t.toString(), false); |
|
497 } |
|
498 } |
|
499 |
|
500 private static class FinalizeClass |
|
501 { |
|
502 private static boolean mFinalized = false; |
|
503 |
|
504 static |
|
505 { |
|
506 JvmInternal.enableFinalization(JvmPortTest.FinalizeClass.class); |
|
507 } |
|
508 |
|
509 protected final void finalize() |
|
510 { |
|
511 mFinalized = true; |
|
512 } |
|
513 } |
|
514 |
|
515 private void testFinalization() |
|
516 { |
|
517 try |
|
518 { |
|
519 //************ JvmInternal.runYoungGenerationGc *************************** |
|
520 FinalizeClass fClass = new FinalizeClass(); |
|
521 fClass = null; |
|
522 System.gc(); |
|
523 JvmInternal.runFinalization(); |
|
524 assertTrue("Class was not finalized", FinalizeClass.mFinalized); |
|
525 |
|
526 } |
|
527 catch (Throwable t) |
|
528 { |
|
529 t.printStackTrace(); |
|
530 assertTrue(t.toString(), false); |
|
531 } |
|
532 } |
|
533 |
|
534 private class TestListener implements ThreadEventListener |
|
535 { |
|
536 private int startCount = 0; |
|
537 private int diedCount = 0; |
|
538 private int uncaughtCount = 0; |
|
539 private Throwable throwable; |
|
540 private Thread throwngThread; |
|
541 private Thread newThread; |
|
542 private Thread parentThread; |
|
543 private Thread dyingThread; |
|
544 |
|
545 public synchronized void threadStarting(Thread newThread, Thread parentThread) |
|
546 { |
|
547 startCount++; |
|
548 this.newThread = newThread; |
|
549 this.parentThread = parentThread; |
|
550 } |
|
551 public synchronized void threadDied(Thread thread) |
|
552 { |
|
553 diedCount++; |
|
554 dyingThread = thread; |
|
555 } |
|
556 public synchronized void uncaughtException(Thread thread, Throwable t) |
|
557 { |
|
558 throwngThread = thread; |
|
559 throwable = t; |
|
560 uncaughtCount++; |
|
561 } |
|
562 |
|
563 } |
|
564 private void testSetThreadEventListener() |
|
565 { |
|
566 try |
|
567 { |
|
568 Thread currentThread = Thread.currentThread(); |
|
569 //************ JvmInternal.setThreadEventListener *************************** |
|
570 TestListener listener = new TestListener(); |
|
571 JvmInternal.setThreadEventListener(listener); |
|
572 Thread th = new Thread(new Runnable() |
|
573 { |
|
574 public void run() {} |
|
575 }); |
|
576 th.start(); |
|
577 Thread.sleep(50); |
|
578 assertTrue("new thread not correct(1)", th == listener.newThread); |
|
579 assertTrue("parent thread not correct(1)", currentThread == listener.parentThread); |
|
580 assertTrue("died thread not correct(1)", th == listener.dyingThread); |
|
581 |
|
582 th = new Thread(new Runnable() |
|
583 { |
|
584 public void run() {} |
|
585 }); |
|
586 th.start(); |
|
587 Thread.sleep(50); |
|
588 assertTrue("new thread not correct(2)", th == listener.newThread); |
|
589 assertTrue("parent thread not correct(2)", currentThread == listener.parentThread); |
|
590 assertTrue("died thread not correct(2)", th == listener.dyingThread); |
|
591 |
|
592 th = new Thread(new Runnable() |
|
593 { |
|
594 public void run() {} |
|
595 }); |
|
596 th.start(); |
|
597 Thread.sleep(50); |
|
598 assertTrue("new thread not correct(3)", th == listener.newThread); |
|
599 assertTrue("parent thread not correct(3)", currentThread == listener.parentThread); |
|
600 assertTrue("died thread not correct(3)", th == listener.dyingThread); |
|
601 |
|
602 final RuntimeException re = new RuntimeException("ex"); |
|
603 th = new Thread(new Runnable() |
|
604 { |
|
605 public void run() |
|
606 { |
|
607 throw re; |
|
608 } |
|
609 }); |
|
610 th.start(); |
|
611 Thread.sleep(150); |
|
612 assertTrue("new thread not correct(4)", th == listener.newThread); |
|
613 assertTrue("parent thread not correct(4)", currentThread == listener.parentThread); |
|
614 assertTrue("died thread not correct(4)", th == listener.dyingThread); |
|
615 assertTrue("Throwable not correct(4)", re == listener.throwable); |
|
616 |
|
617 assertTrue("Start count not correct: " + listener.startCount, listener.startCount == 4); |
|
618 assertTrue("End count not correct: " + listener.diedCount, listener.diedCount == 4); |
|
619 assertTrue("Throwable count not correct: " + listener.uncaughtCount, listener.uncaughtCount == 1); |
|
620 } |
|
621 catch (Throwable t) |
|
622 { |
|
623 t.printStackTrace(); |
|
624 assertTrue(t.toString(), false); |
|
625 } |
|
626 } |
|
627 |
|
628 private void testAddRestrictedPackagePrefixes() |
|
629 { |
|
630 if (!isCldc()) |
|
631 { |
|
632 return; |
|
633 } |
|
634 try |
|
635 { |
|
636 //************ JvmInternal.addRestrictedPackagePrefixes *************************** |
|
637 |
|
638 javax.microedition.rms.RecordComparator obj0 = (javax.microedition.rms.RecordComparator) |
|
639 Jvm.loadApplicationClass("com.nokia.mj.impl.test.jvmporttest2.TestClass").newInstance(); |
|
640 assertTrue("Object was null", obj0 != null); |
|
641 int res = obj0.compare(null, new byte[1]); |
|
642 assertTrue("Restricted test failed: " + res, res == 0); |
|
643 |
|
644 JvmInternal.addRestrictedPackagePrefixes(new String[] {"com.nokia.mj.impl.rt.support.restricted."}); |
|
645 |
|
646 javax.microedition.rms.RecordComparator obj = (javax.microedition.rms.RecordComparator) |
|
647 Jvm.loadApplicationClass("com.nokia.mj.impl.test.jvmporttest2.TestClass").newInstance(); |
|
648 assertTrue("Object was null", obj != null); |
|
649 res = obj.compare(new byte[1], null); |
|
650 assertTrue("Restricted test failed2: " + res, res == 0); |
|
651 } |
|
652 catch (Throwable t) |
|
653 { |
|
654 t.printStackTrace(); |
|
655 assertTrue(t.toString(), false); |
|
656 } |
|
657 } |
|
658 |
|
659 private void testAddProtectedPackagePrefixes() |
|
660 { |
|
661 if (!isCldc()) |
|
662 { |
|
663 return; |
|
664 } |
|
665 try |
|
666 { |
|
667 //************ JvmInternal.addProtectedPackagePrefixes *************************** |
|
668 javax.microedition.rms.RecordComparator obj0 = (javax.microedition.rms.RecordComparator) |
|
669 Jvm.loadApplicationClass("com.nokia.mj.impl.test.jvmporttest.TestClass").newInstance(); |
|
670 assertTrue("Object was null", obj0 != null); |
|
671 int res = obj0.compare(null, new byte[1]); |
|
672 assertTrue("Protected test failed: " + res, res == 0); |
|
673 |
|
674 JvmInternal.addProtectedPackagePrefixes(new String[] {"com.nokia.mj.impl.rt.support.prot."}); |
|
675 |
|
676 javax.microedition.rms.RecordComparator obj = (javax.microedition.rms.RecordComparator) |
|
677 Jvm.loadApplicationClass("com.nokia.mj.impl.test.jvmporttest.TestClass").newInstance(); |
|
678 assertTrue("Object was null", obj != null); |
|
679 res = obj.compare(new byte[1], null); |
|
680 assertTrue("Protected test failed2: " + res, res == 0); |
|
681 } |
|
682 catch (Throwable t) |
|
683 { |
|
684 t.printStackTrace(); |
|
685 assertTrue(t.toString(), false); |
|
686 } |
|
687 } |
|
688 |
|
689 private void testAppendToClassPath() |
|
690 { |
|
691 try |
|
692 { |
|
693 //************ JvmInternal.appendToClassPath *************************** |
|
694 javax.microedition.rms.RecordComparator obj = null; |
|
695 try |
|
696 { |
|
697 obj = (javax.microedition.rms.RecordComparator) |
|
698 Jvm.loadApplicationClass("com.nokia.mj.impl.test.jvmporttest.TestClass").newInstance(); |
|
699 assertTrue("Shouldn't be found.", false); |
|
700 } |
|
701 catch (ClassNotFoundException cnfe) {} |
|
702 |
|
703 try |
|
704 { |
|
705 obj = (javax.microedition.rms.RecordComparator) |
|
706 Jvm.loadApplicationClass("com.nokia.mj.impl.test.jvmporttest2.TestClass").newInstance(); |
|
707 assertTrue("Shouldn't be found2.", false); |
|
708 } |
|
709 catch (ClassNotFoundException cnfe) |
|
710 { |
|
711 } |
|
712 |
|
713 JvmInternal.appendToClassPath(jarFile); |
|
714 obj = (javax.microedition.rms.RecordComparator) |
|
715 Jvm.loadApplicationClass("com.nokia.mj.impl.test.jvmporttest.TestClass").newInstance(); |
|
716 assertTrue("Object was null", obj != null); |
|
717 int res = obj.compare(null, null); |
|
718 assertTrue("Incorrect result: " + res, res == 42); |
|
719 |
|
720 try |
|
721 { |
|
722 obj = (javax.microedition.rms.RecordComparator) |
|
723 Jvm.loadApplicationClass("com.nokia.mj.impl.test.jvmporttest2.TestClass").newInstance(); |
|
724 assertTrue("Shouldn't be found3.", false); |
|
725 } |
|
726 catch (ClassNotFoundException cnfe) |
|
727 { |
|
728 } |
|
729 |
|
730 JvmInternal.appendToClassPath(jarFile2); |
|
731 obj = (javax.microedition.rms.RecordComparator) |
|
732 Jvm.loadApplicationClass("com.nokia.mj.impl.test.jvmporttest.TestClass").newInstance(); |
|
733 assertTrue("Object was null 2", obj != null); |
|
734 res = obj.compare(null, null); |
|
735 assertTrue("Incorrect result(2): " + res, res == 42); |
|
736 |
|
737 obj = (javax.microedition.rms.RecordComparator) |
|
738 Jvm.loadApplicationClass("com.nokia.mj.impl.test.jvmporttest2.TestClass").newInstance(); |
|
739 assertTrue("Object was null 3", obj != null); |
|
740 res = obj.compare(null, null); |
|
741 assertTrue("Incorrect result(3): " + res, res == 43); |
|
742 |
|
743 } |
|
744 catch (Throwable t) |
|
745 { |
|
746 t.printStackTrace(); |
|
747 assertTrue(t.toString(), false); |
|
748 } |
|
749 } |
|
750 |
|
751 |
|
752 private void testShrinkJavaHeapToMinimum() |
|
753 { |
|
754 try |
|
755 { |
|
756 //************ JvmInternal.shrinkJavaHeapToMinimum *************************** |
|
757 Object[] o = new Object[10000]; |
|
758 assertTrue("Alocation failed: " + o.length, o.length > 0); |
|
759 |
|
760 Runtime rt = Runtime.getRuntime(); |
|
761 long startTotal = rt.totalMemory(); |
|
762 o = null; |
|
763 int shrinkAmount = JvmInternal.shrinkJavaHeapToMinimum(); |
|
764 long endTotal = rt.totalMemory(); |
|
765 assertTrue("Heap shrink failed: " + shrinkAmount, shrinkAmount > 0); |
|
766 assertTrue("Heap shrink failed, total mem was not reduced. endTotal: " + endTotal + ", startTotal: " + startTotal, endTotal < startTotal); |
|
767 } |
|
768 catch (Throwable t) |
|
769 { |
|
770 t.printStackTrace(); |
|
771 assertTrue(t.toString(), false); |
|
772 } |
|
773 } |
|
774 |
|
775 private void testExpandJavaHeap() |
|
776 { |
|
777 try |
|
778 { |
|
779 //************ JvmInternal.expandJavaHeap *************************** |
|
780 Runtime rt = Runtime.getRuntime(); |
|
781 long startTotal = rt.totalMemory(); |
|
782 final int expandSize = 1024 * 1024; |
|
783 int expanded = JvmInternal.expandJavaHeap(expandSize); |
|
784 assertTrue("Didn't expand as expected: " + expanded, expandSize == expanded); |
|
785 long endTotal = rt.totalMemory(); |
|
786 assertTrue("Runtime.totalMemory not what expected. startTotal: " + startTotal + ", endTotal: " + endTotal, (endTotal - startTotal) == expandSize); |
|
787 } |
|
788 catch (Throwable t) |
|
789 { |
|
790 t.printStackTrace(); |
|
791 assertTrue(t.toString(), false); |
|
792 } |
|
793 } |
|
794 |
|
795 private native String _getResource(long ptr, int size); |
|
796 |
|
797 } |
|
798 |
|
799 |