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.test;
022:
023: import com.db4o.ObjectContainer;
024: import com.db4o.ObjectSet;
025: import com.db4o.ext.ExtDb4o;
026: import com.db4o.ext.MemoryFile;
027:
028: public class AnnotationTest {
029:
030: public static void main(String[] args) {
031: MemoryFile file = new MemoryFile();
032: ObjectContainer db = ExtDb4o.openMemoryFile(file);
033: try {
034: fillDBwithSheeps(db);
035: db = ExtDb4o.openMemoryFile(file);
036: retriveSheep(db);
037:
038: db = ExtDb4o.openMemoryFile(file);
039: System.out.println("\n");
040: updateSheep(db);
041:
042: db = ExtDb4o.openMemoryFile(file);
043: System.out.println("\n");
044: deleteSheep(db);
045:
046: } finally {
047: db.close();
048: }
049: }
050:
051: private static void updateSheep(ObjectContainer db) {
052: System.out.println("updating annotated sheeps");
053: Sheep sheep = (Sheep) db.get(new Sheep("test7", null)).next();
054: sheep.setName(sheep.getName() + "_new");
055: db.set(sheep);
056: ObjectSet<Sheep> result = db.get(Sheep.class);
057: while (result.hasNext()) {
058: System.out.println(result.next());
059: }
060:
061: System.out.println("updating not annotated sheeps");
062: SheepNotAnnotated notAnnot = (SheepNotAnnotated) db.get(
063: new SheepNotAnnotated("notAnnotTest7", null)).next();
064: notAnnot.setName(notAnnot.getName() + "_new");
065: db.set(notAnnot);
066: ObjectSet<SheepNotAnnotated> res = db
067: .get(SheepNotAnnotated.class);
068: while (res.hasNext()) {
069: System.out.println(res.next());
070:
071: }
072:
073: db.close();
074: }
075:
076: private static void deleteSheep(ObjectContainer db) {
077: System.out.println("deleting annotated sheeps");
078: Sheep sheep = (Sheep) db.get(new Sheep("test15", null)).next();
079: db.delete(sheep);
080: ObjectSet<Sheep> result = db.get(Sheep.class);
081: while (result.hasNext()) {
082: System.out.println(">>" + (Sheep) result.next());
083: }
084: System.out.println("deleting not annotated sheeps");
085: SheepNotAnnotated notAnnot = (SheepNotAnnotated) db.get(
086: new SheepNotAnnotated("notAnnotTest15", null)).next();
087: db.delete(notAnnot);
088: ObjectSet<SheepNotAnnotated> res = db
089: .get(SheepNotAnnotated.class);
090: while (res.hasNext()) {
091: System.out.println(">>>" + (SheepNotAnnotated) res.next());
092: }
093: db.commit();
094: db.close();
095: }
096:
097: private static void retriveSheep(ObjectContainer db) {
098: System.out.println("retriving annotated sheeps");
099: ObjectSet<Sheep> result = db.get(new Sheep("test23", null));
100: System.out.println((Sheep) result.next());
101:
102: System.out.println("retriving not annotated sheeps");
103: ObjectSet<SheepNotAnnotated> res = db
104: .get(new SheepNotAnnotated("notAnnotTest23", null));
105: System.out.println((SheepNotAnnotated) res.next());
106: db.close();
107: }
108:
109: private static void fillDBwithSheeps(ObjectContainer db) {
110: Sheep parent = null;
111: for (int i = 0; i < 36; i++) {
112: Sheep sheep = new Sheep("test" + i, parent);
113: db.set(sheep);
114: parent = sheep;
115: }
116:
117: SheepNotAnnotated notAnnotParent = null;
118: for (int j = 0; j < 36; j++) {
119: SheepNotAnnotated notAnnotChild = new SheepNotAnnotated(
120: "notAnnotTest" + j, notAnnotParent);
121: db.set(notAnnotChild);
122: notAnnotParent = notAnnotChild;
123: }
124:
125: db.commit();
126: db.close();
127: }
128:
129: }
|