001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.test;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026:
027: public class ReadOnly implements Runnable {
028:
029: private static final String FILE = "readonly.yap";
030: private static final int COUNT = 100;
031: private static final String MY_STRING = "ReadOnly test instance ";
032:
033: public String myString;
034:
035: public static void main(String[] args) {
036: Db4o.configure().readOnly(true);
037: new ReadOnly().spendSomeTime();
038: Db4o.configure().readOnly(false);
039: }
040:
041: public void run() {
042: setUp();
043: test();
044: Db4o.configure().readOnly(false);
045: }
046:
047: private void setUp() {
048: new File(FILE).delete();
049: ObjectContainer con = Db4o.openFile(FILE);
050: for (int i = 0; i < COUNT; i++) {
051: ReadOnly ro = new ReadOnly();
052: ro.myString = MY_STRING + i;
053: con.set(ro);
054: }
055: con.close();
056: }
057:
058: private void test() {
059: Db4o.configure().readOnly(true);
060: checkCount();
061: ObjectContainer con = Db4o.openFile(FILE);
062: con.set(new ReadOnly());
063: con.close();
064: checkCount();
065: try {
066: /*
067: Process pcs = Runtime.getRuntime().exec(new String[] {"javaw", "com.db4o.test.ReadOnly"}, null, new File("D:\\db4o\\"));
068: InputStream in = pcs.getInputStream();
069: while(in.available() > 0){
070: System.out.print(in.read());
071: }
072: */
073: } catch (Exception e) {
074: }
075: }
076:
077: private void spendSomeTime() {
078: Db4o.configure().readOnly(true);
079: ObjectContainer con = Db4o.openFile(FILE);
080: ObjectSet set = con.get(new ReadOnly());
081: while (set.hasNext()) {
082: ReadOnly ro = (ReadOnly) set.next();
083: if (ro.myString.equals(MY_STRING + "1")) {
084: System.out.println("O.K. " + ro.myString);
085: }
086: if (ro.myString.equals(MY_STRING + (COUNT - 1))) {
087: System.out.println("O.K. " + ro.myString);
088: }
089: synchronized (this ) {
090: try {
091: this .wait(50);
092: } catch (Exception e) {
093: }
094: }
095: }
096: con.close();
097: }
098:
099: private void checkCount() {
100: Db4o.configure().readOnly(true);
101: ObjectContainer con = Db4o.openFile(FILE);
102: int size = con.get(new ReadOnly()).size();
103: if (size != COUNT) {
104: throw new RuntimeException(
105: "ReadOnly.test: unexpected number of objects:"
106: + size);
107: }
108: con.close();
109: }
110:
111: }
|