/** * (c) Piet Jonas, 2001 * @author Piet Jonas * @version 1.0 */ import java.util.*; import typesafecollections.*; /** * Implements tests for the type-safe collection classes from the * typesafecollections package. */ public class TestTypeSafeCollections { /** * Empty test class, implemented as inner class. */ private static class Test { } public static void main(String[] args) { //Test TypeSafeCollection Collection c = new ArrayList(); Collection tsc = TypeSafeCollections.typeSafeCollection(c, Test.class); tsc.add(new Test()); try { tsc.add(""); } catch (ClassCastException e) { System.out.println("Expected Exception (Collection.add) : " + e); } System.out.println("TypeSafeCollection test: OK"); tsc.add(new Test() {}); System.out.println("TypeSafeCollection inheritance test: OK"); c.clear(); tsc = TypeSafeCollections.typeSafeCollection(c, Set.class); tsc.add(new HashSet()); try { tsc.add(new ArrayList()); } catch (ClassCastException e) { System.out.println("Expected Exception (Collection.add) : " + e); } System.out.println("TypeSafeCollection test with interfaces: OK"); //Test TypeSafeList List l = new ArrayList(); List tsl = TypeSafeCollections.typeSafeList(l, Test.class); ListIterator i = tsl.listIterator(); i.add(new Test()); try { i.add("test"); } catch (ClassCastException e) { System.out.println("Expected Exception (ListIterator.add) : " + e); } System.out.println("TypeSafeList ListIterator test : OK"); tsl = tsl.subList(0, 0); tsl.add(new Test() {}); try { tsl.add("test"); } catch (ClassCastException e) { System.out.println("Expected Exception (List.subList) : " + e); } System.out.println("TypeSafeList sub List test : OK"); //Test TypeSafeSortedSet SortedSet ss = new TreeSet(); SortedSet tsss = TypeSafeCollections.typeSafeSortedSet(ss, String.class); tsss.add("a"); tsss.add("b"); tsss.add("d"); tsss = tsss.tailSet("b"); tsss.add("c"); try { tsss.add(new Integer(5)); } catch (ClassCastException e) { System.out.println("Expected Exception (SortedSet.tailSet) : " + e); } System.out.println("TypeSafeSortedSet sub Set test : OK"); //Test TypeSafeMap Map map = new HashMap(); Map tsm = TypeSafeCollections.typeSafeMap(map, String.class, Integer.class); tsm.put("first", new Integer(1)); tsm.put("second", new Integer(2)); try { tsm.put("third", new Double(3)); } catch (ClassCastException e) { System.out.println("Expected Exception (Map value) : " + e); } try { tsm.put(new Integer(3), new Integer(3)); } catch (ClassCastException e) { System.out.println("Expected Exception (Map key) : " + e); } System.out.println("TypeSafeMap test : OK"); Set entries = tsm.entrySet(); Iterator it = entries.iterator(); if (it.hasNext()) { Map.Entry entry = (Map.Entry)it.next(); entry.setValue(new Integer(4)); try { entry.setValue(new Double(3)); } catch (ClassCastException e) { System.out.println("Expected Exception (Map.Entry value) : " + e); } } System.out.println("TypeSafeMap Map.Entry test : OK"); } }