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