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 java.util.*;
024:
025: import com.db4o.reflect.*;
026: import com.db4o.reflect.generic.*;
027: import com.db4o.reflect.jdk.*;
028:
029: public class GenericObjects extends Test {
030:
031: private GenericReflector _reflector;
032: private final GenericClass _objectIClass;
033:
034: private GenericClass _iClass;
035:
036: public GenericObjects() throws ClassNotFoundException {
037: _reflector = new GenericReflector(null, new JdkReflector(Thread
038: .currentThread().getContextClassLoader()));
039: _objectIClass = (GenericClass) _reflector
040: .forClass(Object.class);
041: }
042:
043: public void test() throws ClassNotFoundException {
044: _reflector.register(acmeDataClass());
045: _iClass = (GenericClass) _reflector.forName("com.acme.Person");
046: _assert(_iClass.getName().equals("com.acme.Person"));
047: _assert(_iClass.getSuperclass() == _objectIClass);
048:
049: _assert(_iClass.isAssignableFrom(subclass()));
050: _assert(!_iClass.isAssignableFrom(otherDataClass()));
051: _assert(!_iClass.isAssignableFrom(_objectIClass));
052:
053: _assert(_iClass.isInstance(_iClass.newInstance()));
054: _assert(_iClass.isInstance(subclass().newInstance()));
055: _assert(!_iClass.isInstance(otherDataClass().newInstance()));
056: _assert(!_iClass.isInstance("whatever"));
057:
058: _assert(_reflector.forObject(_iClass.newInstance()) == _iClass);
059:
060: tstFields();
061: tstReflectionDelegation();
062:
063: }
064:
065: private void tstReflectionDelegation()
066: throws ClassNotFoundException {
067: Reflection test = new Reflection(new GenericReflector(null,
068: new JdkReflector(Thread.currentThread()
069: .getContextClassLoader())));
070: test.tstEverything();
071: }
072:
073: private GenericClass otherDataClass() {
074: return new GenericClass(_reflector, null, "anyName",
075: _objectIClass);
076: }
077:
078: private GenericClass subclass() {
079: return new GenericClass(_reflector, null, "anyName", _iClass);
080: }
081:
082: private void tstFields() {
083: ReflectField surname = _iClass.getDeclaredField("surname");
084: ReflectField birthdate = _iClass.getDeclaredField("birthdate");
085: ReflectField[] fields = _iClass.getDeclaredFields();
086: _assert(fields.length == 3);
087: _assert(fields[0] == surname);
088: _assert(fields[1] == birthdate);
089:
090: Object person = _iClass.newInstance();
091: _assert(birthdate.get(person) == null);
092: surname.set(person, "Cleese");
093: _assert(surname.get(person).equals("Cleese"));
094: }
095:
096: private GenericClass acmeDataClass() {
097: GenericClass result = new GenericClass(_reflector, null,
098: "com.acme.Person", _objectIClass);
099: result.initFields(fields(result));
100: return result;
101: }
102:
103: private GenericField[] fields(ReflectClass personClass) {
104: return new GenericField[] {
105: new GenericField("surname", _reflector
106: .forClass(String.class), false),
107: new GenericField("birthdate", _reflector
108: .forClass(Date.class), false),
109: new GenericField("bestFriend", personClass, false) };
110: }
111:
112: }
|