KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectContainer;
24 import com.db4o.ObjectSet;
25 import com.db4o.ext.ExtDb4o;
26 import com.db4o.ext.MemoryFile;
27
28 public class AnnotationTest {
29
30     public static void main(String JavaDoc[] args) {
31         MemoryFile file = new MemoryFile();
32         ObjectContainer db = ExtDb4o.openMemoryFile(file);
33         try {
34             fillDBwithSheeps(db);
35             db = ExtDb4o.openMemoryFile(file);
36             retriveSheep(db);
37
38             db = ExtDb4o.openMemoryFile(file);
39             System.out.println("\n");
40             updateSheep(db);
41
42             db = ExtDb4o.openMemoryFile(file);
43             System.out.println("\n");
44             deleteSheep(db);
45
46         } finally {
47             db.close();
48         }
49     }
50
51     private static void updateSheep(ObjectContainer db) {
52         System.out.println("updating annotated sheeps");
53         Sheep sheep = (Sheep) db.get(new Sheep("test7", null)).next();
54         sheep.setName(sheep.getName() + "_new");
55         db.set(sheep);
56         ObjectSet<Sheep> result = db.get(Sheep.class);
57         while (result.hasNext()) {
58             System.out.println(result.next());
59         }
60
61         System.out.println("updating not annotated sheeps");
62         SheepNotAnnotated notAnnot = (SheepNotAnnotated) db.get(
63                 new SheepNotAnnotated("notAnnotTest7", null)).next();
64         notAnnot.setName(notAnnot.getName() + "_new");
65         db.set(notAnnot);
66         ObjectSet<SheepNotAnnotated> res = db.get(SheepNotAnnotated.class);
67         while (res.hasNext()) {
68             System.out.println(res.next());
69
70         }
71
72         db.close();
73     }
74
75     private static void deleteSheep(ObjectContainer db) {
76         System.out.println("deleting annotated sheeps");
77         Sheep sheep = (Sheep) db.get(new Sheep("test15", null)).next();
78         db.delete(sheep);
79         ObjectSet<Sheep> result = db.get(Sheep.class);
80         while (result.hasNext()) {
81             System.out.println(">>" + (Sheep) result.next());
82         }
83         System.out.println("deleting not annotated sheeps");
84         SheepNotAnnotated notAnnot = (SheepNotAnnotated) db.get(
85                 new SheepNotAnnotated("notAnnotTest15", null)).next();
86         db.delete(notAnnot);
87         ObjectSet<SheepNotAnnotated> res = db.get(SheepNotAnnotated.class);
88         while (res.hasNext()) {
89             System.out.println(">>>" + (SheepNotAnnotated) res.next());
90         }
91         db.commit();
92         db.close();
93     }
94
95     private static void retriveSheep(ObjectContainer db) {
96         System.out.println("retriving annotated sheeps");
97         ObjectSet<Sheep> result = db.get(new Sheep("test23", null));
98         System.out.println((Sheep) result.next());
99
100         System.out.println("retriving not annotated sheeps");
101         ObjectSet<SheepNotAnnotated> res = db.get(new SheepNotAnnotated(
102                 "notAnnotTest23", null));
103         System.out.println((SheepNotAnnotated) res.next());
104         db.close();
105     }
106
107     private static void fillDBwithSheeps(ObjectContainer db) {
108         Sheep parent = null;
109         for (int i = 0; i < 36; i++) {
110             Sheep sheep = new Sheep("test" + i, parent);
111             db.set(sheep);
112             parent = sheep;
113         }
114
115         SheepNotAnnotated notAnnotParent = null;
116         for (int j = 0; j < 36; j++) {
117             SheepNotAnnotated notAnnotChild = new SheepNotAnnotated(
118                     "notAnnotTest" + j, notAnnotParent);
119             db.set(notAnnotChild);
120             notAnnotParent = notAnnotChild;
121         }
122
123         db.commit();
124         db.close();
125     }
126
127 }
128
Popular Tags