1 21 package com.db4o.db4ounit.common.foundation; 22 23 import com.db4o.foundation.*; 24 25 import db4ounit.*; 26 27 public class Collection4TestCase implements TestCase { 28 29 public static void main(String [] args) { 30 new TestRunner(Collection4TestCase.class).run(); 31 } 32 33 public void testNulls(){ 34 final Collection4 c = new Collection4(); 35 c.add("one"); 36 assertNotContainsNull(c); 37 c.add(null); 38 assertContainsNull(c); 39 assertCollection(new String [] { "one", null }, c); 40 c.prepend(null); 41 assertCollection(new String [] { null, "one", null }, c); 42 c.prepend("zero"); 43 c.add("two"); 44 assertCollection(new String [] {"zero", null, "one", null , "two"}, c); 45 assertContainsNull(c); 46 c.remove(null); 47 assertCollection(new String [] { "zero", "one", null , "two"}, c); 48 c.remove(null); 49 assertNotContainsNull(c); 50 assertCollection(new String [] { "zero", "one", "two"}, c); 51 c.remove(null); 52 assertCollection(new String [] { "zero", "one", "two"}, c); 53 } 54 55 public void testPrepend() { 56 final Collection4 c = new Collection4(); 57 c.prepend("foo"); 58 assertCollection(new String [] { "foo" }, c); 59 c.add("bar"); 60 assertCollection(new String [] { "foo", "bar" }, c); 61 c.prepend("baz"); 62 assertCollection(new String [] { "baz", "foo", "bar" }, c); 63 c.prepend("gazonk"); 64 assertCollection(new String [] { "gazonk", "baz", "foo", "bar" }, c); 65 } 66 67 public void testCopyConstructor() { 68 final String [] expected = new String [] { "1", "2", "3" }; 69 final Collection4 c = newCollection(expected); 70 assertCollection(expected, new Collection4(c)); 71 } 72 73 public void testInvalidIteratorException() { 74 final Collection4 c = newCollection(new String [] { "1", "2" }); 75 final Iterator4 i = c.iterator(); 76 Assert.isTrue(i.moveNext()); 77 c.add("3"); 78 Assert.expect(InvalidIteratorException.class, new CodeBlock() { 79 public void run() throws Exception { 80 System.out.println(i.current()); 81 } 82 }); 83 } 84 85 public void testRemove() { 86 final Collection4 c = newCollection(new String [] { "1", "2", "3", "4" }); 87 c.remove("3"); 88 assertCollection(new String [] { "1", "2", "4"} , c); 89 c.remove("4"); 90 assertCollection(new String [] { "1", "2" } , c); 91 c.add("5"); 92 assertCollection(new String [] { "1", "2", "5" } , c); 93 c.remove("1"); 94 assertCollection(new String [] { "2", "5" } , c); 95 c.remove("2"); 96 c.remove("5"); 97 assertCollection(new String [] {}, c); 98 c.add("6"); 99 assertCollection(new String [] { "6" }, c); 100 } 101 102 private void assertCollection(String [] expected, Collection4 c) { 103 Assert.areEqual(expected.length, c.size()); 104 assertIterator(expected, c.iterator()); 105 } 106 107 private void assertContainsNull(Collection4 c) { 108 Assert.isTrue(c.contains(null)); 109 Assert.isNull(c.get(null)); 110 int size = c.size(); 111 c.ensure(null); 112 Assert.areEqual(size, c.size()); 113 } 114 115 private void assertNotContainsNull(Collection4 c) { 116 Assert.isFalse(c.contains(null)); 117 Assert.isNull(c.get(null)); 118 int size = c.size(); 119 c.ensure(null); 120 Assert.areEqual(size + 1, c.size()); 121 c.remove(null); 122 Assert.areEqual(size, c.size()); 123 } 124 125 public void testIterator() { 126 String [] expected = new String [] { "1", "2", "3" }; 127 Collection4 c = newCollection(expected); 128 assertIterator(expected, c.iterator()); 129 } 130 131 private Collection4 newCollection(String [] expected) { 132 Collection4 c = new Collection4(); 133 c.addAll(expected); 134 return c; 135 } 136 137 private void assertIterator(String [] expected, Iterator4 iterator) { 138 Assert.isNotNull(iterator); 139 140 for (int i=0; i<expected.length; ++i) { 141 Assert.isTrue(iterator.moveNext()); 142 Assert.areEqual(expected[i], iterator.current()); 143 } 144 Assert.isFalse(iterator.moveNext()); 145 } 146 147 public void testToString() { 148 Collection4 c = new Collection4(); 149 Assert.areEqual("[]", c.toString()); 150 151 c.add("foo"); 152 Assert.areEqual("[foo]", c.toString()); 153 c.add("bar"); 154 Assert.areEqual("[foo, bar]", c.toString()); 155 } 156 } 157 | Popular Tags |