1 16 package org.apache.commons.collections.bag; 17 18 import java.util.Set ; 19 20 import junit.framework.Test; 21 import junit.framework.TestSuite; 22 23 import org.apache.commons.collections.Bag; 24 25 34 public class TestTypedBag extends AbstractTestBag { 35 36 public TestTypedBag(String testName) { 37 super(testName); 38 } 39 40 public static Test suite() { 41 return new TestSuite(TestTypedBag.class); 42 } 43 44 public static void main(String args[]) { 45 String [] testCaseName = { TestTypedBag.class.getName()}; 46 junit.textui.TestRunner.main(testCaseName); 47 } 48 49 51 protected Class stringClass = this.getName().getClass(); 52 private Object obj = new Object (); 53 protected Class objectClass = obj.getClass(); 54 55 protected Bag decorateBag(HashBag bag, Class claz) { 56 return TypedBag.decorate(bag, claz); 57 } 58 59 public Bag makeBag() { 60 return decorateBag(new HashBag(), objectClass); 61 } 62 63 protected Bag makeTestBag() { 64 return decorateBag(new HashBag(), stringClass); 65 } 66 67 69 public void testlegalAddRemove() { 70 Bag bag = makeTestBag(); 71 assertEquals(0, bag.size()); 72 Object [] els = new Object [] {"1", "3", "5", "7", "2", "4", "1"}; 73 for (int i = 0; i < els.length; i++) { 74 bag.add(els[i]); 75 assertEquals(i + 1, bag.size()); 76 assertEquals(true, bag.contains(els[i])); 77 } 78 Set set = ((PredicatedBag) bag).uniqueSet(); 79 assertTrue("Unique set contains the first element",set.contains(els[0])); 80 assertEquals(true, bag.remove(els[0])); 81 set = ((PredicatedBag) bag).uniqueSet(); 82 assertTrue("Unique set now does not contain the first element", 83 !set.contains(els[0])); 84 } 85 86 public void testIllegalAdd() { 87 Bag bag = makeTestBag(); 88 Integer i = new Integer (3); 89 try { 90 bag.add(i); 91 fail("Integer should fail type check."); 92 } catch (IllegalArgumentException e) { 93 } 95 assertTrue("Collection shouldn't contain illegal element", 96 !bag.contains(i)); 97 } 98 99 public void testIllegalDecorate() { 100 HashBag elements = new HashBag(); 101 elements.add("one"); 102 elements.add("two"); 103 elements.add(new Integer (3)); 104 elements.add("four"); 105 try { 106 Bag bag = decorateBag(elements, stringClass); 107 fail("Bag contains an element that should fail the type test."); 108 } catch (IllegalArgumentException e) { 109 } 111 try { 112 Bag bag = decorateBag(new HashBag(), null); 113 fail("Expectiing IllegalArgumentException for null predicate."); 114 } catch (IllegalArgumentException e) { 115 } 117 } 118 119 protected boolean skipSerializedCanonicalTests() { 120 return true; 121 } 122 123 } 124 | Popular Tags |