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.reflect;
022:
023: import com.db4o.*;
024: import com.db4o.foundation.*;
025: import com.db4o.query.*;
026: import com.db4o.reflect.*;
027: import com.db4o.reflect.generic.*;
028: import com.db4o.reflect.jdk.*;
029: import com.db4o.test.*;
030: import com.db4o.test.Test;
031: import com.db4o.test.util.*;
032:
033: // TODO: Works for solo mode only currently
034: public class GRHierarchy {
035: public static abstract class A {
036: private int id;
037:
038: public A(int id) {
039: this .id = id;
040: }
041: }
042:
043: public static abstract class B {
044: private String name;
045:
046: public B(String name) {
047: this .name = name;
048: }
049: }
050:
051: public static class A1 extends A {
052: private Character ch;
053:
054: public A1(int id, Character ch) {
055: super (id);
056: this .ch = ch;
057: }
058: }
059:
060: public static class B1 extends B {
061: private A a;
062:
063: public B1(String name, A a) {
064: super (name);
065: this .a = a;
066: }
067: }
068:
069: public void store() {
070: if (Test.clientServer) {
071: return;
072: }
073: A a = new A1(42, new Character('x'));
074: B b = new B1("test", a);
075: Db4o.configure().reflectWith(
076: new JdkReflector(getClass().getClassLoader()));
077: com.db4o.test.Test.reOpenServer();
078: com.db4o.test.Test.reOpen();
079: com.db4o.test.Test.store(b);
080: }
081:
082: public void test() {
083: if (Test.clientServer) {
084: return;
085: }
086: Collection4 excluded = new Collection4();
087: excluded.add(A.class.getName());
088: excluded.add(B.class.getName());
089: excluded.add(A1.class.getName());
090: excluded.add(B1.class.getName());
091: ExcludingClassLoader loader = new ExcludingClassLoader(
092: getClass().getClassLoader(), excluded);
093: Db4o.configure().reflectWith(new JdkReflector(loader));
094: com.db4o.test.Test.reOpenServer();
095: com.db4o.test.Test.reOpen();
096:
097: com.db4o.test.Test.objectContainer().storedClasses();
098:
099: GenericReflector reflector = com.db4o.test.Test
100: .objectContainer().ext().reflector();
101: ReflectClass proto = reflector.forName(B.class.getName());
102:
103: ReflectClass protoSuper = proto.getSuperclass();
104: Test.ensure(protoSuper != null);
105: Test.ensureEquals(Object.class.getName(), protoSuper.getName());
106:
107: Query query = com.db4o.test.Test.query();
108: query.constrain(proto);
109: ObjectSet result = query.execute();
110: com.db4o.test.Test.ensureEquals(1, result.size());
111: Object obj = result.next();
112: com.db4o.test.Test.ensure(obj instanceof GenericObject);
113:
114: ReflectClass clazz = reflector.forObject(obj);
115: com.db4o.test.Test.ensure(clazz instanceof GenericClass);
116: com.db4o.test.Test.ensureEquals(B1.class.getName(), clazz
117: .getName());
118: ReflectClass super clazz = clazz.getSuperclass();
119: com.db4o.test.Test.ensure(super clazz instanceof GenericClass);
120: com.db4o.test.Test.ensureEquals(B.class.getName(), super clazz
121: .getName());
122:
123: ReflectField[] subfields = clazz.getDeclaredFields();
124: com.db4o.test.Test.ensureEquals(1, subfields.length);
125: com.db4o.test.Test.ensureEquals("a", subfields[0].getName());
126: ReflectClass fieldtype = reflector.forName(A.class.getName());
127: com.db4o.test.Test.ensureEquals(fieldtype, subfields[0]
128: .getFieldType());
129: Object subfieldvalue = subfields[0].get(obj);
130: com.db4o.test.Test
131: .ensure(subfieldvalue instanceof GenericObject);
132: ReflectClass concretetype = reflector.forObject(subfieldvalue);
133: com.db4o.test.Test.ensure(concretetype instanceof GenericClass);
134: com.db4o.test.Test.ensureEquals(A1.class.getName(),
135: concretetype.getName());
136:
137: ReflectField[] super fields = super clazz.getDeclaredFields();
138: com.db4o.test.Test.ensureEquals(1, super fields.length);
139: com.db4o.test.Test.ensureEquals("name", super fields[0]
140: .getName());
141: fieldtype = reflector.forName(String.class.getName());
142: com.db4o.test.Test.ensureEquals(fieldtype, super fields[0]
143: .getFieldType());
144: Object super fieldvalue = super fields[0].get(obj);
145: com.db4o.test.Test.ensureEquals("test", super fieldvalue);
146:
147: Db4o.configure().reflectWith(
148: new JdkReflector(getClass().getClassLoader()));
149: }
150:
151: public static void main(String[] args) {
152: AllTests.run(GRHierarchy.class);
153: }
154: }
|