1   /*
2    * @(#) EventIteratorTest.java Sep 1, 2003 Copyright (c) 2002-2005 Delft
3    * University of Technology Jaffalaan 5, 2628 BX Delft, the Netherlands. All
4    * rights reserved. This software is proprietary information of Delft University
5    * of Technology The code is published under the Lesser General Public License
6    */
7   package nl.tudelft.simulation.event.ref;
8   
9   import java.rmi.MarshalledObject;
10  
11  import junit.framework.Assert;
12  import junit.framework.TestCase;
13  
14  /***
15   * The test script for the reference package. All classes in this package are
16   * tested with this test
17   * <p>
18   * (c) copyright 2002-2005-2004 <a href="http://www.simulation.tudelft.nl">Delft
19   * University of Technology </a>, the Netherlands. <br>
20   * See for project information <a
21   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
22   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
23   * General Public License (LGPL) </a>, no warranty.
24   * 
25   * @author <a href="http://www.peter-jacobs.com">Peter Jacobs </a>
26   * @version $Revision: 1.8 $ $Date: 2005/07/04 12:23:02 $
27   * @since 1.5
28   */
29  public class EventRefTest extends TestCase
30  {
31      /*** TEST_METHOD is the name of the test method */
32      public static final String TEST_METHOD = "test";
33  
34      /***
35       * constructs a new EventIteratorTest.
36       */
37      public EventRefTest()
38      {
39          this(TEST_METHOD);
40      }
41  
42      /***
43       * constructs a new EventIteratorTest
44       * 
45       * @param method the name of the test method
46       */
47      public EventRefTest(final String method)
48      {
49          super(method);
50      }
51  
52      /***
53       * tests the classes in the reference class.
54       */
55      public void test()
56      {
57          try
58          {
59              // Test 1: We since we have a pointer to referent, gc should not
60              // clean the weakReference
61  
62              Object referent = new String("EventIteratorTest");
63              /*
64               * It is absolutely amazing what you see if you replace the above
65               * with the following: Object referent = "EventIteratorTest";
66               */
67  
68              Reference<Object> reference = new WeakReference<Object>(referent);
69              Assert.assertEquals(reference.get(), referent);
70              Runtime.getRuntime().gc();
71              Assert.assertNotNull(reference.get());
72  
73              // Test 2: We since we have a pointer to referent, gc should
74              // clean the weakReference
75              reference = new WeakReference<Object>(new String(
76                      "EventIteratorTest"));
77              Runtime.getRuntime().gc();
78              Assert.assertNull(reference.get());
79  
80              // Test 3: The strong reference...
81              reference = new StrongReference<Object>(new String(
82                      "EventIteratorTest"));
83              Assert.assertNotNull(reference.get());
84              Runtime.getRuntime().gc();
85              Assert.assertNotNull(reference.get());
86  
87              // A Strong one
88              new MarshalledObject(new StrongReference<Object>(new Double(12)))
89                      .get();
90  
91              // A Weak one
92              new MarshalledObject(new WeakReference<Object>(new Double(12)))
93                      .get();
94          } catch (Throwable throwable)
95          {
96              // runtime exceptions are not appreciated
97              throwable.printStackTrace();
98              Assert.fail();
99          }
100     }
101 }