KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > reflect > GRHierarchy


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.test.reflect;
22
23 import java.util.*;
24
25 import com.db4o.*;
26 import com.db4o.query.*;
27 import com.db4o.reflect.*;
28 import com.db4o.reflect.generic.*;
29 import com.db4o.reflect.jdk.*;
30 import com.db4o.test.*;
31 import com.db4o.test.Test;
32 import com.db4o.test.util.*;
33
34 // TODO: Works for solo mode only currently
35
public class GRHierarchy {
36     public static abstract class A {
37         private int id;
38
39         public A(int id) {
40             this.id = id;
41         }
42     }
43
44     public static abstract class B {
45         private String JavaDoc name;
46         
47         public B(String JavaDoc name) {
48             this.name = name;
49         }
50     }
51
52     public static class A1 extends A {
53         private Character JavaDoc ch;
54
55         public A1(int id, Character JavaDoc ch) {
56             super(id);
57             this.ch = ch;
58         }
59     }
60
61     public static class B1 extends B {
62         private A a;
63
64         public B1(String JavaDoc name, A a) {
65             super(name);
66             this.a = a;
67         }
68     }
69
70     public void store() {
71         if(Test.clientServer) {
72             return;
73         }
74         A a = new A1(42, new Character JavaDoc('x'));
75         B b = new B1("test", a);
76         Db4o.configure().reflectWith(new JdkReflector(getClass().getClassLoader()));
77         com.db4o.test.Test.reOpenServer();
78         com.db4o.test.Test.reOpen();
79         com.db4o.test.Test.store(b);
80     }
81
82     public void test() {
83         if(Test.clientServer) {
84             return;
85         }
86         Vector excluded=new Vector();
87         excluded.add(A.class.getName());
88         excluded.add(B.class.getName());
89         excluded.add(A1.class.getName());
90         excluded.add(B1.class.getName());
91         ExcludingClassLoader loader=new ExcludingClassLoader(getClass().getClassLoader(),excluded);
92         Db4o.configure().reflectWith(new JdkReflector(loader));
93         com.db4o.test.Test.reOpenServer();
94         com.db4o.test.Test.reOpen();
95         
96         com.db4o.test.Test.objectContainer().storedClasses();
97         
98         GenericReflector reflector = com.db4o.test.Test.objectContainer().ext().reflector();
99         ReflectClass proto=reflector.forName(B.class.getName());
100         
101         ReflectClass protoSuper = proto.getSuperclass();
102         Test.ensure(protoSuper!=null);
103         Test.ensureEquals(Object JavaDoc.class.getName(), protoSuper.getName());
104         
105         Query query=com.db4o.test.Test.query();
106         query.constrain(proto);
107         ObjectSet result=query.execute();
108         com.db4o.test.Test.ensureEquals(1,result.size());
109         Object JavaDoc obj=result.next();
110         com.db4o.test.Test.ensure(obj instanceof GenericObject);
111         
112         ReflectClass clazz=reflector.forObject(obj);
113         com.db4o.test.Test.ensure(clazz instanceof GenericClass);
114         com.db4o.test.Test.ensureEquals(B1.class.getName(),clazz.getName());
115         ReflectClass superclazz=clazz.getSuperclass();
116         com.db4o.test.Test.ensure(superclazz instanceof GenericClass);
117         com.db4o.test.Test.ensureEquals(B.class.getName(),superclazz.getName());
118         
119         ReflectField[] subfields=clazz.getDeclaredFields();
120         com.db4o.test.Test.ensureEquals(1,subfields.length);
121         com.db4o.test.Test.ensureEquals("a",subfields[0].getName());
122         ReflectClass fieldtype=reflector.forName(A.class.getName());
123         com.db4o.test.Test.ensureEquals(fieldtype,subfields[0].getFieldType());
124         Object JavaDoc subfieldvalue=subfields[0].get(obj);
125         com.db4o.test.Test.ensure(subfieldvalue instanceof GenericObject);
126         ReflectClass concretetype=reflector.forObject(subfieldvalue);
127         com.db4o.test.Test.ensure(concretetype instanceof GenericClass);
128         com.db4o.test.Test.ensureEquals(A1.class.getName(),concretetype.getName());
129         
130         ReflectField[] superfields=superclazz.getDeclaredFields();
131         com.db4o.test.Test.ensureEquals(1,superfields.length);
132         com.db4o.test.Test.ensureEquals("name",superfields[0].getName());
133         fieldtype=reflector.forName(String JavaDoc.class.getName());
134         com.db4o.test.Test.ensureEquals(fieldtype,superfields[0].getFieldType());
135         Object JavaDoc superfieldvalue=superfields[0].get(obj);
136         com.db4o.test.Test.ensureEquals("test",superfieldvalue);
137         
138         Db4o.configure().reflectWith(new JdkReflector(getClass().getClassLoader()));
139     }
140     
141     public static void main(String JavaDoc[] args) {
142         AllTests.run(GRHierarchy.class);
143     }
144 }
145
Popular Tags