KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > CountAnnotatedSheep


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 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.getName(),noParent);
40             Test.store(sheep);
41             Test.store(noSheep);
42             parent=sheep;
43             noParent=noSheep;
44         }
45     }
46     
47     public void testRead() {
48         Test.objectContainer().purge();
49         Sheep curSheep = (Sheep) fetch(Sheep.class,String.valueOf(NUMSHEEP));
50         int sheepCount=1;
51         while(curSheep.parent!=null) {
52             Test.ensure(curSheep.constructorCalled());
53             curSheep=curSheep.parent;
54             sheepCount++;
55         }
56         Test.ensureEquals(NUMSHEEP,sheepCount);
57
58         SheepNotAnnotated curNoSheep = (SheepNotAnnotated) fetch(SheepNotAnnotated.class,String.valueOf(NUMSHEEP));
59         Test.ensure(!curNoSheep.constructorCalled());
60         Test.ensure(curNoSheep.parent==null);
61     }
62
63     public void testUpdate() {
64         Test.objectContainer().purge();
65         Sheep curSheep = (Sheep) fetch(Sheep.class,String.valueOf(NUMSHEEP));
66         String JavaDoc oldName=curSheep.getName();
67         curSheep.setName(oldName+"X");
68         Test.store(curSheep);
69         SheepNotAnnotated curNoSheep = (SheepNotAnnotated) fetch(SheepNotAnnotated.class,String.valueOf(NUMSHEEP));
70         Test.objectContainer().ext().activate(curNoSheep,1);
71         String JavaDoc oldNoName=curNoSheep.getName();
72         curNoSheep.setName(oldNoName+"X");
73         Test.store(curNoSheep);
74         Test.commit();
75         Test.reOpen();
76         fetch(Sheep.class,oldName+"X");
77         // FIXME: problem with configuration or rather with global update depth of 0?
78
// fetch(SheepNotAnnotated.class,oldNoName);
79
}
80
81     private Object JavaDoc fetch(Class JavaDoc clazz,String JavaDoc name) {
82         Query noSheepQuery=Test.query();
83         noSheepQuery.constrain(clazz);
84         noSheepQuery.descend("name").constrain(name);
85         ObjectSet noSheep=noSheepQuery.execute();
86         Test.ensureEquals(1,noSheep.size());
87         return noSheep.next();
88     }
89
90 }
91
Popular Tags