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.db4ounit.common.internal;
022:
023: import com.db4o.config.*;
024: import com.db4o.ext.*;
025: import com.db4o.foundation.*;
026: import com.db4o.internal.*;
027: import com.db4o.reflect.*;
028:
029: import db4ounit.*;
030: import db4ounit.extensions.*;
031:
032: public class StoredClassTestCase extends AbstractDb4oTestCase {
033:
034: private static final String FIELD_NAME = "_name";
035: private static final String ITEM_NAME = "item";
036:
037: public static class ItemParent {
038: public String[] _array;
039: }
040:
041: public static class Item extends ItemParent {
042:
043: public String _name;
044:
045: public Item(String name) {
046: _name = name;
047: }
048: }
049:
050: private long _id;
051:
052: protected void configure(Configuration config) throws Exception {
053: config.objectClass(Item.class).objectField(FIELD_NAME).indexed(
054: true);
055: }
056:
057: protected void store() throws Exception {
058: Item item = new Item(ITEM_NAME);
059: store(item);
060: }
061:
062: protected void db4oSetupAfterStore() throws Exception {
063: _id = db().getID(retrieveOnlyInstance(Item.class));
064: }
065:
066: public void testUnknownStoredClass() {
067: Assert.isNull(storedClass(this .getClass()));
068: }
069:
070: public void testStoredClassImpl() {
071: Assert.isInstanceOf(StoredClassImpl.class, itemStoredClass());
072: }
073:
074: public void testGetIds() {
075: StoredClass itemClass = itemStoredClass();
076: long[] ids = itemClass.getIDs();
077: Assert.areEqual(1, ids.length);
078: Assert.areEqual(_id, ids[0]);
079: }
080:
081: public void testGetName() {
082: StoredClass itemClass = itemStoredClass();
083: Assert.areEqual(reflector().forClass(Item.class).getName(),
084: itemClass.getName());
085: }
086:
087: public void testGetParentStoredClass() {
088: StoredClass itemClass = itemStoredClass();
089: StoredClass parentStoredClass = itemClass
090: .getParentStoredClass();
091: Assert.areEqual(reflector().forClass(ItemParent.class)
092: .getName(), parentStoredClass.getName());
093: Assert.areEqual(parentStoredClass, db().storedClass(
094: ItemParent.class));
095: }
096:
097: public void testGetStoredFields() {
098: assertStoredField(Item.class, FIELD_NAME, ITEM_NAME,
099: String.class, true, false);
100: assertStoredField(ItemParent.class, "_array", null,
101: String.class, false, true);
102:
103: StoredClass itemStoredClass = itemStoredClass();
104: StoredField storedField = itemStoredClass.storedField(
105: FIELD_NAME, null);
106: StoredField sameStoredField = itemStoredClass.getStoredFields()[0];
107: StoredField otherStoredField = storedClass(ItemParent.class)
108: .getStoredFields()[0];
109: Assert.equalsAndHashcode(storedField, sameStoredField,
110: otherStoredField);
111:
112: Assert.isNull(itemStoredClass.storedField("", null));
113: }
114:
115: private void assertStoredField(Class objectClass, String fieldName,
116: final Object expectedFieldValue, Class expectedFieldType,
117: boolean hasIndex, boolean isArray) {
118: StoredClass storedClass = storedClass(objectClass);
119: StoredField[] storedFields = storedClass.getStoredFields();
120: Assert.areEqual(1, storedFields.length);
121: final StoredField storedField = storedFields[0];
122: Assert.areEqual(fieldName, storedField.getName());
123: StoredField storedFieldByName = storedClass.storedField(
124: fieldName, expectedFieldType);
125: Assert.areEqual(storedField, storedFieldByName);
126:
127: Object item = retrieveOnlyInstance(objectClass);
128: Assert.areEqual(expectedFieldValue, storedField.get(item));
129: ReflectClass fieldType = storedField.getStoredType();
130: Assert.areEqual(reflector().forClass(expectedFieldType),
131: fieldType);
132: Assert.areEqual(isArray, storedField.isArray());
133:
134: if (isClientServer()) {
135: return;
136: }
137:
138: Assert.areEqual(hasIndex, storedField.hasIndex());
139:
140: // FIXME: test rename
141:
142: if (!hasIndex) {
143: Assert.expect(RuntimeException.class, new CodeBlock() {
144: public void run() throws Throwable {
145: storedField.traverseValues(new Visitor4() {
146: public void visit(Object obj) {
147: }
148: });
149: }
150: });
151: } else {
152: final IntByRef count = new IntByRef();
153: storedField.traverseValues(new Visitor4() {
154: public void visit(Object obj) {
155: count.value++;
156: Assert.areEqual(expectedFieldValue, obj);
157: }
158: });
159: Assert.areEqual(1, count.value);
160: }
161: }
162:
163: public void testEqualsAndHashCode() {
164: StoredClass clazz = itemStoredClass();
165: StoredClass same = itemStoredClass();
166: StoredClass other = db().storedClass(ItemParent.class);
167: Assert.equalsAndHashcode(clazz, same, other);
168: }
169:
170: private StoredClass itemStoredClass() {
171: return storedClass(Item.class);
172: }
173:
174: private StoredClass storedClass(Class clazz) {
175: return db().storedClass(clazz);
176: }
177:
178: }
|