1 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 37 public class UpdatingDb4oVersions { 38 39 static final String PATH = "./test/db4oVersions/"; 40 41 static final String [] 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 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 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 oldFilePath = PATH + VERSIONS[i]; 91 File oldFile = new File(oldFilePath); 92 if(oldFile.exists()){ 93 94 String testFilePath = PATH + VERSIONS[i] + ".yap"; 95 new File(testFilePath).delete(); 96 97 File4.copy(oldFilePath, testFilePath); 98 99 100 checkDatabaseFile(testFilePath); 101 102 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 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 fileName(){ 144 return Db4o.version().replace(' ', '_') + ".yap"; 145 } 146 } 147 148 | Popular Tags |