Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

TestEnv.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2002-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: TestEnv.java,v 12.2 2005/08/01 20:25:31 mark Exp $
00008  */
00009 
00010 package com.sleepycat.collections.test;
00011 
00012 import java.io.File;
00013 import java.io.IOException;
00014 
00015 import com.sleepycat.compat.DbCompat;
00016 import com.sleepycat.db.DatabaseException;
00017 import com.sleepycat.db.Environment;
00018 import com.sleepycat.db.EnvironmentConfig;
00019 
00023 public class TestEnv {
00024 
00025     public static final TestEnv BDB;
00026     public static final TestEnv CDB;
00027     public static final TestEnv TXN;
00028     static {
00029         EnvironmentConfig config;
00030 
00031         config = newEnvConfig();
00032         BDB = new TestEnv("bdb", config);
00033 
00034         if (DbCompat.CDB) {
00035             config = newEnvConfig();
00036             DbCompat.setInitializeCDB(config, true);
00037             CDB = new TestEnv("cdb", config);
00038         } else {
00039             CDB = null;
00040         }
00041 
00042         config = newEnvConfig();
00043         config.setTransactional(true);
00044         DbCompat.setInitializeLocking(config, true);
00045         TXN = new TestEnv("txn", config);
00046     }
00047 
00048     private static EnvironmentConfig newEnvConfig() {
00049 
00050         EnvironmentConfig config = new EnvironmentConfig();
00051         if (DbCompat.MEMORY_SUBSYSTEM) {
00052             DbCompat.setInitializeCache(config, true);
00053         }
00054         return config;
00055     }
00056 
00057     public static final TestEnv[] ALL;
00058     static {
00059         if (DbCompat.CDB) {
00060             ALL = new TestEnv[] { BDB, CDB, TXN };
00061         } else {
00062             ALL = new TestEnv[] { BDB, TXN };
00063         }
00064     }
00065 
00066     private String name;
00067     private EnvironmentConfig config;
00068 
00069     TestEnv(String name, EnvironmentConfig config) {
00070 
00071         this.name = name;
00072         this.config = config;
00073     }
00074 
00075     public String getName() {
00076 
00077         return name;
00078     }
00079 
00080     public boolean isTxnMode() {
00081 
00082         return config.getTransactional();
00083     }
00084 
00085     public boolean isCdbMode() {
00086 
00087         return DbCompat.getInitializeCDB(config);
00088     }
00089 
00090     public Environment open(String testName)
00091         throws IOException, DatabaseException {
00092 
00093         return open(testName, true);
00094     }
00095 
00096     public Environment open(String testName, boolean create)
00097         throws IOException, DatabaseException {
00098 
00099         config.setAllowCreate(create);
00100         /* OLDEST deadlock detection on DB matches the use of timeouts on JE.*/
00101         DbCompat.setLockDetectModeOldest(config);
00102         File dir = getDirectory(testName, create);
00103         return newEnvironment(dir, config);
00104     }
00105 
00109     protected Environment newEnvironment(File dir, EnvironmentConfig config)
00110         throws DatabaseException, IOException {
00111 
00112         return new Environment(dir, config);
00113     }
00114 
00115     public File getDirectory(String testName)
00116         throws IOException {
00117 
00118         return getDirectory(testName, true);
00119     }
00120 
00121     public File getDirectory(String testName, boolean create)
00122         throws IOException {
00123 
00124         if (create) {
00125             return DbTestUtil.getNewDir("db-test/" + testName);
00126         } else {
00127             return DbTestUtil.getExistingDir("db-test/" + testName);
00128         }
00129     }
00130 }

Generated on Sun Dec 25 12:14:56 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2