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.jdk5;
022:
023: import java.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.query.*;
027: import com.db4o.test.*;
028:
029: public class Jdk5EnumTest {
030: private final static int NUMRUNS = 1;
031:
032: public void testSingleStoreRetrieve() {
033: ObjectContainer db = reopen();
034:
035: // We make sure the Jdk5Enum class is already loaded, otherwise
036: // we may get the side effect that storing it will load the class
037: // and overwrite our changes exactly when we store them.
038: db.set(Jdk5Enum.A);
039:
040: Jdk5Data<String> data = new Jdk5Data<String>("Test", Jdk5Enum.A);
041: Jdk5Enum.A.reset();
042: Test.ensure(Jdk5Enum.A.getCount() == 0);
043: Jdk5Enum.A.incCount();
044:
045: // The Jdk5Enum object may already be stored on the server, so we
046: // can't persist by reachability. We have to store the object
047: // explicitely.
048: db.set(Jdk5Enum.A);
049:
050: Test.ensure(Jdk5Enum.A.getCount() == 1);
051: Test.ensure(Jdk5Enum.B.getCount() == 0);
052: Test.ensure(data.getType() == Jdk5Enum.A);
053: Test.ensure(data.getSize() == 0);
054: Test.ensure(data.getMax() == Integer.MIN_VALUE);
055: data.add(2, 4, 6, 1, 3, 5);
056: Test.ensure(data.getSize() == 6);
057: Test.ensure(data.getMax() == 6);
058: Test.ensure(Jdk5Data.class
059: .isAnnotationPresent(Jdk5Annotation.class));
060:
061: db.set(data);
062: db = reopen();
063: data = null;
064:
065: Query query = db.query();
066: query.constrain(Jdk5Data.class);
067: Query sub = query.descend("type");
068: sub.constrain(Jdk5Enum.class);
069: sub.constrain(Jdk5Enum.A);
070: sub.descend("type").constrain("A");
071: sub.descend("count").constrain(Integer.valueOf(1));
072:
073: ObjectSet result = query.execute();
074: Test.ensure(result.size() == 1);
075: data = (Jdk5Data<String>) result.next();
076: Test.ensure(data.getItem().equals("Test"));
077: Test.ensure(Jdk5Enum.A == data.getType());
078: Test.ensure(Jdk5Enum.A.name().equals(data.getType().name()));
079: Test.ensure(data.getSize() == 6);
080: Test.ensure(data.getMax() == 6);
081: Test.ensure(result.size() == 1);
082:
083: ensureEnumInstancesInDB(db);
084:
085: Test.deleteAllInstances(data);
086: }
087:
088: public void testMultipleStoreRetrieve() {
089: ObjectContainer db = reopen();
090: for (int i = 0; i < NUMRUNS; i++) {
091: Jdk5Data<Integer> data = new Jdk5Data<Integer>(Integer
092: .valueOf(i), nthEnum(i));
093: db.set(data);
094: }
095:
096: db = reopen();
097: ObjectSet result = db.get(Jdk5Data.class);
098: Test.ensure(result.size() == NUMRUNS);
099: Comparator<Jdk5Data<Integer>> comp = new Comparator<Jdk5Data<Integer>>() {
100: public int compare(Jdk5Data<Integer> d1,
101: Jdk5Data<Integer> d2) {
102: return d1.getItem().compareTo(d2.getItem());
103: }
104: };
105: SortedSet<Jdk5Data<Integer>> sorted = new TreeSet<Jdk5Data<Integer>>(
106: comp);
107: while (result.hasNext()) {
108: sorted.add((Jdk5Data<Integer>) result.next());
109: }
110: int count = 0;
111: for (Jdk5Data<Integer> data : sorted) {
112: Test.ensure(data.getItem().intValue() == count);
113: Test.ensure(data.getType() == nthEnum(count));
114: count++;
115: }
116: ensureEnumInstancesInDB(db);
117:
118: Test.deleteAllInstances(Jdk5Data.class);
119: }
120:
121: public void testEnumsInCollections() {
122: final boolean withDb4oCollections = true;
123:
124: ObjectContainer db = reopen();
125:
126: class CollectionHolder {
127: public List<Jdk5Enum> list;
128: public List<Jdk5Enum> db4olist;
129: public Set<Jdk5Enum> set;
130: public Map<Jdk5Enum, String> keymap;
131: public Map<String, Jdk5Enum> valmap;
132: public Map<Jdk5Enum, String> db4okeymap;
133: public Map<String, Jdk5Enum> db4ovalmap;
134: public Jdk5Enum[] array;
135: }
136:
137: CollectionHolder holder = new CollectionHolder();
138: holder.list = new ArrayList<Jdk5Enum>(NUMRUNS);
139: Comparator<Jdk5Enum> comp = new Comparator<Jdk5Enum>() {
140: public int compare(Jdk5Enum e1, Jdk5Enum e2) {
141: return e1.name().compareTo(e2.name());
142: }
143: };
144: holder.set = new TreeSet<Jdk5Enum>(comp);
145: holder.keymap = new HashMap<Jdk5Enum, String>(NUMRUNS);
146: holder.valmap = new HashMap<String, Jdk5Enum>(NUMRUNS);
147: holder.array = new Jdk5Enum[NUMRUNS];
148: holder.db4olist = db.ext().collections().newLinkedList();
149: holder.db4okeymap = db.ext().collections().newHashMap(2);
150: holder.db4ovalmap = db.ext().collections().newHashMap(2);
151: for (int i = 0; i < NUMRUNS; i++) {
152: Jdk5Enum curenum = nthEnum(i);
153: holder.list.add(curenum);
154: if (withDb4oCollections) {
155: holder.db4olist.add(curenum);
156: }
157: holder.array[i] = curenum;
158: }
159: holder.set.add(Jdk5Enum.A);
160: holder.set.add(Jdk5Enum.B);
161: holder.keymap.put(Jdk5Enum.A, Jdk5Enum.A.name());
162: holder.keymap.put(Jdk5Enum.B, Jdk5Enum.B.name());
163: holder.valmap.put(Jdk5Enum.A.name(), Jdk5Enum.A);
164: holder.valmap.put(Jdk5Enum.B.name(), Jdk5Enum.B);
165: if (withDb4oCollections) {
166: holder.db4okeymap.put(Jdk5Enum.A, Jdk5Enum.A.name());
167: holder.db4okeymap.put(Jdk5Enum.B, Jdk5Enum.B.name());
168: holder.db4ovalmap.put(Jdk5Enum.A.name(), Jdk5Enum.A);
169: holder.db4ovalmap.put(Jdk5Enum.B.name(), Jdk5Enum.B);
170: }
171: db.set(holder);
172:
173: db = reopen();
174: ObjectSet result = db.get(CollectionHolder.class);
175: Test.ensure(result.size() == 1);
176: holder = (CollectionHolder) result.next();
177:
178: Test.ensure(holder.list.size() == NUMRUNS);
179: Test.ensure(holder.set.size() == 2);
180: Test.ensure(holder.keymap.size() == 2);
181: Test.ensure(holder.valmap.size() == 2);
182: Test.ensure(holder.array.length == NUMRUNS);
183: if (withDb4oCollections) {
184: Test.ensure(holder.db4olist.size() == NUMRUNS);
185: Test.ensure(holder.db4okeymap.size() == 2);
186: Test.ensure(holder.db4ovalmap.size() == 2);
187: }
188: ensureEnumInstancesInDB(db);
189:
190: Test.deleteAllInstances(CollectionHolder.class);
191: }
192:
193: private ObjectContainer reopen() {
194: Test.reOpen();
195: return Test.objectContainer();
196: }
197:
198: private void ensureEnumInstancesInDB(ObjectContainer db) {
199: Query query;
200: ObjectSet result;
201: query = db.query();
202: query.constrain(Jdk5Enum.class);
203: result = query.execute();
204: // We should have all enum members once in the database, since they're
205: // statically referenced by the Enum subclass.
206: if (result.size() != 2) {
207: System.err.println("# instances in db: " + result.size());
208: while (result.hasNext()) {
209: Jdk5Enum curenum = (Jdk5Enum) result.next();
210: long id = db.ext().getID(curenum);
211: System.err.println(curenum + " : ihc "
212: + System.identityHashCode(curenum) + " : id "
213: + id);
214: }
215:
216: }
217: Test.ensure(result.size() == 2);
218: }
219:
220: private Jdk5Enum nthEnum(int n) {
221: return (n % 2 == 0 ? Jdk5Enum.A : Jdk5Enum.B);
222: }
223: }
|