KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 import com.db4o.*;
26
27
28 public class ReadOnly implements Runnable JavaDoc{
29     
30     private static final String JavaDoc FILE = "readonly.yap";
31     private static final int COUNT = 100;
32     private static final String JavaDoc MY_STRING = "ReadOnly test instance ";
33     
34     public String JavaDoc myString;
35     
36     public static void main(String JavaDoc [] 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             /*
68             Process pcs = Runtime.getRuntime().exec(new String[] {"javaw", "com.db4o.test.ReadOnly"}, null, new File("D:\\db4o\\"));
69             InputStream in = pcs.getInputStream();
70             while(in.available() > 0){
71                 System.out.print(in.read());
72             }
73             */

74         }catch(Exception JavaDoc 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 JavaDoc 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 JavaDoc("ReadOnly.test: unexpected number of objects:" + size);
106         }
107         con.close();
108     }
109
110 }
111
Popular Tags