FileDocCategorySizeDatePackage
TestWeakHashSet.javaAPI DocExample13479Sun Dec 14 22:47:38 GMT 2003oreilly.hcj.references._test

TestWeakHashSet

public class TestWeakHashSet extends TestCase
Test case for the WeakHashSet class.
author
Robert Simmons jr.
version
$Revision: 1.1 $
see
mirror.utilities

Fields Summary
private static final Logger
LOGGER
Holds the logger instance for the class.
private static final String
CONTAINS_ALL
Assert string constant.
private static final String
SIZE
Assert string constant.
private static final String
CONTAINS
Assert string constant.
private static final String
IS_EMPTY
Assert string constant.
private static final String
ADD_ALL
Assert string constant.
private static final String
RETAIN_ALL
Assert string constant.
private static final String
REMOVE_ALL
Assert string constant.
private static final String
A
Testing instance.
private static final String
B
Testing instance.
private static final String
C
Testing instance.
private static final String
D
Testing instance.
private static final String
E
Testing instance.
private static final String
F
Testing instance.
private static final String
G
Testing instance.
private static final Set
TEST_SET
Testing instance.
Constructors Summary
public TestWeakHashSet(String name)
Creates a new instance of this test case.

param
name The name of the test case.


	 
		TEST_SET = new HashSet();
		TEST_SET.add(A);
		TEST_SET.add(B);
		TEST_SET.add(C);
		TEST_SET.add(D);
		TEST_SET.add(E);
		TEST_SET.add(F);
		TEST_SET.add(G);
	
		super(name);
	
Methods Summary
public static voidmain(java.lang.String[] args)
Run all the test using a text based Test runner..

param
args Arguments (ignored).

		TestRunner.run(suite());
	
public static junit.framework.Testsuite()
Gets the suite for this test case. {@inheritDoc}

return
The test suite.

		TestSuite suite = new TestSuite();
		suite.setName("WeakHashSet");  //$NON-NLS-1$
		suite.addTest(new TestWeakHashSet("testGeneralFunctionality"));  //$NON-NLS-1$
		suite.addTest(new TestWeakHashSet("testSetOperations"));  //$NON-NLS-1$
		suite.addTest(new TestWeakHashSet("testToArray"));  //$NON-NLS-1$
		suite.addTest(new TestWeakHashSet("testWeakRefCleanup"));  //$NON-NLS-1$
		suite.addTest(new TestWeakHashSet("testIterators"));  //$NON-NLS-1$
		suite.addTest(new TestWeakHashSet("testStrongConversion"));  //$NON-NLS-1$
		return suite;
	
public voidtestGeneralFunctionality()
Tests general WeakHashSet functionality.
Validates:
  • The method isEmpty() returns the correct result.
  • The method contains(Object) returns the correct result.
  • Objects can be inserted into the set.
  • The method size() returns the correct size after insertion.
  • Objects can be removed from the set.
  • The set can be cleared.

		WeakHashSet whs = new WeakHashSet();

		// *** isEmpty()
		assertEquals(IS_EMPTY, true, whs.isEmpty());

		// *** add(Object);
		whs.add(A);

		// *** isEmpty()
		assertEquals(IS_EMPTY, false, whs.isEmpty());

		// *** size()
		assertEquals(SIZE, 1, whs.size());

		// *** contains(Object)
		assertEquals(CONTAINS, true, whs.contains(A));
		whs.add(B);
		whs.add(C);
		assertEquals(SIZE, 3, whs.size());
		assertEquals(CONTAINS, true, whs.contains(C));
		whs.remove(C);
		assertEquals(CONTAINS, false, whs.contains(C));
		assertEquals(SIZE, 2, whs.size());

		// *** clear()
		whs.clear();
		assertEquals(IS_EMPTY, true, whs.isEmpty());
		assertEquals(CONTAINS, false, whs.contains(B));
		assertEquals(SIZE, 0, whs.size());

		// *** add(null) Which should exception.
		boolean exed = false;
		try {
			whs.add(null);
		} catch (NullPointerException ex) {
			exed = true;
		}
		assertEquals("Exception Wanted Test: ", true, exed);  //$NON-NLS-1$
		exed = false;
	
