javacommons/utils/tsrc/javasrc/com/nokia/mj/impl/rt/support/FinalizerTest.java
changeset 80 d6dafc5d983f
equal deleted inserted replaced
78:71ad690e91f5 80:d6dafc5d983f
       
     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 j2meunit.framework.Test;
       
    24 import j2meunit.framework.TestCase;
       
    25 import j2meunit.framework.TestMethod;
       
    26 import j2meunit.framework.TestSuite;
       
    27 
       
    28 import com.nokia.mj.impl.rt.support.JvmInternal;
       
    29 
       
    30 /**
       
    31  * Finalizer unit tests.
       
    32  */
       
    33 public class FinalizerTest extends TestCase implements UnitTestSuiteCreator
       
    34 {
       
    35 
       
    36     // Begin j2meunit test framework setup
       
    37     public TestSuite createTestSuite(String[] args)
       
    38     {
       
    39         TestSuite suite = new TestSuite(this.getClass().getName());
       
    40 
       
    41         suite.addTest(new FinalizerTest("testFinalizerSingleClass", new TestMethod()
       
    42         {
       
    43             public void run(TestCase tc)
       
    44             {
       
    45                 ((FinalizerTest)tc).testFinalizerSingleClass();
       
    46             }
       
    47         }));
       
    48 
       
    49         suite.addTest(new FinalizerTest("testFinalizerManyClassesSingleObject", new TestMethod()
       
    50         {
       
    51             public void run(TestCase tc)
       
    52             {
       
    53                 ((FinalizerTest)tc).testFinalizerManyClassesSingleObject();
       
    54             }
       
    55         }));
       
    56 
       
    57         suite.addTest(new FinalizerTest("testFinalizerManyClasses", new TestMethod()
       
    58         {
       
    59             public void run(TestCase tc)
       
    60             {
       
    61                 ((FinalizerTest)tc).testFinalizerManyClasses();
       
    62             }
       
    63         }));
       
    64 
       
    65         return suite;
       
    66     }
       
    67 
       
    68     public FinalizerTest()
       
    69     {
       
    70     }
       
    71 
       
    72     public FinalizerTest(String aTestName, TestMethod aTestMethod)
       
    73     {
       
    74         super(aTestName, aTestMethod);
       
    75     }
       
    76 
       
    77     // End j2meunit test framework setup
       
    78 
       
    79     protected void setUp()
       
    80     {
       
    81     }
       
    82 
       
    83     protected void tearDown()
       
    84     {
       
    85     }
       
    86 
       
    87     // Test cases
       
    88 
       
    89     private static class Test1 extends FinalizableBase
       
    90     {
       
    91     }
       
    92 
       
    93     private static class Test2 extends FinalizableBase
       
    94     {
       
    95     }
       
    96 
       
    97     private static class Test3 extends FinalizableBase
       
    98     {
       
    99     }
       
   100 
       
   101     private static class Test4 extends FinalizableBase
       
   102     {
       
   103     }
       
   104 
       
   105     private static class Test5 extends FinalizableBase
       
   106     {
       
   107     }
       
   108 
       
   109     private static class Test6 extends FinalizableBase
       
   110     {
       
   111     }
       
   112 
       
   113     private void testFinalizerSingleClass()
       
   114     {
       
   115         try
       
   116         {
       
   117             new Test1();
       
   118             System.gc();
       
   119             JvmInternal.runFinalization();
       
   120             StringBuffer sb = new StringBuffer();
       
   121             boolean ok = FinalizerStatistics.getNonfinalizedObjects(sb);
       
   122             assertTrue(sb.toString(), ok);
       
   123         }
       
   124         catch (Throwable t)
       
   125         {
       
   126             t.printStackTrace();
       
   127             assertTrue(t.toString(), false);
       
   128         }
       
   129     }
       
   130     private void testFinalizerManyClassesSingleObject()
       
   131     {
       
   132         try
       
   133         {
       
   134             new Test1();
       
   135             new Test2();
       
   136             new Test3();
       
   137             new Test4();
       
   138             new Test5();
       
   139             new Test6();
       
   140             System.gc();
       
   141             JvmInternal.runFinalization();
       
   142             StringBuffer sb = new StringBuffer();
       
   143             boolean ok = FinalizerStatistics.getNonfinalizedObjects(sb);
       
   144             assertTrue(sb.toString(), ok);
       
   145         }
       
   146         catch (Throwable t)
       
   147         {
       
   148             t.printStackTrace();
       
   149             assertTrue(t.toString(), false);
       
   150         }
       
   151     }
       
   152 
       
   153     private void testFinalizerManyClasses()
       
   154     {
       
   155         try
       
   156         {
       
   157             for (int i = 0; i < 10; ++i)
       
   158             {
       
   159                 new Test1();
       
   160                 new Test2();
       
   161                 new Test3();
       
   162                 new Test4();
       
   163                 new Test5();
       
   164                 new Test6();
       
   165             }
       
   166             System.gc();
       
   167             JvmInternal.runFinalization();
       
   168             StringBuffer sb = new StringBuffer();
       
   169             boolean ok = FinalizerStatistics.getNonfinalizedObjects(sb);
       
   170             assertTrue(sb.toString(), ok);
       
   171         }
       
   172         catch (Throwable t)
       
   173         {
       
   174             t.printStackTrace();
       
   175             assertTrue(t.toString(), false);
       
   176         }
       
   177     }
       
   178 
       
   179 }