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: import com.db4o.defragment.*;
027: import com.db4o.ext.*;
028: import com.db4o.foundation.*;
029: import com.db4o.foundation.io.*;
030: import com.db4o.query.*;
031: import com.db4o.tools.*;
032:
033: public class Test extends AllTests {
034:
035: private static final boolean USE_NEW_DEFRAGMENT = true;
036:
037: private static ObjectServer objectServer;
038: private static ExtObjectContainer oc;
039: private static ExtObjectContainer _replica;
040:
041: static AllTests currentRunner;
042: public static boolean clientServer = true;
043: static boolean runServer = true;
044: static int errorCount = 0;
045: public static int assertionCount = 0;
046: static int run;
047:
048: static MemoryFile memoryFile;
049: static byte[] memoryFileContent;
050:
051: public static final boolean COMPARE_INTERNAL_OK = false;
052:
053: public static boolean canCheckFileSize() {
054: if (Deploy.debug) {
055: return false;
056: }
057: if (currentRunner != null) {
058: if (!MEMORY_FILE) {
059: return !clientServer || !currentRunner.REMOTE_SERVER;
060: }
061: }
062: return false;
063: }
064:
065: public static void beginTesting() {
066: File file = new File(BLOB_PATH);
067: file.mkdirs();
068: if (!file.exists()) {
069: System.out.println("Unable to create blob directory: "
070: + BLOB_PATH);
071: }
072: }
073:
074: private static Class classOf(Object obj) {
075: if (obj == null) {
076: return null;
077: }
078: if (obj instanceof Class) {
079: return (Class) obj;
080: }
081: return obj.getClass();
082: }
083:
084: public static void close() {
085: if (null != oc) {
086: while (!oc.close()) {
087: }
088: oc = null;
089: }
090: if (memoryFile != null) {
091: memoryFileContent = memoryFile.getBytes();
092: }
093: if (_replica != null) {
094: while (!_replica.close()) {
095: }
096: _replica = null;
097: }
098: }
099:
100: public static void commit() {
101: oc.commit();
102: }
103:
104: public static void configureMessageLevel() {
105: Db4o.configure().messageLevel(-1);
106: }
107:
108: public static ObjectServer currentServer() {
109: if (clientServer && runServer) {
110: return objectServer;
111: }
112: return null;
113: }
114:
115: public static void defragment() {
116: String fileName = FILE_SOLO;
117: close();
118: if (isClientServer()) {
119: server().close();
120: fileName = FILE_SERVER;
121: objectServer = null;
122: }
123: try {
124: if (MEMORY_FILE) {
125: File4.delete(fileName);
126: RandomAccessFile raf = new RandomAccessFile(fileName,
127: "rw");
128: raf.write(memoryFileContent);
129: raf.close();
130: }
131:
132: if (USE_NEW_DEFRAGMENT) {
133: String targetFile = fileName + ".defrag.backup";
134: DefragmentConfig defragConfig = new DefragmentConfig(
135: fileName, targetFile);
136: defragConfig.forceBackupDelete(true);
137: com.db4o.defragment.Defragment.defrag(defragConfig);
138: } else {
139:
140: new com.db4o.tools.Defragment().run(fileName, true);
141:
142: }
143:
144: if (MEMORY_FILE) {
145: RandomAccessFile raf = new RandomAccessFile(fileName,
146: "rw");
147: memoryFileContent = new byte[(int) raf.length()];
148: raf.read(memoryFileContent);
149: raf.close();
150: }
151:
152: } catch (Exception e) {
153: e.printStackTrace();
154: }
155: open();
156: }
157:
158: public static void delete() {
159: new File(FILE_SOLO).delete();
160: new File(FILE_SERVER).delete();
161: new File(replicatedFileName(false)).delete();
162: new File(replicatedFileName(true)).delete();
163: }
164:
165: public static void delete(Object obj) {
166: objectContainer().delete(obj);
167: }
168:
169: public static void deleteAll(ObjectContainer container) {
170: deleteObjectSet(container, container.get(null));
171: }
172:
173: public static void deleteObjectSet(ObjectContainer container,
174: ObjectSet all) {
175: while (all.hasNext()) {
176: container.delete(all.next());
177: }
178: }
179:
180: public static void deleteAllInstances(Object obj) {
181: try {
182: Query q = oc.query();
183: q.constrain(classOf(obj));
184: deleteObjectSet(oc, q.execute());
185: } catch (Exception e) {
186: e.printStackTrace();
187: }
188: }
189:
190: public static void end() {
191: if (oc != null) {
192: while (!oc.close()) {
193: }
194: }
195: if (objectServer != null) {
196: Cool.sleepIgnoringInterruption(1000);
197: objectServer.close();
198: objectServer = null;
199: }
200: }
201:
202: public static boolean ensure(boolean condition, String msg) {
203: assertionCount++;
204: if (!condition) {
205: error(msg);
206: return false;
207: }
208: return true;
209: }
210:
211: public static boolean ensure(boolean condition) {
212: return ensure(condition, null);
213: }
214:
215: public static boolean ensureEquals(Object exp, Object actual) {
216: return ensureEquals(exp, actual, null);
217: }
218:
219: public static boolean ensureEquals(Object exp, Object actual,
220: String msg) {
221: assertionCount++;
222: if (!exp.equals(actual)) {
223: String errMsg = "Expected " + exp + " but was " + actual;
224: if (msg != null) {
225: errMsg = msg + "\n" + errMsg;
226: }
227: error(errMsg);
228: return false;
229: }
230: return true;
231: }
232:
233: public static boolean ensureEquals(int exp, int actual) {
234: return ensureEquals(exp, actual, null);
235: }
236:
237: public static boolean ensureEquals(int exp, int actual, String msg) {
238: return ensureEquals(new Integer(exp), new Integer(actual), msg);
239: }
240:
241: public static void ensureOccurrences(Object obj, int count) {
242: assertionCount++;
243: int occ = occurrences(obj);
244: if (occ != count) {
245: error("Expected count: " + count + " Count was:" + occ);
246: }
247: }
248:
249: public static void error(String msg) {
250: errorCount++;
251: if (msg != null) {
252: new Exception(msg).printStackTrace();
253: } else {
254: new Exception().printStackTrace();
255: }
256: }
257:
258: public static void error() {
259: error(null);
260: }
261:
262: public static int fileLength() {
263: String fileName = clientServer ? FILE_SERVER : FILE_SOLO;
264: return (int) new File(fileName).length();
265: }
266:
267: public static void forEach(Object obj, Visitor4 vis) {
268: ObjectContainer con = objectContainer();
269: con.deactivate(obj, Integer.MAX_VALUE);
270: ObjectSet set = oc.get(obj);
271: while (set.hasNext()) {
272: vis.visit(set.next());
273: }
274: }
275:
276: public static Object getOne(Object obj) {
277: Query q = oc.query();
278: q.constrain(classOf(obj));
279: ObjectSet set = q.execute();
280: if (set.size() != 1) {
281: error();
282: }
283: return set.next();
284: }
285:
286: public static boolean isClientServer() {
287: return currentServer() != null;
288: }
289:
290: public static void log(Query q) {
291: ObjectSet set = q.execute();
292: while (set.hasNext()) {
293: Logger.log(oc, set.next());
294: }
295: }
296:
297: public static void logAll() {
298: ObjectSet set = oc.get(null);
299: while (set.hasNext()) {
300: Logger.log(oc, set.next());
301: }
302: }
303:
304: public static ExtObjectContainer objectContainer() {
305: if (oc == null) {
306: open();
307: }
308: return oc;
309: }
310:
311: public static int occurrences(Object obj) {
312: Query q = oc.query();
313: q.constrain(classOf(obj));
314: return q.execute().size();
315: }
316:
317: public static ExtObjectContainer open() {
318: if (runServer && clientServer && objectServer == null) {
319: objectServer = Db4o.openServer(FILE_SERVER, SERVER_PORT);
320:
321: // Null can happen, for EncryptionWrongPassword
322: if (objectServer != null) {
323: objectServer.grantAccess(DB4O_USER, DB4O_PASSWORD);
324: objectServer.ext().configure().messageLevel(0);
325: } else {
326: throw new RuntimeException("Couldn't open server.");
327: }
328: }
329: if (clientServer) {
330: oc = openClient();
331: } else {
332: if (MEMORY_FILE) {
333: memoryFile = new MemoryFile(memoryFileContent);
334: oc = ExtDb4o.openMemoryFile(memoryFile).ext();
335: } else {
336: oc = Db4o.openFile(FILE_SOLO).ext();
337: }
338: }
339: return oc;
340: }
341:
342: public static ExtObjectContainer openClient() {
343: if (clientServer) {
344: try {
345:
346: if (EMBEDDED_CLIENT) {
347: return objectServer.openClient().ext();
348: }
349:
350: return Db4o.openClient(SERVER_HOSTNAME, SERVER_PORT,
351: DB4O_USER, DB4O_PASSWORD).ext();
352: // oc = objectServer.openClient().ext();
353: } catch (Exception e) {
354: e.printStackTrace();
355: }
356: }
357: return null;
358: }
359:
360: public static Query query() {
361: return objectContainer().query();
362: }
363:
364: public static ObjectContainer reOpen() {
365: close();
366: return open();
367: }
368:
369: public static ObjectContainer reOpenServer() {
370: if (runServer && clientServer) {
371: close();
372: if (objectServer != null) {
373: objectServer.close();
374: objectServer = null;
375: }
376: Cool.sleepIgnoringInterruption(500);
377: return open();
378: } else {
379: return reOpen();
380: }
381: }
382:
383: public static ExtObjectContainer replica() {
384: if (_replica != null) {
385: while (!_replica.close())
386: ;
387: }
388: _replica = Db4o.openFile(replicatedFileName(isClientServer()))
389: .ext();
390: return _replica;
391: }
392:
393: private static String replicatedFileName(boolean clientServer) {
394: if (clientServer) {
395: return "replicated_" + FILE_SERVER;
396: }
397: return "replicated_" + FILE_SOLO;
398:
399: }
400:
401: public static void rollBack() {
402: objectContainer().rollback();
403: }
404:
405: public static ObjectServer server() {
406: return objectServer;
407: }
408:
409: public static void store(Object obj) {
410: objectContainer().set(obj);
411: }
412:
413: public static void statistics() {
414: Statistics.main(new String[] { FILE_SOLO });
415: }
416:
417: public static void commitSync(ExtObjectContainer client1,
418: ExtObjectContainer client2) {
419: client1.setSemaphore("sem", 0);
420: client1.commit();
421: client1.releaseSemaphore("sem");
422: ensure(client2.setSemaphore("sem", 5000));
423: client2.releaseSemaphore("sem");
424: }
425:
426: }
|