KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > common > assorted > BackupStressTestCase


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.assorted;
22
23 import java.io.*;
24
25 import com.db4o.*;
26 import com.db4o.query.*;
27
28 import db4ounit.*;
29
30
31 public class BackupStressTestCase implements TestLifeCycle {
32     
33     private static boolean verbose = false;
34     
35     private static boolean runOnOldJDK = false;
36     
37     private static final String JavaDoc FILE = "backupstress.yap";
38     
39     private static final int ITERATIONS = 5;
40     
41     private static final int OBJECTS = 50;
42     
43     private static final int COMMITS = 10;
44     
45     private ObjectContainer _objectContainer;
46     
47     private volatile boolean _inBackup;
48     
49     private volatile boolean _noMoreBackups;
50     
51     private int _backups;
52     
53     private int _commitCounter;
54     
55     
56     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
57         
58         verbose = true;
59         runOnOldJDK = true;
60         
61         BackupStressTestCase stressTest = new BackupStressTestCase();
62         stressTest.setUp();
63         stressTest.test();
64     }
65     
66     public void setUp(){
67         Db4o.configure().objectClass(BackupStressItem.class).objectField("_iteration").indexed(true);
68     }
69     
70     public void tearDown() {
71     }
72
73     public void test() throws Exception JavaDoc {
74         openDatabase();
75         try {
76             runTestIterations();
77         } finally {
78             closeDatabase();
79         }
80         checkBackups();
81     }
82
83     private void runTestIterations() {
84         if(! runOnOldJDK && isOldJDK()) {
85             System.out.println("BackupStressTest is too slow for regression testing on Java JDKs < 1.4");
86             return;
87         }
88         
89         BackupStressIteration iteration = new BackupStressIteration();
90         _objectContainer.set(iteration);
91         _objectContainer.commit();
92         startBackupThread();
93         for (int i = 1; i <= ITERATIONS; i++) {
94             for (int obj = 0; obj < OBJECTS; obj++) {
95                 _objectContainer.set(new BackupStressItem("i" + obj, i));
96                 _commitCounter ++;
97                 if(_commitCounter >= COMMITS){
98                     _objectContainer.commit();
99                     _commitCounter = 0;
100                 }
101             }
102             iteration.setCount(i);
103             _objectContainer.set(iteration);
104             _objectContainer.commit();
105         }
106     }
107
108     private void startBackupThread() {
109         new Thread JavaDoc(new Runnable JavaDoc() {
110             public void run() {
111                 while(!_noMoreBackups){
112                     _backups ++;
113                     String JavaDoc fileName = backupFile(_backups);
114                     deleteFile(fileName);
115                     try {
116                         _inBackup = true;
117                         _objectContainer.ext().backup(fileName);
118                         _inBackup = false;
119                     } catch (IOException e) {
120                         e.printStackTrace();
121                     }
122                 }
123             }
124         }).start();
125     }
126    
127     private void openDatabase(){
128         deleteFile(FILE);
129         _objectContainer = Db4o.openFile(FILE);
130     }
131     
132     private void closeDatabase() throws InterruptedException JavaDoc{
133         _noMoreBackups = true;
134         while(_inBackup){
135             Thread.sleep(1000);
136         }
137         _objectContainer.close();
138     }
139     
140     private void checkBackups(){
141         stdout("BackupStressTest");
142         stdout("Backups created: " + _backups);
143         
144         for (int i = 1; i < _backups; i++) {
145             stdout("Backup " + i);
146             ObjectContainer container = Db4o.openFile(backupFile(i));
147             try {
148                 stdout("Open successful");
149                 Query q = container.query();
150                 q.constrain(BackupStressIteration.class);
151                 BackupStressIteration iteration = (BackupStressIteration) q.execute().next();
152                 
153                 int iterations = iteration.getCount();
154                 
155                 stdout("Iterations in backup: " + iterations);
156                 
157                 if(iterations > 0){
158                     q = container.query();
159                     q.constrain(BackupStressItem.class);
160                     q.descend("_iteration").constrain(new Integer JavaDoc(iteration.getCount()));
161                     ObjectSet items = q.execute();
162                     Assert.areEqual(OBJECTS, items.size());
163                     while(items.hasNext()){
164                         BackupStressItem item = (BackupStressItem) items.next();
165                         Assert.areEqual(iterations, item._iteration);
166                     }
167                 }
168             } finally {
169                 container.close();
170             }
171             stdout("Backup OK");
172         }
173         System.out.println("BackupStressTest " + _backups + " files OK.");
174         for (int i = 1; i <= _backups; i++) {
175             deleteFile(backupFile(i));
176         }
177         deleteFile(FILE);
178     }
179
180     private boolean deleteFile(String JavaDoc fname) {
181         return new File(fname).delete();
182     }
183     
184     private boolean isOldJDK(){
185         YapStream stream = (YapStream)_objectContainer;
186         return stream.needsLockFileThread();
187     }
188     
189     private String JavaDoc backupFile(int count){
190         return "" + count + FILE;
191     }
192
193     private void stdout(String JavaDoc string) {
194         if(verbose){
195             System.out.println(string);
196         }
197     }
198
199
200 }
201
Popular Tags