1 21 package com.db4o.test; 22 23 import java.io.*; 24 25 import com.db4o.*; 26 import com.db4o.defragment.*; 27 import com.db4o.ext.*; 28 import com.db4o.foundation.*; 29 import com.db4o.foundation.io.*; 30 import com.db4o.query.*; 31 import com.db4o.tools.*; 32 33 public class Test extends AllTests { 34 35 private static final boolean USE_NEW_DEFRAGMENT = true; 36 37 private static ObjectServer objectServer; 38 private static ExtObjectContainer oc; 39 private static ExtObjectContainer _replica; 40 41 static AllTests currentRunner; 42 public static boolean clientServer = true; 43 static boolean runServer = true; 44 static int errorCount = 0; 45 public static int assertionCount = 0; 46 static int run; 47 48 static MemoryFile memoryFile; 49 static byte[] memoryFileContent; 50 51 public static final boolean COMPARE_INTERNAL_OK = false; 52 53 public static boolean canCheckFileSize() { 54 if (currentRunner != null) { 55 if(!MEMORY_FILE) { 56 return !clientServer || !currentRunner.REMOTE_SERVER; 57 } 58 } 59 return false; 60 } 61 62 public static void beginTesting(){ 63 File file = new File(BLOB_PATH); 64 file.mkdirs(); 65 if(! file.exists()) { 66 System.out.println("Unable to create blob directory: " + BLOB_PATH); 67 } 68 } 69 70 private static Class classOf(Object obj){ 71 if(obj == null){ 72 return null; 73 } 74 if(obj instanceof Class ){ 75 return (Class )obj; 76 } 77 return obj.getClass(); 78 } 79 80 public static void close() { 81 if (null != oc) { 82 while (!oc.close()) { 83 } 84 oc = null; 85 } 86 if(memoryFile != null) { 87 memoryFileContent = memoryFile.getBytes(); 88 } 89 if(_replica != null){ 90 while(!_replica.close()) { 91 } 92 _replica = null; 93 } 94 } 95 96 public static void commit() { 97 oc.commit(); 98 } 99 100 public static void configureMessageLevel(){ 101 Db4o.configure().messageLevel(-1); 102 } 103 104 public static ObjectServer currentServer(){ 105 if(clientServer && runServer){ 106 return objectServer; 107 } 108 return null; 109 } 110 111 public static void defragment(){ 112 String fileName = FILE_SOLO; 113 close(); 114 if (isClientServer()) { 115 server().close(); 116 fileName = FILE_SERVER; 117 objectServer=null; 118 } 119 try { 120 if(MEMORY_FILE){ 121 File4.delete(fileName); 122 RandomAccessFile raf = new RandomAccessFile(fileName, "rw"); 123 raf.write(memoryFileContent); 124 raf.close(); 125 } 126 127 if(USE_NEW_DEFRAGMENT){ 128 String targetFile = fileName + ".defrag.backup"; 129 DefragmentConfig defragConfig = new DefragmentConfig(fileName, targetFile); 130 defragConfig.forceBackupDelete(true); 131 com.db4o.defragment.Defragment.defrag(defragConfig); 132 } else { 133 134 new com.db4o.tools.Defragment().run(fileName, true); 135 136 } 137 138 if(MEMORY_FILE){ 139 RandomAccessFile raf = new RandomAccessFile(fileName, "rw"); 140 memoryFileContent = new byte[(int)raf.length()]; 141 raf.read(memoryFileContent); 142 raf.close(); 143 } 144 145 } catch(Exception e){ 146 e.printStackTrace(); 147 } 148 open(); 149 } 150 151 public static void delete() { 152 new File(FILE_SOLO).delete(); 153 new File(FILE_SERVER).delete(); 154 new File(replicatedFileName(false)).delete(); 155 new File(replicatedFileName(true)).delete(); 156 } 157 158 public static void delete(Object obj) { 159 objectContainer().delete(obj); 160 } 161 162 public static void deleteAll(ObjectContainer container) { 163 deleteObjectSet(container, container.get(null)); 164 } 165 166 public static void deleteObjectSet(ObjectContainer container, ObjectSet all) { 167 while (all.hasNext()) { 168 container.delete(all.next()); 169 } 170 } 171 172 public static void deleteAllInstances(Object obj) { 173 try { 174 Query q = oc.query(); 175 q.constrain(classOf(obj)); 176 deleteObjectSet(oc, q.execute()); 177 } catch (Exception e) { 178 e.printStackTrace(); 179 } 180 } 181 182 public static void end() { 183 if (oc != null) { 184 while (!oc.close()) { 185 } 186 } 187 if (objectServer != null) { 188 Cool.sleepIgnoringInterruption(1000); 189 objectServer.close(); 190 objectServer = null; 191 } 192 } 193 194 public static boolean ensure(boolean condition,String msg) { 195 assertionCount++; 196 if (!condition) { 197 error(msg); 198 return false; 199 } 200 return true; 201 } 202 203 public static boolean ensure(boolean condition) { 204 return ensure(condition,null); 205 } 206 207 public static boolean ensureEquals(Object exp,Object actual) { 208 return ensureEquals(exp,actual,null); 209 } 210 211 public static boolean ensureEquals(Object exp,Object actual,String msg) { 212 assertionCount++; 213 if (!exp.equals(actual)) { 214 String errMsg = "Expected "+exp+" but was "+actual; 215 if(msg!=null) { 216 errMsg=msg+"\n"+errMsg; 217 } 218 error(errMsg); 219 return false; 220 } 221 return true; 222 } 223 224 public static boolean ensureEquals(int exp,int actual) { 225 return ensureEquals(exp,actual,null); 226 } 227 228 public static boolean ensureEquals(int exp,int actual,String msg) { 229 return ensureEquals(new Integer (exp),new Integer (actual),msg); 230 } 231 232 public static void ensureOccurrences(Object obj, int count) { 233 assertionCount++; 234 int occ = occurrences(obj); 235 if(occ != count) { 236 error("Expected count: " + count + " Count was:" + occ); 237 } 238 } 239 240 public static void error(String msg) { 241 errorCount++; 242 if(msg != null) { 243 new Exception (msg).printStackTrace(); 244 }else { 245 new Exception ().printStackTrace(); 246 } 247 } 248 249 public static void error() { 250 error(null); 251 } 252 253 public static int fileLength() { 254 String fileName = clientServer ? FILE_SERVER : FILE_SOLO; 255 return (int) new File(fileName).length(); 256 } 257 258 public static void forEach(Object obj, Visitor4 vis) { 259 ObjectContainer con = objectContainer(); 260 con.deactivate(obj, Integer.MAX_VALUE); 261 ObjectSet set = oc.get(obj); 262 while (set.hasNext()) { 263 vis.visit(set.next()); 264 } 265 } 266 267 public static Object getOne(Object obj) { 268 Query q = oc.query(); 269 q.constrain(classOf(obj)); 270 ObjectSet set = q.execute(); 271 if (set.size() != 1) { 272 error(); 273 } 274 return set.next(); 275 } 276 277 public static boolean isClientServer(){ 278 return currentServer() != null; 279 } 280 281 public static void log(Query q) { 282 ObjectSet set = q.execute(); 283 while (set.hasNext()) { 284 Logger.log(oc, set.next()); 285 } 286 } 287 288 public static void logAll() { 289 ObjectSet set = oc.get(null); 290 while (set.hasNext()) { 291 Logger.log(oc, set.next()); 292 } 293 } 294 295 public static ExtObjectContainer objectContainer() { 296 if (oc == null) { 297 open(); 298 } 299 return oc; 300 } 301 302 public static int occurrences(Object obj) { 303 Query q = oc.query(); 304 q.constrain(classOf(obj)); 305 return q.execute().size(); 306 } 307 308 public static ExtObjectContainer open() { 309 if (runServer && clientServer && objectServer == null) { 310 objectServer = Db4o.openServer(FILE_SERVER, SERVER_PORT); 311 312 if(objectServer != null){ 314 objectServer.grantAccess(DB4O_USER, DB4O_PASSWORD); 315 objectServer.ext().configure().messageLevel(0); 316 } 317 else { 318 throw new RuntimeException ("Couldn't open server."); 319 } 320 } 321 if (clientServer) { 322 oc = openClient(); 323 } else { 324 if(MEMORY_FILE) { 325 memoryFile = new MemoryFile(memoryFileContent); 326 oc = ExtDb4o.openMemoryFile(memoryFile).ext(); 327 }else { 328 oc = Db4o.openFile(FILE_SOLO).ext(); 329 } 330 } 331 return oc; 332 } 333 334 public static ExtObjectContainer openClient(){ 335 if (clientServer) { 336 try { 337 338 if(EMBEDDED_CLIENT){ 339 return objectServer.openClient().ext(); 340 } 341 342 return Db4o.openClient(SERVER_HOSTNAME, SERVER_PORT, DB4O_USER, DB4O_PASSWORD).ext(); 343 } catch (Exception e) { 345 e.printStackTrace(); 346 } 347 } 348 return null; 349 } 350 351 public static Query query() { 352 return objectContainer().query(); 353 } 354 355 public static ObjectContainer reOpen() { 356 close(); 357 return open(); 358 } 359 360 public static ObjectContainer reOpenServer(){ 361 if(runServer && clientServer){ 362 close(); 363 if(objectServer!=null) { 364 objectServer.close(); 365 objectServer = null; 366 } 367 Cool.sleepIgnoringInterruption(500); 368 return open(); 369 }else{ 370 return reOpen(); 371 } 372 } 373 374 public static ExtObjectContainer replica(){ 375 if(_replica != null){ 376 while(!_replica.close()); 377 } 378 _replica = Db4o.openFile(replicatedFileName(isClientServer())).ext(); 379 return _replica; 380 } 381 382 private static String replicatedFileName(boolean clientServer){ 383 if(clientServer){ 384 return "replicated_" + FILE_SERVER; 385 } 386 return "replicated_" + FILE_SOLO; 387 388 } 389 390 public static void rollBack() { 391 objectContainer().rollback(); 392 } 393 394 public static ObjectServer server(){ 395 return objectServer; 396 } 397 398 399 public static void store(Object obj) { 400 objectContainer().set(obj); 401 } 402 403 public static void statistics() { 404 Statistics.main(new String [] { FILE_SOLO }); 405 } 406 407 public static void commitSync(ExtObjectContainer client1, ExtObjectContainer client2) { 408 client1.setSemaphore("sem", 0); 409 client1.commit(); 410 client1.releaseSemaphore("sem"); 411 ensure(client2.setSemaphore("sem", 5000)); 412 client2.releaseSemaphore("sem"); 413 } 414 415 } 416 | Popular Tags |