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.generic;
022:
023: import java.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.ext.*;
027: import com.db4o.internal.*;
028: import com.db4o.query.*;
029: import com.db4o.reflect.*;
030: import com.db4o.reflect.generic.*;
031:
032: import db4ounit.*;
033: import db4ounit.extensions.*;
034: import db4ounit.extensions.fixtures.*;
035:
036: /**
037: *
038: * @author treeder, Andrew
039: *
040: */
041: public class GenericObjectsTest extends AbstractDb4oTestCase {
042: private String PERSON_CLASSNAME = "com.acme.Person";
043:
044: public static void main(String[] args) {
045: new TestRunner(new Db4oTestSuiteBuilder(new Db4oSolo(),
046: GenericObjectsTest.class)).run();
047: }
048:
049: public void testCreate() throws Exception {
050:
051: initGenericObjects();
052: // fixture().reopen();
053: ExtObjectContainer oc = fixture().db();
054: // now check to see if person was saved
055: ReflectClass rc = getReflectClass(oc, PERSON_CLASSNAME);
056: Assert.isNotNull(rc);
057: Query q = oc.query();
058: q.constrain(rc);
059: ObjectSet results = q.execute();
060: Assert.isTrue(results.size() == 1);
061: //Db4oUtil.dumpResults(fixture().db(), results);
062:
063: }
064:
065: private Object initGenericObjects() {
066: GenericClass personClass = initGenericClass();
067: ReflectField surname = personClass.getDeclaredField("surname");
068: ReflectField birthdate = personClass
069: .getDeclaredField("birthdate");
070: //ReflectField nArray = personClass.getDeclaredField("nArray");
071: Object person = personClass.newInstance();
072: surname.set(person, "John");
073: // /int[][] arrayData = new int[2][2];
074: // todo: FIXME: nArray doesn't work
075: // nArray.set(person, arrayData);
076: birthdate.set(person, new Date());
077: fixture().db().set(person);
078: fixture().db().commit();
079: return person;
080: }
081:
082: /**
083: * todo: Move the GenericClass creation into a utility/factory class.
084: * @return
085: */
086: public GenericClass initGenericClass() {
087: GenericReflector reflector = new GenericReflector(null,
088: Platform4.reflectorForType(GenericObjectsTest.class));
089: GenericClass _objectIClass = (GenericClass) reflector
090: .forClass(Object.class);
091: GenericClass result = new GenericClass(reflector, null,
092: PERSON_CLASSNAME, _objectIClass);
093: result.initFields(fields(result, reflector));
094: return result;
095: }
096:
097: private GenericField[] fields(GenericClass personClass,
098: GenericReflector reflector) {
099: return new GenericField[] {
100: new GenericField("surname", reflector
101: .forClass(String.class), false),
102: new GenericField("birthdate", reflector
103: .forClass(Date.class), false),
104: new GenericField("bestFriend", personClass, false),
105: new GenericField("nArray", reflector
106: .forClass(int[][].class), true) };
107: }
108:
109: public void testUpdate() {
110: ExtObjectContainer oc = fixture().db();
111: initGenericObjects();
112: //Db4oUtil.dump(oc);
113: ReflectClass rc = getReflectClass(oc, PERSON_CLASSNAME);
114: Assert.isNotNull(rc);
115: Query q = oc.query();
116: q.constrain(rc);
117: ObjectSet results = q.execute();
118: //Db4oUtil.dumpResults(oc, results);
119: Assert.isTrue(results.size() == 1);
120:
121: }
122:
123: public void testQuery() {
124: ExtObjectContainer oc = fixture().db();
125: initGenericObjects();
126: ReflectClass rc = getReflectClass(oc, PERSON_CLASSNAME);
127: Assert.isNotNull(rc);
128: // now query to make sure there are none left
129: Query q = oc.query();
130: q.constrain(rc);
131: q.descend("surname").constrain("John");
132: ObjectSet results = q.execute();
133: Assert.isTrue(results.size() == 1);
134: }
135:
136: public void testDelete() {
137: ExtObjectContainer oc = fixture().db();
138: initGenericObjects();
139: ReflectClass rc = getReflectClass(oc, PERSON_CLASSNAME);
140: Assert.isNotNull(rc);
141: Query q = oc.query();
142: q.constrain(rc);
143: ObjectSet results = q.execute();
144: while (results.hasNext()) {
145: Object o = results.next();
146: oc.delete(o);
147: }
148: oc.commit();
149:
150: // now query to make sure there are none left
151: q = oc.query();
152: q.constrain(rc);
153: q.descend("surname").constrain("John");
154: results = q.execute();
155: Assert.isTrue(results.size() == 0);
156: }
157:
158: private ReflectClass getReflectClass(ExtObjectContainer oc,
159: String className) {
160: // FIXME: If GenericReflector#knownClasses is not called, the test will
161: // fail.
162: /*ReflectClass[] classes = */oc.reflector().knownClasses();
163: return oc.reflector().forName(className);
164: }
165:
166: /**
167: * This is to ensure that reflector.forObject(GenericArray) returns an instance of GenericArrayClass instead of GenericClass
168: * http://tracker.db4o.com/jira/browse/COR-376
169: */
170: public void testGenericArrayClass() {
171: ExtObjectContainer oc = fixture().db();
172: initGenericObjects();
173: ReflectClass rc = oc.reflector().forName(PERSON_CLASSNAME);
174:
175: Object array = reflector().array().newInstance(rc, 5);
176:
177: ReflectClass arrayClass = oc.reflector().forObject(array);
178: Assert.isTrue(arrayClass.isArray());
179: Assert.isTrue(arrayClass instanceof GenericArrayClass);
180:
181: arrayClass = oc.reflector().forName(array.getClass().getName());
182: Assert.isTrue(arrayClass.isArray());
183: Assert.isTrue(arrayClass instanceof GenericArrayClass);
184:
185: arrayClass = oc.reflector().forClass(array.getClass());
186: Assert.isTrue(arrayClass.isArray());
187: Assert.isTrue(arrayClass instanceof GenericArrayClass);
188: }
189: }
|