public voidtestIterators()
Tests WeakHashSet iterator.
Validates:
  • Iterators can iterate through the set.
  • Iterators correctly handle removed references.

		try {
			String x = new String("Object x");  //$NON-NLS-1$
			String y = new String("Object y");  //$NON-NLS-1$
			String z = new String("Object z");  //$NON-NLS-1$
			ReferenceQueue queue = new ReferenceQueue();
			new WeakReference(x, queue);
			new WeakReference(y, queue);
			new WeakReference(z, queue);
			// *** add everything to the WeakHashSet
			WeakHashSet whs = new WeakHashSet();
			whs.addAll(TEST_SET);
			whs.add(x);
			whs.add(y);
			whs.add(z);

			// *** y should still be in the set as p.
			assertEquals(CONTAINS_ALL, true, whs.containsAll(TEST_SET));
			assertEquals(CONTAINS, true, whs.contains(x));
			assertEquals(CONTAINS, true, whs.contains(y));
			assertEquals(CONTAINS, true, whs.contains(z));
			assertEquals(SIZE, (TEST_SET.size() + 3), whs.size());
			// *** get an iterator
			Iterator iter = whs.iterator();

			// *** nuke the three weak objects.
			x = null;
			y = null;
			z = null;
			System.gc();
			while (queue.remove(1000) != null) {
			}

			// *** Iterate and make sure all memeber are in TEST_SET. (x, y, z skipped)
			Object obj = null;
			while (iter.hasNext()) {
				obj = iter.next();
				assertEquals(CONTAINS_ALL, true, TEST_SET.contains(obj));
			}
		} catch (Exception ex) {
			LOGGER.error(ex.getMessage(), ex);
			fail(ex.getMessage());
		}
	
public voidtestSetOperations()
Tests WeakHashSet set operations functionality.
Validates:
  • The method addAll(Collection) works properly.
  • The method containsAll(Collection) works properly.
  • The method retainAll(Collection) works properly.
  • The method removeAll(Collection) works properly.

		WeakHashSet whs = new WeakHashSet();
		HashSet hs = new HashSet();
		Set st = new TreeSet();
		List lst = new LinkedList();

		// *** containsAll(Collection)
		whs.add(A);
		whs.add(B);
		whs.add(C);
		whs.add(D);
		st.add(A);
		st.add(B);
		assertEquals(CONTAINS_ALL, true, whs.containsAll(st));
		st.add(C);
		assertEquals(CONTAINS_ALL, true, whs.containsAll(st));
		st.add(D);
		st.add(E);
		assertEquals(CONTAINS_ALL, false, whs.containsAll(st));
		assertEquals(SIZE, 4, whs.size());

		// *** addAll(Collection)
		hs.add(C);
		assertEquals(ADD_ALL, false, whs.addAll(hs));
		hs.add(E);
		hs.add(F);
		assertEquals(ADD_ALL, true, whs.addAll(hs));
		assertEquals(ADD_ALL, false, whs.addAll(hs));
		hs.add(G);
		assertEquals(ADD_ALL, true, whs.addAll(hs));
		assertEquals(SIZE, 7, whs.size());
		assertEquals(CONTAINS_ALL, true, whs.containsAll(st));
		assertEquals(CONTAINS_ALL, true, whs.containsAll(hs));

		// *** removeAll(Collection)    
		assertEquals(REMOVE_ALL, true, whs.removeAll(hs));
		assertEquals(REMOVE_ALL, false, whs.removeAll(hs));
		assertEquals(CONTAINS_ALL, false, whs.containsAll(hs));

		// *** retainAll(Collection)        
		lst.add(A);
		lst.add(B);
		assertEquals(RETAIN_ALL, true, whs.retainAll(lst));
		assertEquals(RETAIN_ALL, false, whs.retainAll(lst));
		assertEquals(CONTAINS_ALL, true, whs.containsAll(lst));
		assertEquals(CONTAINS_ALL, false, whs.containsAll(st));
		assertEquals(SIZE, lst.size(), whs.size());
	
