KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > foundation > Collection4TestCase


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

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 JavaDoc[] 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 JavaDoc[] { "one", null }, c);
40         c.prepend(null);
41         assertCollection(new String JavaDoc[] { null, "one", null }, c);
42         c.prepend("zero");
43         c.add("two");
44         assertCollection(new String JavaDoc[] {"zero", null, "one", null , "two"}, c);
45         assertContainsNull(c);
46         c.remove(null);
47         assertCollection(new String JavaDoc[] { "zero", "one", null , "two"}, c);
48         c.remove(null);
49         assertNotContainsNull(c);
50         assertCollection(new String JavaDoc[] { "zero", "one", "two"}, c);
51         c.remove(null);
52         assertCollection(new String JavaDoc[] { "zero", "one", "two"}, c);
53     }
54     
55     public void testPrepend() {
56         final Collection4 c = new Collection4();
57         c.prepend("foo");
58         assertCollection(new String JavaDoc[] { "foo" }, c);
59         c.add("bar");
60         assertCollection(new String JavaDoc[] { "foo", "bar" }, c);
61         c.prepend("baz");
62         assertCollection(new String JavaDoc[] { "baz", "foo", "bar" }, c);
63         c.prepend("gazonk");
64         assertCollection(new String JavaDoc[] { "gazonk", "baz", "foo", "bar" }, c);
65     }
66     
67     public void testCopyConstructor() {
68         final String JavaDoc[] expected = new String JavaDoc[] { "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 JavaDoc[] { "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 JavaDoc {
80                 System.out.println(i.current());
81             }
82         });
83     }
84     
85     public void testRemove() {
86         final Collection4 c = newCollection(new String JavaDoc[] { "1", "2", "3", "4" });
87         c.remove("3");
88         assertCollection(new String JavaDoc[] { "1", "2", "4"} , c);
89         c.remove("4");
90         assertCollection(new String JavaDoc[] { "1", "2" } , c);
91         c.add("5");
92         assertCollection(new String JavaDoc[] { "1", "2", "5" } , c);
93         c.remove("1");
94         assertCollection(new String JavaDoc[] { "2", "5" } , c);
95         c.remove("2");
96         c.remove("5");
97         assertCollection(new String JavaDoc[] {}, c);
98         c.add("6");
99         assertCollection(new String JavaDoc[] { "6" }, c);
100     }
101     
102     private void assertCollection(String JavaDoc[] 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 JavaDoc[] expected = new String JavaDoc[] { "1", "2", "3" };
127         Collection4 c = newCollection(expected);
128         assertIterator(expected, c.iterator());
129     }
130     
131     private Collection4 newCollection(String JavaDoc[] expected) {
132         Collection4 c = new Collection4();
133         c.addAll(expected);
134         return c;
135     }
136
137     private void assertIterator(String JavaDoc[] 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