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.reflect.*;
024: import com.db4o.reflect.generic.*;
025: import com.db4o.reflect.jdk.*;
026:
027: /**
028: * test for custom reflection implementations.
029: * <br><br>
030: * db4o internally uses java.lang.reflect.* by default. On platforms that
031: * do not support this package, customized implementations may be written
032: * to supply all the functionality of the interfaces in the com.db4o.reflect
033: * package. The sources in this sample packages demonstrate, how db4o
034: * accesses the java.lang.reflect.* functionality.
035: * <br><br>
036: * This TestReflect method may be used to test, if you own implementation
037: * provides the functionality that db4o needs. You may call the test from
038: * the command line by specifying the classname of your own class that
039: * implements IReflect. Alternatively you can call the test(IReflect) method.
040: */
041: public class Reflection extends Test {
042:
043: private final Reflector _reflector;
044: private final ReflectClass _classReflector;
045:
046: public Reflection() throws ClassNotFoundException {
047: this (new GenericReflector(null, new JdkReflector(Thread
048: .currentThread().getContextClassLoader())));
049: }
050:
051: public Reflection(Reflector reflector) {
052: _reflector = reflector;
053: _classReflector = _reflector.forName(TestReflectClass.class
054: .getName());
055: }
056:
057: public void testIClass() throws ClassNotFoundException {
058: ReflectField[] fields = _classReflector.getDeclaredFields();
059: _assert(fields.length == TestReflectClass.FIELD_COUNT,
060: "getDeclaredFields method failed.");
061: for (int i = 0; i < fields.length; i++) {
062: _assert(fields != null, "getDeclaredFields[" + i
063: + "] is valid");
064: String fieldName = fields[i].getName();
065: ReflectField fieldReflector = _classReflector
066: .getDeclaredField(fieldName);
067: _assert(fieldReflector != null, "getDeclaredField('"
068: + fieldName + "') is valid");
069: }
070:
071: tstIField();
072:
073: ReflectClass abstractReflector = _reflector
074: .forName(TestReflectAbstractClass.class.getName());
075: _assert(abstractReflector.isAbstract(), "isAbstract");
076: ReflectClass interfaceReflector = _reflector
077: .forName(TestReflectInterface.class.getName());
078: _assert(interfaceReflector.isInterface(), "isInterface");
079: Object instance = _classReflector.newInstance();
080: _assert(instance != null, "newInstance");
081:
082: }
083:
084: private void tstIField() {
085: tstIField1("myString", "HiBabe", String.class);
086: tstIField1("myInt", new Integer(10), int.class);
087: tstIField1("myTyped", new TestReflectClass(),
088: TestReflectClass.class);
089: tstIField1("myUntyped", "Foooo", Object.class);
090: tstIField1("myUntyped", new TestReflectClass(), Object.class);
091: _assert(
092: _classReflector.getDeclaredField("myStatic").isStatic(),
093: "IField.isStatic()");
094: _assert(_classReflector.getDeclaredField("myTransient")
095: .isTransient(), "IField.isTransient()");
096: }
097:
098: private void tstIField1(String fieldName, Object obj, Class clazz) {
099: ReflectClass claxx = _reflector.forClass(clazz);
100: String fieldMessage = TestReflectClass.class.getName() + ":"
101: + fieldName;
102: TestReflectClass onObject = new TestReflectClass();
103: ReflectField fieldReflector = _classReflector
104: .getDeclaredField(fieldName);
105: fieldReflector.set(onObject, obj);
106: Object got = fieldReflector.get(onObject);
107: _assert(got != null, fieldMessage + " IField.get returns NULL");
108: _assert(obj.equals(got), fieldMessage
109: + " IField.get returns strange Object");
110: _assert(fieldReflector.getName().equals(fieldName),
111: "IField.getName()");
112: _assert(fieldReflector.isPublic(), "IField.isPublic()");
113: _assert(!fieldReflector.isStatic(), "IField.isStatic()");
114: _assert(!fieldReflector.isTransient(), "IField.isTransient()");
115: _assert(fieldReflector.getFieldType().equals(claxx),
116: "IField.getType()");
117: }
118:
119: public void testIArray() {
120: tstIArray1(new Object[] { "", "hi", "Cool" });
121: tstIArray1(new Object[] { new Object(), new TestReflectClass(),
122: "Woooa", new Integer(3) });
123: tstIArray1(new Object[] { new TestReflectClass(),
124: new TestReflectClass() });
125: tstIArray2(new int[] { 1, 2, 3 });
126: tstIArray2(new long[] { 1L, 2L, 3L });
127: }
128:
129: public void tstEverything() throws ClassNotFoundException {
130: testIClass();
131: testIArray();
132: }
133:
134: private void tstIArray1(Object[] elements) {
135: ReflectArray array = _reflector.array();
136: ReflectClass clazz = _reflector.forObject(elements[0]);
137: Object obj = array.newInstance(clazz, 0);
138: _assert(obj != null, "Creation of zero length array");
139: _assert(array.getLength(obj) == 0, "Zero length array length");
140: obj = array.newInstance(clazz, elements.length);
141: _assert(obj != null, "Creation of variable length array");
142: _assert(array.getLength(obj) == elements.length,
143: "Variable length array length");
144: for (int i = 0; i < elements.length; i++) {
145: array.set(obj, i, elements[i]);
146: }
147: for (int i = 0; i < elements.length; i++) {
148: _assert(elements[i].equals(array.get(obj, i)),
149: "Array element comparison");
150: }
151: }
152:
153: private void tstIArray2(Object arr) {
154: ReflectArray array = _reflector.array();
155: Object element = array.get(arr, 0);
156: ReflectClass clazz = _reflector.forObject(element);
157: Object obj = array.newInstance(clazz, 0);
158: _assert(obj != null, "Creation of zero length array");
159: _assert(array.getLength(obj) == 0, "Zero length array length");
160: int length = array.getLength(arr);
161: obj = array.newInstance(clazz, length);
162: _assert(obj != null, "Creation of variable length array");
163: _assert(array.getLength(obj) == length,
164: "Variable length array length");
165: for (int i = 0; i < length; i++) {
166: array.set(obj, i, array.get(arr, i));
167: }
168: for (int i = 0; i < length; i++) {
169: _assert(array.get(arr, i).equals(array.get(obj, i)),
170: "Array element comparison");
171: }
172: }
173: }
|