01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: 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;
22:
23: import com.db4o.*;
24: import com.db4o.query.*;
25:
26: public class CountAnnotatedSheep {
27: public final static int NUMSHEEP = 10;
28:
29: public void configure() {
30: Db4o.configure().activationDepth(0);
31: Db4o.configure().updateDepth(0);
32: }
33:
34: public void store() {
35: Sheep parent = null;
36: SheepNotAnnotated noParent = null;
37: for (int i = 0; i < 10; i++) {
38: Sheep sheep = new Sheep(String.valueOf(i + 1), parent);
39: SheepNotAnnotated noSheep = new SheepNotAnnotated(sheep
40: .getName(), noParent);
41: Test.store(sheep);
42: Test.store(noSheep);
43: parent = sheep;
44: noParent = noSheep;
45: }
46: }
47:
48: public void testRead() {
49: Test.objectContainer().purge();
50: Sheep curSheep = (Sheep) fetch(Sheep.class, String
51: .valueOf(NUMSHEEP));
52: int sheepCount = 1;
53: while (curSheep.parent != null) {
54: Test.ensure(curSheep.constructorCalled());
55: curSheep = curSheep.parent;
56: sheepCount++;
57: }
58: Test.ensureEquals(NUMSHEEP, sheepCount);
59:
60: SheepNotAnnotated curNoSheep = (SheepNotAnnotated) fetch(
61: SheepNotAnnotated.class, String.valueOf(NUMSHEEP));
62: Test.ensure(!curNoSheep.constructorCalled());
63: Test.ensure(curNoSheep.parent == null);
64: }
65:
66: public void testUpdate() {
67: Test.objectContainer().purge();
68: Sheep curSheep = (Sheep) fetch(Sheep.class, String
69: .valueOf(NUMSHEEP));
70: String oldName = curSheep.getName();
71: curSheep.setName(oldName + "X");
72: Test.store(curSheep);
73: SheepNotAnnotated curNoSheep = (SheepNotAnnotated) fetch(
74: SheepNotAnnotated.class, String.valueOf(NUMSHEEP));
75: Test.objectContainer().ext().activate(curNoSheep, 1);
76: String oldNoName = curNoSheep.getName();
77: curNoSheep.setName(oldNoName + "X");
78: Test.store(curNoSheep);
79: Test.commit();
80: Test.reOpen();
81: fetch(Sheep.class, oldName + "X");
82: // FIXME: problem with configuration or rather with global update depth of 0?
83: // fetch(SheepNotAnnotated.class,oldNoName);
84: }
85:
86: private Object fetch(Class clazz, String name) {
87: Query noSheepQuery = Test.query();
88: noSheepQuery.constrain(clazz);
89: noSheepQuery.descend("name").constrain(name);
90: ObjectSet noSheep = noSheepQuery.execute();
91: Test.ensureEquals(1, noSheep.size());
92: return noSheep.next();
93: }
94:
95: }
|