001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.foundation;
022:
023: import com.db4o.foundation.*;
024:
025: import db4ounit.*;
026:
027: public class Collection4TestCase implements TestCase {
028:
029: public static void main(String[] args) {
030: new TestRunner(Collection4TestCase.class).run();
031: }
032:
033: public void testReplace() {
034: final Collection4 c = new Collection4();
035: c.replace("one", "two");
036: c.add("one");
037: c.add("two");
038: c.add("three");
039: c.replace("two", "two.half");
040: assertCollection(new String[] { "one", "two.half", "three" }, c);
041: c.replace("two.half", "one");
042: c.replace("one", "half");
043: assertCollection(new String[] { "half", "one", "three" }, c);
044: }
045:
046: public void testNulls() {
047: final Collection4 c = new Collection4();
048: c.add("one");
049: assertNotContainsNull(c);
050: c.add(null);
051: assertContainsNull(c);
052: assertCollection(new String[] { "one", null }, c);
053: c.prepend(null);
054: assertCollection(new String[] { null, "one", null }, c);
055: c.prepend("zero");
056: c.add("two");
057: assertCollection(new String[] { "zero", null, "one", null,
058: "two" }, c);
059: assertContainsNull(c);
060: c.remove(null);
061: assertCollection(new String[] { "zero", "one", null, "two" }, c);
062: c.remove(null);
063: assertNotContainsNull(c);
064: assertCollection(new String[] { "zero", "one", "two" }, c);
065: c.remove(null);
066: assertCollection(new String[] { "zero", "one", "two" }, c);
067: }
068:
069: public void testPrepend() {
070: final Collection4 c = new Collection4();
071: c.prepend("foo");
072: assertCollection(new String[] { "foo" }, c);
073: c.add("bar");
074: assertCollection(new String[] { "foo", "bar" }, c);
075: c.prepend("baz");
076: assertCollection(new String[] { "baz", "foo", "bar" }, c);
077: c.prepend("gazonk");
078: assertCollection(
079: new String[] { "gazonk", "baz", "foo", "bar" }, c);
080: }
081:
082: public void testCopyConstructor() {
083: final String[] expected = new String[] { "1", "2", "3" };
084: final Collection4 c = newCollection(expected);
085: assertCollection(expected, new Collection4(c));
086: }
087:
088: public void testInvalidIteratorException() {
089: final Collection4 c = newCollection(new String[] { "1", "2" });
090: final Iterator4 i = c.iterator();
091: Assert.isTrue(i.moveNext());
092: c.add("3");
093: Assert.expect(InvalidIteratorException.class, new CodeBlock() {
094: public void run() throws Throwable {
095: System.out.println(i.current());
096: }
097: });
098: }
099:
100: public void testRemove() {
101: final Collection4 c = newCollection(new String[] { "1", "2",
102: "3", "4" });
103: c.remove("3");
104: assertCollection(new String[] { "1", "2", "4" }, c);
105: c.remove("4");
106: assertCollection(new String[] { "1", "2" }, c);
107: c.add("5");
108: assertCollection(new String[] { "1", "2", "5" }, c);
109: c.remove("1");
110: assertCollection(new String[] { "2", "5" }, c);
111: c.remove("2");
112: c.remove("5");
113: assertCollection(new String[] {}, c);
114: c.add("6");
115: assertCollection(new String[] { "6" }, c);
116: }
117:
118: private void assertCollection(String[] expected, Collection4 c) {
119: Assert.areEqual(expected.length, c.size());
120: Iterator4Assert.areEqual(expected, c.iterator());
121: }
122:
123: private void assertContainsNull(Collection4 c) {
124: Assert.isTrue(c.contains(null));
125: Assert.isNull(c.get(null));
126: int size = c.size();
127: c.ensure(null);
128: Assert.areEqual(size, c.size());
129: }
130:
131: private void assertNotContainsNull(Collection4 c) {
132: Assert.isFalse(c.contains(null));
133: Assert.isNull(c.get(null));
134: int size = c.size();
135: c.ensure(null);
136: Assert.areEqual(size + 1, c.size());
137: c.remove(null);
138: Assert.areEqual(size, c.size());
139: }
140:
141: public void testIterator() {
142: String[] expected = new String[] { "1", "2", "3" };
143: Collection4 c = newCollection(expected);
144: Iterator4Assert.areEqual(expected, c.iterator());
145: }
146:
147: private Collection4 newCollection(String[] expected) {
148: Collection4 c = new Collection4();
149: c.addAll(expected);
150: return c;
151: }
152:
153: public void testToString() {
154: Collection4 c = new Collection4();
155: Assert.areEqual("[]", c.toString());
156:
157: c.add("foo");
158: Assert.areEqual("[foo]", c.toString());
159: c.add("bar");
160: Assert.areEqual("[foo, bar]", c.toString());
161: }
162: }
|