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.jre5.enums;
022:
023: import java.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.query.Query;
027:
028: import db4ounit.Assert;
029: import db4ounit.extensions.AbstractDb4oTestCase;
030:
031: public class EnumTestCase extends AbstractDb4oTestCase {
032: private final static int NUMRUNS = 1;
033:
034: @SuppressWarnings("unchecked")
035: public void testSingleStoreRetrieve() throws Exception {
036: // We make sure the Jdk5Enum class is already loaded, otherwise
037: // we may get the side effect that storing it will load the class
038: // and overwrite our changes exactly when we store them.
039: db().set(TypeCountEnum.A);
040:
041: EnumHolder data = new EnumHolder(TypeCountEnum.A);
042: TypeCountEnum.A.reset();
043: Assert.areEqual(0, TypeCountEnum.A.getCount());
044: TypeCountEnum.A.incCount();
045:
046: // The Jdk5Enum object may already be stored on the server, so we
047: // can't persist by reachability. We have to store the object
048: // explicitely.
049: db().set(TypeCountEnum.A);
050:
051: Assert.areEqual(1, TypeCountEnum.A.getCount());
052: Assert.areEqual(0, TypeCountEnum.B.getCount());
053: Assert.areEqual(TypeCountEnum.A, data.getType());
054:
055: db().set(data);
056: reopen();
057: data = null;
058:
059: Query query = db().query();
060: query.constrain(EnumHolder.class);
061: Query sub = query.descend("type");
062: sub.constrain(TypeCountEnum.class);
063: sub.constrain(TypeCountEnum.A);
064: sub.descend("type").constrain("A");
065: sub.descend("count").constrain(Integer.valueOf(1));
066:
067: ObjectSet<EnumHolder> result = (ObjectSet<EnumHolder>) query
068: .execute();
069: Assert.areEqual(1, result.size());
070: data = (EnumHolder) result.next();
071: Assert.areEqual(data.getType(), TypeCountEnum.A);
072: Assert.areEqual(TypeCountEnum.A.name(), data.getType().name());
073: Assert.areEqual(1, result.size());
074:
075: ensureEnumInstancesInDB(db());
076: }
077:
078: private static class TypeCountEnumComparator implements
079: Comparator<TypeCountEnum> {
080: public int compare(TypeCountEnum e1, TypeCountEnum e2) {
081: return e1.name().compareTo(e2.name());
082: }
083: }
084:
085: private static class CollectionHolder {
086: public List<TypeCountEnum> list;
087: public List<TypeCountEnum> db4olist;
088: public Set<TypeCountEnum> set;
089: public Map<TypeCountEnum, String> keymap;
090: public Map<String, TypeCountEnum> valmap;
091: public Map<TypeCountEnum, String> db4okeymap;
092: public Map<String, TypeCountEnum> db4ovalmap;
093: public TypeCountEnum[] array;
094: }
095:
096: @SuppressWarnings("unchecked")
097: public void testEnumsInCollections() throws Exception {
098: final boolean withDb4oCollections = true;
099:
100: CollectionHolder holder = new CollectionHolder();
101: holder.list = new ArrayList<TypeCountEnum>(NUMRUNS);
102: Comparator<TypeCountEnum> comp = new TypeCountEnumComparator();
103: holder.set = new TreeSet<TypeCountEnum>(comp);
104: holder.keymap = new HashMap<TypeCountEnum, String>(NUMRUNS);
105: holder.valmap = new HashMap<String, TypeCountEnum>(NUMRUNS);
106: holder.array = new TypeCountEnum[NUMRUNS];
107: holder.db4olist = db().ext().collections().newLinkedList();
108: holder.db4okeymap = db().ext().collections().newHashMap(2);
109: holder.db4ovalmap = db().ext().collections().newHashMap(2);
110: for (int i = 0; i < NUMRUNS; i++) {
111: TypeCountEnum curenum = nthEnum(i);
112: holder.list.add(curenum);
113: if (withDb4oCollections) {
114: holder.db4olist.add(curenum);
115: }
116: holder.array[i] = curenum;
117: }
118: holder.set.add(TypeCountEnum.A);
119: holder.set.add(TypeCountEnum.B);
120: holder.keymap.put(TypeCountEnum.A, TypeCountEnum.A.name());
121: holder.keymap.put(TypeCountEnum.B, TypeCountEnum.B.name());
122: holder.valmap.put(TypeCountEnum.A.name(), TypeCountEnum.A);
123: holder.valmap.put(TypeCountEnum.B.name(), TypeCountEnum.B);
124: if (withDb4oCollections) {
125: holder.db4okeymap.put(TypeCountEnum.A, TypeCountEnum.A
126: .name());
127: holder.db4okeymap.put(TypeCountEnum.B, TypeCountEnum.B
128: .name());
129: holder.db4ovalmap.put(TypeCountEnum.A.name(),
130: TypeCountEnum.A);
131: holder.db4ovalmap.put(TypeCountEnum.B.name(),
132: TypeCountEnum.B);
133: }
134: db().set(holder);
135:
136: reopen();
137: ObjectSet result = db().get(CollectionHolder.class);
138: Assert.areEqual(1, result.size());
139: holder = (CollectionHolder) result.next();
140:
141: Assert.areEqual(NUMRUNS, holder.list.size());
142: Assert.areEqual(2, holder.set.size());
143: Assert.areEqual(2, holder.keymap.size());
144: Assert.areEqual(2, holder.valmap.size());
145: Assert.areEqual(NUMRUNS, holder.array.length);
146: if (withDb4oCollections) {
147: Assert.areEqual(NUMRUNS, holder.db4olist.size());
148: Assert.areEqual(2, holder.db4okeymap.size());
149: Assert.areEqual(2, holder.db4ovalmap.size());
150: }
151: ensureEnumInstancesInDB(db());
152: }
153:
154: @SuppressWarnings("unchecked")
155: private void ensureEnumInstancesInDB(ObjectContainer db) {
156: Query query;
157: ObjectSet<TypeCountEnum> result;
158: query = db.query();
159: query.constrain(TypeCountEnum.class);
160: result = (ObjectSet<TypeCountEnum>) query.execute();
161: // We should have all enum members once in the database, since they're
162: // statically referenced by the Enum subclass.
163: if (result.size() != 2) {
164: System.err.println("# instances in db: " + result.size());
165: while (result.hasNext()) {
166: TypeCountEnum curenum = (TypeCountEnum) result.next();
167: long id = db.ext().getID(curenum);
168: System.err.println(curenum + " : ihc "
169: + System.identityHashCode(curenum) + " : id "
170: + id);
171: }
172:
173: }
174: Assert.areEqual(2, result.size());
175: }
176:
177: private TypeCountEnum nthEnum(int n) {
178: return (n % 2 == 0 ? TypeCountEnum.A : TypeCountEnum.B);
179: }
180: }
|