KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > acid > CrashSimulatingTestCase


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.db4ounit.common.acid;
22
23 import java.io.IOException JavaDoc;
24
25 import com.db4o.*;
26 import com.db4o.db4ounit.common.assorted.SimplestPossibleItem;
27 import com.db4o.foundation.io.*;
28 import com.db4o.io.RandomAccessFileAdapter;
29 import com.db4o.query.*;
30
31 import db4ounit.*;
32 import db4ounit.extensions.fixtures.OptOutCS;
33
34
35 public class CrashSimulatingTestCase implements TestCase, OptOutCS {
36     
37     public String JavaDoc _name;
38     
39     public CrashSimulatingTestCase _next;
40     
41     private static final String JavaDoc PATH = Path4.combine(Path4.getTempPath(), "crashSimulate");
42     private static final String JavaDoc FILE = Path4.combine(PATH, "cs");
43     
44     static final boolean LOG = false;
45     
46     
47     public CrashSimulatingTestCase() {
48     }
49     
50     public CrashSimulatingTestCase(CrashSimulatingTestCase next_, String JavaDoc name) {
51         _next = next_;
52         _name = name;
53     }
54     
55     public void test() throws IOException JavaDoc{
56         
57         File4.delete(FILE);
58         File4.mkdirs(PATH);
59         
60         Db4o.configure().bTreeNodeSize(4);
61
62         createFile();
63         
64         CrashSimulatingIoAdapter adapterFactory = new CrashSimulatingIoAdapter(new RandomAccessFileAdapter());
65         Db4o.configure().io(adapterFactory);
66         
67         ObjectContainer oc = Db4o.openFile(FILE);
68         
69         ObjectSet objectSet = oc.get(new CrashSimulatingTestCase(null, "three"));
70         oc.delete(objectSet.next());
71         
72         oc.set(new CrashSimulatingTestCase(null, "four"));
73         oc.set(new CrashSimulatingTestCase(null, "five"));
74         oc.set(new CrashSimulatingTestCase(null, "six"));
75         oc.set(new CrashSimulatingTestCase(null, "seven"));
76         oc.set(new CrashSimulatingTestCase(null, "eight"));
77         oc.set(new CrashSimulatingTestCase(null, "nine"));
78         oc.set(new CrashSimulatingTestCase(null, "10"));
79         oc.set(new CrashSimulatingTestCase(null, "11"));
80         oc.set(new CrashSimulatingTestCase(null, "12"));
81         oc.set(new CrashSimulatingTestCase(null, "13"));
82         oc.set(new CrashSimulatingTestCase(null, "14"));
83         
84         oc.commit();
85         
86         Query q = oc.query();
87         q.constrain(CrashSimulatingTestCase.class);
88         objectSet = q.execute();
89         while(objectSet.hasNext()){
90             CrashSimulatingTestCase cst = (CrashSimulatingTestCase) objectSet.next();
91             if( ! (cst._name.equals("10") || cst._name.equals("13")) ){
92                 oc.delete(cst);
93             }
94         }
95         
96         oc.commit();
97
98         oc.close();
99
100         Db4o.configure().io(new RandomAccessFileAdapter());
101
102         int count = adapterFactory.batch.writeVersions(FILE);
103
104         checkFiles("R", adapterFactory.batch.numSyncs());
105         checkFiles("W", count);
106                 
107         System.out.println("Total versions: " + count);
108     }
109
110     private void checkFiles(String JavaDoc infix,int count) {
111         for (int i = 1; i <= count; i++) {
112             if(LOG){
113                 System.out.println("Checking " + infix + i);
114             }
115             String JavaDoc fileName = FILE + infix + i;
116             ObjectContainer oc = Db4o.openFile(fileName);
117             
118             if(! stateBeforeCommit(oc)){
119                 if(! stateAfterFirstCommit(oc)){
120                     Assert.isTrue(stateAfterSecondCommit(oc));
121                 }
122             }
123             oc.close();
124         }
125     }
126     
127     private boolean stateBeforeCommit(ObjectContainer oc){
128         return expect(oc, new String JavaDoc[] {"one", "two", "three"});
129     }
130     
131     private boolean stateAfterFirstCommit (ObjectContainer oc){
132         return expect(oc, new String JavaDoc[] {"one", "two", "four", "five", "six", "seven", "eight", "nine", "10", "11", "12", "13", "14" });
133     }
134     
135     private boolean stateAfterSecondCommit (ObjectContainer oc){
136         return expect(oc, new String JavaDoc[] {"10", "13"});
137     }
138     
139     private boolean expect(ObjectContainer oc, String JavaDoc[] names){
140         ObjectSet objectSet = oc.query(CrashSimulatingTestCase.class);
141         if(objectSet.size()!=names.length) {
142             return false;
143         }
144         while(objectSet.hasNext()){
145             CrashSimulatingTestCase cst = (CrashSimulatingTestCase)objectSet.next();
146             boolean found = false;
147             for (int i = 0; i < names.length; i++) {
148                 if(cst._name.equals(names[i])){
149                     names[i] = null;
150                     found = true;
151                     break;
152                 }
153             }
154             if(! found){
155                 return false;
156             }
157         }
158         for (int i = 0; i < names.length; i++) {
159             if(names[i] != null){
160                 return false;
161             }
162         }
163         return true;
164     }
165     
166     private void createFile(){
167         ObjectContainer oc = Db4o.openFile(FILE);
168         for (int i = 0; i < 10; i++) {
169             oc.set(new SimplestPossibleItem("delme"));
170         }
171         CrashSimulatingTestCase one = new CrashSimulatingTestCase(null, "one");
172         CrashSimulatingTestCase two = new CrashSimulatingTestCase(one, "two");
173         CrashSimulatingTestCase three = new CrashSimulatingTestCase(one, "three");
174         oc.set(one);
175         oc.set(two);
176         oc.set(three);
177         oc.commit();
178         ObjectSet objectSet = oc.query(SimplestPossibleItem.class);
179         while(objectSet.hasNext()){
180             oc.delete(objectSet.next());
181         }
182         oc.close();
183         File4.copy(FILE, FILE + "0");
184     }
185     
186     public String JavaDoc toString() {
187         return _name+" -> "+_next;
188     }
189     
190 }
191
Popular Tags