public voidtestStrongConversion()
Test conversion from weak hash set to normal hash set.

		WeakHashSet weak = new WeakHashSet(TEST_SET);
		Set set = new HashSet(weak);
		Set lset = new LinkedHashSet(weak);
		assertTrue("Testing conversion to HashSet: ", set.equals(weak));  //$NON-NLS-1$
		assertTrue("Testing conversion to HashSet: ", lset.equals(weak));  //$NON-NLS-1$
	
public voidtestToArray()
Tests WeakHashSet array conversion functionality.
Validates:
  • The method toArray() works properly.
  • The method toArray(Object[]) works properly.

		WeakHashSet whs = new WeakHashSet();
		whs.addAll(TEST_SET);
		// *** toArray()    
		Object[] strArrayOne = whs.toArray();
		assertEquals("Testing toArray(): ", true,  //$NON-NLS-1$
		             Arrays.asList(strArrayOne).containsAll(TEST_SET));
		// *** toArray(Object[])    
		String[] strArrayTwo = new String[1];
		strArrayTwo = (String[])whs.toArray(strArrayTwo);
		assertEquals("Testing toArray(): ", true,  //$NON-NLS-1$
		             Arrays.asList(strArrayTwo).containsAll(TEST_SET));
	
public voidtestWeakRefCleanup()
Tests Removal of references when object is garbage collected.
Validates:
  • The method size() returns the correct size after insertion.
  • Objects can be inserted into the set.
  • The weak referenced object should vanish from the set.
  • The objects that have strong references should still be in the set.

		try {
			String x = new String("Object x");  //$NON-NLS-1$
			String y = new String("Object y");  //$NON-NLS-1$
			String z = new String("Object z");  //$NON-NLS-1$
			ReferenceQueue queue = new ReferenceQueue();
			new WeakReference(x, queue);
			new WeakReference(y, queue);
			new WeakReference(z, queue);
			// *** add everything to the WeakHashSet
			WeakHashSet whs = new WeakHashSet();
			whs.addAll(TEST_SET);
			whs.add(x);
			whs.add(y);
			whs.add(z);

			// *** validate everything in the WeakHashSet    
			assertEquals(CONTAINS_ALL, true, whs.containsAll(TEST_SET));
			assertEquals(CONTAINS, true, whs.contains(x));
			assertEquals(CONTAINS, true, whs.contains(y));
			assertEquals(CONTAINS, true, whs.contains(z));

			// *** nuke x from the vm. 
			x = null;
			System.gc();
			while (queue.remove(1000) != null) {
			}

			// *** make sure x isnt in the set anymore but all others are.
			assertEquals(CONTAINS_ALL, true, whs.containsAll(TEST_SET));
			assertEquals(CONTAINS, true, whs.contains(y));
			assertEquals(CONTAINS, true, whs.contains(z));
			assertEquals(SIZE, (TEST_SET.size() + 2), whs.size());
			// *** build a double link, p, to y and nuke z.
			String p = y;
			z = null;
			System.gc();
			while (queue.remove(1000) != null) {
			}

			// *** make sure x, and z isnt in the set anymore but all others are.
			assertEquals(CONTAINS_ALL, true, whs.containsAll(TEST_SET));
			assertEquals(CONTAINS, true, whs.contains(y));
			assertEquals(SIZE, (TEST_SET.size() + 1), whs.size());

			// *** nuke y but leave p intact.
			y = null;
			System.gc();
			while (queue.remove(1000) != null) {
			}

			// *** y should still be in the set as p.
			assertEquals(CONTAINS_ALL, true, whs.containsAll(TEST_SET));
			assertEquals(CONTAINS, true, whs.contains(p));
			assertEquals(SIZE, (TEST_SET.size() + 1), whs.size());

			// *** finally nuke p.
			p = null;
			System.gc();
			while (queue.remove(1000) != null) {
			}

			// *** y should still be in the set as p.
			assertEquals(CONTAINS_ALL, true, whs.containsAll(TEST_SET));
			assertEquals(SIZE, (TEST_SET.size()), whs.size());
		} catch (Exception ex) {
			LOGGER.error(ex.getMessage(), ex);
			fail(ex.getMessage());
		}