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.jre12.collections;
022:
023: import java.util.*;
024:
025: import com.db4o.internal.collections.*;
026:
027: import db4ounit.*;
028: import db4ounit.extensions.*;
029:
030: public class PersistentListTestCase extends AbstractDb4oTestCase {
031:
032: public static void main(String[] args) {
033: new PersistentListTestCase().runSolo();
034: }
035:
036: private final PersistentList[] lists = new PersistentList[] { new MockPersistentList(), };
037:
038: private Vector testData() {
039: Vector vector = new Vector();
040: vector.add("zero");
041: vector.add("one");
042: vector.add("two");
043: return vector;
044: }
045:
046: public void testAdd() {
047: for (int i = 0; i < lists.length; i++) {
048: PersistentList list = lists[i];
049: Vector data = testData();
050: addData(list, data);
051: assertAreEqual(data, list);
052: data.add("one");
053: list.add("one");
054: assertAreEqual(data, list);
055: }
056: }
057:
058: public void testClear() {
059: for (int i = 0; i < lists.length; i++) {
060: PersistentList list = lists[i];
061: list.clear();
062: Assert.areEqual(0, list.size());
063: addData(list, testData());
064: list.clear();
065: Assert.areEqual(0, list.size());
066: }
067: }
068:
069: public void testRemove() {
070: for (int i = 0; i < lists.length; i++) {
071: PersistentList list = lists[i];
072: Vector data = testData();
073: addData(list, data);
074:
075: Object item = data.get(1);
076: list.remove(item);
077: data.remove(item);
078: assertAreEqual(data, list);
079:
080: for (int j = 0; j < data.size(); j++) {
081: item = data.get(0);
082: list.remove(item);
083: data.remove(item);
084: }
085: assertAreEqual(data, list);
086: }
087: }
088:
089: public void testSet() {
090: for (int i = 0; i < lists.length; i++) {
091: PersistentList list = lists[i];
092: Vector data = testData();
093: addData(list, data);
094:
095: int size = data.size();
096: for (int j = 0; j < size; j++) {
097: data.set(j, new Integer(j));
098: list.set(j, new Integer(j));
099: }
100:
101: assertAreEqual(data, list);
102: }
103: }
104:
105: public void testAddAtIndex() {
106: for (int i = 0; i < lists.length; i++) {
107: PersistentList list = lists[i];
108: Vector data = testData();
109: addData(list, data);
110:
111: int size = data.size();
112: for (int j = 0; j < size; j++) {
113: data.add(j + 2, new Integer(j));
114: list.add(j + 2, new Integer(j));
115: }
116:
117: assertAreEqual(data, list);
118: }
119: }
120:
121: public void testRemoveAtIndex() {
122: for (int i = 0; i < lists.length; i++) {
123: PersistentList list = lists[i];
124:
125: Vector data = testData();
126: addData(list, data);
127:
128: int size = data.size();
129: for (int j = 0; j < size; j++) {
130: data.remove(0);
131: list.remove(0);
132: }
133: assertAreEqual(data, list);
134:
135: data = testData();
136: addData(list, data);
137:
138: for (int j = 0; j < size; j++) {
139: int pos = data.size() - 1;
140: data.remove(pos);
141: list.remove(pos);
142: }
143: assertAreEqual(data, list);
144:
145: data = testData();
146: addData(list, data);
147:
148: for (int j = 0; j < size - 2; j++) {
149: data.remove(1);
150: list.remove(1);
151: }
152: assertAreEqual(data, list);
153:
154: }
155: }
156:
157: private void addData(PersistentList list, Vector data) {
158: for (int j = 0; j < data.size(); j++) {
159: list.add(data.get(j));
160: }
161: }
162:
163: private void assertAreEqual(Vector vector, PersistentList list) {
164: Assert.areEqual(vector.size(), list.size());
165: ArrayAssert.areEqual(vector.toArray(), list.toArray());
166: }
167:
168: }
|