1 21 package com.db4o.test; 22 23 import java.io.*; 24 25 import com.db4o.*; 26 27 28 public class ReadOnly implements Runnable { 29 30 private static final String FILE = "readonly.yap"; 31 private static final int COUNT = 100; 32 private static final String MY_STRING = "ReadOnly test instance "; 33 34 public String myString; 35 36 public static void main(String [] args){ 37 Db4o.configure().readOnly(true); 38 new ReadOnly().spendSomeTime(); 39 Db4o.configure().readOnly(false); 40 } 41 42 public void run(){ 43 setUp(); 44 test(); 45 Db4o.configure().readOnly(false); 46 } 47 48 private void setUp(){ 49 new File(FILE).delete(); 50 ObjectContainer con = Db4o.openFile(FILE); 51 for (int i = 0; i < COUNT; i++) { 52 ReadOnly ro = new ReadOnly(); 53 ro.myString = MY_STRING + i; 54 con.set(ro); 55 } 56 con.close(); 57 } 58 59 private void test(){ 60 Db4o.configure().readOnly(true); 61 checkCount(); 62 ObjectContainer con = Db4o.openFile(FILE); 63 con.set(new ReadOnly()); 64 con.close(); 65 checkCount(); 66 try{ 67 74 }catch(Exception e){ 75 } 76 } 77 78 private void spendSomeTime(){ 79 Db4o.configure().readOnly(true); 80 ObjectContainer con = Db4o.openFile(FILE); 81 ObjectSet set = con.get(new ReadOnly()); 82 while(set.hasNext()){ 83 ReadOnly ro = (ReadOnly)set.next(); 84 if(ro.myString.equals(MY_STRING + "1")){ 85 System.out.println("O.K. " + ro.myString); 86 } 87 if(ro.myString.equals(MY_STRING + (COUNT - 1))){ 88 System.out.println("O.K. " + ro.myString); 89 } 90 synchronized(this){ 91 try{ 92 this.wait(50); 93 }catch(Exception e){ 94 } 95 } 96 } 97 con.close(); 98 } 99 100 private void checkCount(){ 101 Db4o.configure().readOnly(true); 102 ObjectContainer con = Db4o.openFile(FILE); 103 int size = con.get(new ReadOnly()).size(); 104 if (size != COUNT){ 105 throw new RuntimeException ("ReadOnly.test: unexpected number of objects:" + size); 106 } 107 con.close(); 108 } 109 110 } 111 | Popular Tags |