KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.*;
24 import java.util.*;
25
26 import com.db4o.*;
27 import com.db4o.ext.*;
28 import com.db4o.foundation.io.*;
29 import com.db4o.inside.btree.*;
30 import com.db4o.inside.classindex.*;
31 import com.db4o.query.*;
32 import com.db4o.test.lib.*;
33
34 /**
35  *
36  */

37 public class UpdatingDb4oVersions {
38     
39     static final String JavaDoc PATH = "./test/db4oVersions/";
40
41     static final String JavaDoc[] VERSIONS = {
42         "db4o_3.0.3",
43         "db4o_4.0.004",
44         "db4o_4.1.001",
45         "db4o_4.6.003",
46         "db4o_4.6.004",
47         "db4o_5.0.007",
48         "db4o_5.1.001",
49         "db4o_5.2.001",
50         "db4o_5.2.003",
51         "db4o_5.2.008",
52         "db4o_5.3.001",
53         "db4o_5.4.004",
54         "db4o_5.5.2",
55         "db4o_5.6.2"
56     };
57
58     List list;
59     Map map;
60     String JavaDoc name;
61     
62     public void configure(){
63         Db4o.configure().allowVersionUpdates(true);
64         Db4o.configure().objectClass(UpdatingDb4oVersions.class).objectField("name").indexed(true);
65     }
66
67     public void store(){
68         if(Test.isClientServer()){
69             return;
70         }
71         String JavaDoc file = PATH + fileName();
72         new File(file).mkdirs();
73         new File(file).delete();
74         ExtObjectContainer objectContainer = Db4o.openFile(file).ext();
75         UpdatingDb4oVersions udv = new UpdatingDb4oVersions();
76         udv.name = "check";
77         udv.list = objectContainer.collections().newLinkedList();
78         udv.map = objectContainer.collections().newHashMap(1);
79         objectContainer.set(udv);
80         udv.list.add("check");
81         udv.map.put("check","check");
82         objectContainer.close();
83     }
84
85     public void test(){
86         if(Test.isClientServer()){
87             return;
88         }
89         for(int i = 0; i < VERSIONS.length; i ++){
90             String JavaDoc oldFilePath = PATH + VERSIONS[i];
91             File oldFile = new File(oldFilePath);
92             if(oldFile.exists()){
93                 
94                 String JavaDoc testFilePath = PATH + VERSIONS[i] + ".yap";
95                 new File(testFilePath).delete();
96                 
97                 File4.copy(oldFilePath, testFilePath);
98                 
99                 
100                 checkDatabaseFile(testFilePath);
101                 
102                 // Twice, to ensure everything is fine after opening, converting and closing.
103
checkDatabaseFile(testFilePath);
104                 
105                 
106             }else{
107                 System.err.println("Version upgrade check failed. File not found:");
108                 System.err.println(oldFile);
109             }
110         }
111     }
112
113     private void checkDatabaseFile(String JavaDoc testFile) {
114         ExtObjectContainer objectContainer = Db4o.openFile(testFile).ext();
115         checkStoredObjectsArePresent(objectContainer);
116         checkBTreeSize(objectContainer);
117         objectContainer.close();
118     }
119
120     private void checkBTreeSize(ExtObjectContainer objectContainer) {
121         YapStream yapStream = (YapStream)objectContainer;
122         StoredClass storedClass = objectContainer.storedClass(this.getClass().getName());
123         YapClass yc = (YapClass) storedClass;
124         BTreeClassIndexStrategy btreeClassIndexStrategy = (BTreeClassIndexStrategy) yc.index();
125         BTree btree = btreeClassIndexStrategy.btree();
126         Test.ensure(btree != null);
127         int size = btree.size(yapStream.getTransaction());
128         Test.ensure(size == 1);
129     }
130
131     private void checkStoredObjectsArePresent(ExtObjectContainer objectContainer) {
132         Query q = objectContainer.query();
133         q.constrain(UpdatingDb4oVersions.class);
134         ObjectSet objectSet = q.execute();
135         Test.ensure(objectSet.size() == 1);
136         UpdatingDb4oVersions udv = (UpdatingDb4oVersions)objectSet.next();
137         Test.ensure(udv.name.equals("check"));
138         Test.ensure(udv.list.size() == 1);
139         Test.ensure(udv.list.get(0).equals("check"));
140         Test.ensure(udv.map.get("check").equals("check"));
141     }
142     
143     private static String JavaDoc fileName(){
144         return Db4o.version().replace(' ', '_') + ".yap";
145     }
146 }
147
148
Popular Tags