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.test.legacy.soda.collections;
022:
023: import java.util.*;
024:
025: import com.db4o.query.*;
026: import com.db4o.test.legacy.soda.*;
027: import com.db4o.test.legacy.soda.collections.*;
028: import com.db4o.test.util.*;
029:
030: public class STOwnCollectionW implements STClass {
031:
032: public static transient SodaTest st;
033:
034: Collection col;
035:
036: public STOwnCollectionW() {
037:
038: }
039:
040: public STOwnCollectionW(Object[] arr) {
041: col = new MyCollection();
042: for (int i = 0; i < arr.length; i++) {
043: col.add(arr[i]);
044: }
045: }
046:
047: public Object[] store() {
048: return new Object[] {
049: new STOwnCollectionW(),
050: new STOwnCollectionW(new Object[0]),
051: new STOwnCollectionW(new Object[] { new Integer(0),
052: new Integer(0) }),
053: new STOwnCollectionW(new Object[] { new Integer(1),
054: new Integer(17),
055: new Integer(Integer.MAX_VALUE - 1) }),
056: new STOwnCollectionW(new Object[] { new Integer(3),
057: new Integer(17), new Integer(25),
058: new Integer(Integer.MAX_VALUE - 2) }),
059: new STOwnCollectionW(new Object[] { "foo",
060: new STElement("bar", "barbar") }),
061: new STOwnCollectionW(new Object[] { "foo2",
062: new STElement("bar", "barbar2") }) };
063: }
064:
065: public void testDefaultContainsInteger() {
066: Query q = st.query();
067: Object[] r = store();
068: q.constrain(new STOwnCollectionW(
069: new Object[] { new Integer(17) }));
070: st.expect(q, new Object[] { r[3], r[4] });
071: }
072:
073: public void testDefaultContainsString() {
074: Query q = st.query();
075: Object[] r = store();
076: q.constrain(new STOwnCollectionW(new Object[] { "foo" }));
077: st.expect(q, new Object[] { r[5] });
078: }
079:
080: public void testDefaultContainsTwo() {
081: Query q = st.query();
082: Object[] r = store();
083: q.constrain(new STOwnCollectionW(new Object[] {
084: new Integer(17), new Integer(25) }));
085: st.expect(q, new Object[] { r[4] });
086: }
087:
088: public void testDescendOne() {
089: Query q = st.query();
090: Object[] r = store();
091: q.constrain(STOwnCollectionW.class);
092: q.descend("col").constrain(new Integer(17));
093: st.expect(q, new Object[] { r[3], r[4] });
094: }
095:
096: public void testDescendTwo() {
097: Query q = st.query();
098: Object[] r = store();
099: q.constrain(STOwnCollectionW.class);
100: Query qElements = q.descend("col");
101: qElements.constrain(new Integer(17));
102: qElements.constrain(new Integer(25));
103: st.expect(q, new Object[] { r[4] });
104: }
105:
106: public void testDescendSmaller() {
107: Query q = st.query();
108: Object[] r = store();
109: q.constrain(STOwnCollectionW.class);
110: Query qElements = q.descend("col");
111: qElements.constrain(new Integer(3)).smaller();
112: st.expect(q, new Object[] { r[2], r[3] });
113: }
114:
115: public void testDefaultContainsObject() {
116: Query q = st.query();
117: Object[] r = store();
118: q.constrain(new STOwnCollectionW(new Object[] { new STElement(
119: "bar", null) }));
120: st.expect(q, new Object[] { r[5], r[6] });
121: }
122:
123: public void testDescendToObject() {
124: Query q = st.query();
125: Object[] r = store();
126: q.constrain(new STOwnCollectionW());
127: q.descend("col").descend("foo1").constrain("bar");
128: st.expect(q, new Object[] { r[5], r[6] });
129: }
130:
131: public static class MyCollection implements Collection {
132:
133: ArrayList myList;
134:
135: public MyCollection() {
136: myList = new ArrayList();
137: }
138:
139: public int size() {
140: return myList.size();
141: }
142:
143: public boolean isEmpty() {
144: return myList.isEmpty();
145: }
146:
147: public boolean contains(Object o) {
148: return myList.contains(o);
149: }
150:
151: public Iterator iterator() {
152: return myList.iterator();
153: }
154:
155: public Object[] toArray() {
156: return myList.toArray();
157: }
158:
159: public Object[] toArray(Object[] a) {
160: return myList.toArray(a);
161: }
162:
163: public boolean add(Object o) {
164: return myList.add(o);
165: }
166:
167: public boolean remove(Object o) {
168: return myList.remove(o);
169: }
170:
171: public boolean containsAll(Collection c) {
172: return myList.containsAll(c);
173: }
174:
175: public boolean addAll(Collection c) {
176: return myList.addAll(c);
177: }
178:
179: public boolean removeAll(Collection c) {
180: return myList.removeAll(c);
181: }
182:
183: public boolean retainAll(Collection c) {
184: return myList.retainAll(c);
185: }
186:
187: public void clear() {
188: myList.clear();
189: }
190:
191: public boolean equals(Object o) {
192: if (o instanceof MyCollection) {
193: return new TCompare().isEqual(myList,
194: ((MyCollection) o).myList);
195: }
196: return false;
197: }
198:
199: public int hashCode() {
200: return myList.hashCode();
201: }
202: }
203: }
|