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.assorted;
022:
023: import java.util.*;
024:
025: import com.db4o.config.*;
026:
027: import db4ounit.*;
028: import db4ounit.extensions.*;
029:
030: public class UpdateDepthTestCase extends AbstractDb4oTestCase {
031:
032: public static final class Item {
033:
034: public String name;
035: public Item child;
036: public Item[] childArray;
037: public Vector childVector;
038:
039: public Item() {
040: }
041:
042: public Item(String name) {
043: this .name = name;
044: }
045:
046: public Item(String name, Item child) {
047: this (name);
048: this .child = child;
049: }
050:
051: public Item(String name, Item child, Item[] childArray) {
052: this (name, child);
053: this .childArray = childArray;
054: this .childVector = new Vector();
055: for (int i = 0; i < childArray.length; ++i) {
056: childVector.addElement(childArray[i]);
057: }
058: }
059: }
060:
061: public static final class RootItem {
062: public Item root;
063:
064: public RootItem() {
065: }
066:
067: public RootItem(Item root) {
068: this .root = root;
069: }
070: }
071:
072: protected void store() throws Exception {
073: store(new RootItem(newGraph()));
074:
075: }
076:
077: protected void configure(Configuration config) throws Exception {
078: final ObjectClass itemClass = config.objectClass(Item.class);
079: itemClass.updateDepth(3);
080: itemClass.minimumActivationDepth(3);
081: }
082:
083: public void testDepth0() throws Exception {
084:
085: db().set(pokeName(queryRoot()), 0);
086:
087: expect(newGraph());
088: }
089:
090: public void testDepth1() throws Exception {
091:
092: final Item item = pokeChild(pokeName(queryRoot()));
093:
094: db().set(item, 1);
095:
096: expect(pokeName(newGraph()));
097: }
098:
099: public void testDepth2() throws Exception {
100:
101: final Item root = pokeChild(pokeName(queryRoot()));
102: pokeChild(root.child); // one level too many
103:
104: db().set(root, 2);
105:
106: expect(pokeChild(pokeName(newGraph())));
107: }
108:
109: public void testDepth3() throws Exception {
110: final Item item = pokeChild(pokeName(queryRoot()));
111: pokeChild(item.child);
112:
113: db().set(item, 3);
114:
115: expect(item);
116: }
117:
118: private Item newGraph() {
119: return new Item("Level 1", new Item("Level 2", new Item(
120: "Level 3"), new Item[] { new Item("Array Level 3") }),
121: new Item[] { new Item("Array Level 2") });
122: }
123:
124: private Item pokeChild(final Item item) {
125: pokeName(item.child);
126: if (item.childArray != null) {
127: pokeName(item.childArray[0]);
128: pokeName((Item) item.childVector.elementAt(0));
129: }
130: return item;
131: }
132:
133: private Item pokeName(Item item) {
134: item.name = item.name + "*";
135: return item;
136: }
137:
138: private void expect(Item expected) throws Exception {
139: reopen();
140: assertEquals(expected, queryRoot());
141: }
142:
143: private void assertEquals(Item expected, Item actual) {
144: if (expected == null) {
145: Assert.isNull(actual);
146: return;
147: }
148: Assert.isNotNull(actual);
149: Assert.areEqual(expected.name, actual.name);
150: assertEquals(expected.child, actual.child);
151: assertEquals(expected.childArray, actual.childArray);
152: assertCollection(expected.childVector, actual.childVector);
153: }
154:
155: private void assertCollection(Vector expected, Vector actual) {
156: if (expected == null) {
157: Assert.isNull(actual);
158: return;
159: }
160: Assert.isNotNull(actual);
161: Assert.areEqual(expected.size(), actual.size());
162: for (int i = 0; i < expected.size(); ++i) {
163: assertEquals((Item) expected.elementAt(i), (Item) actual
164: .elementAt(i));
165: }
166: }
167:
168: private void assertEquals(Item[] expected, Item[] actual) {
169: if (expected == null) {
170: Assert.isNull(actual);
171: return;
172: }
173: Assert.isNotNull(actual);
174: Assert.areEqual(expected.length, actual.length);
175: for (int i = 0; i < expected.length; ++i) {
176: assertEquals(expected[i], actual[i]);
177: }
178: }
179:
180: private Item queryRoot() {
181: return ((RootItem) newQuery(RootItem.class).execute().next()).root;
182: }
183:
184: public static void main(String[] arguments) {
185: new UpdateDepthTestCase().runSolo();
186: }
187:
188: }
|