001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: TestEnv.java,v 1.34.2.2 2008/01/07 15:14:24 cwl Exp $
007: */
008:
009: package com.sleepycat.collections.test;
010:
011: import java.io.File;
012: import java.io.IOException;
013:
014: import com.sleepycat.compat.DbCompat;
015: import com.sleepycat.je.DatabaseException;
016: import com.sleepycat.je.Environment;
017: import com.sleepycat.je.EnvironmentConfig;
018:
019: /**
020: * @author Mark Hayes
021: */
022: public class TestEnv {
023:
024: public static final TestEnv BDB;
025: public static final TestEnv CDB;
026: public static final TestEnv TXN;
027: static {
028: EnvironmentConfig config;
029:
030: config = newEnvConfig();
031: BDB = new TestEnv("bdb", config);
032:
033: if (DbCompat.CDB) {
034: config = newEnvConfig();
035: DbCompat.setInitializeCDB(config, true);
036: CDB = new TestEnv("cdb", config);
037: } else {
038: CDB = null;
039: }
040:
041: config = newEnvConfig();
042: config.setTransactional(true);
043: DbCompat.setInitializeLocking(config, true);
044: TXN = new TestEnv("txn", config);
045: }
046:
047: private static EnvironmentConfig newEnvConfig() {
048:
049: EnvironmentConfig config = new EnvironmentConfig();
050: if (DbCompat.MEMORY_SUBSYSTEM) {
051: DbCompat.setInitializeCache(config, true);
052: }
053: return config;
054: }
055:
056: public static final TestEnv[] ALL;
057: static {
058: if (DbCompat.CDB) {
059: ALL = new TestEnv[] { BDB, CDB, TXN };
060: } else {
061: ALL = new TestEnv[] { BDB, TXN };
062: }
063: }
064:
065: private String name;
066: private EnvironmentConfig config;
067:
068: TestEnv(String name, EnvironmentConfig config) {
069:
070: this .name = name;
071: this .config = config;
072: }
073:
074: public String getName() {
075:
076: return name;
077: }
078:
079: public boolean isTxnMode() {
080:
081: return config.getTransactional();
082: }
083:
084: public boolean isCdbMode() {
085:
086: return DbCompat.getInitializeCDB(config);
087: }
088:
089: public Environment open(String testName) throws IOException,
090: DatabaseException {
091:
092: return open(testName, true);
093: }
094:
095: public Environment open(String testName, boolean create)
096: throws IOException, DatabaseException {
097:
098: config.setAllowCreate(create);
099: /* OLDEST deadlock detection on DB matches the use of timeouts on JE.*/
100: DbCompat.setLockDetectModeOldest(config);
101: File dir = getDirectory(testName, create);
102: return newEnvironment(dir, config);
103: }
104:
105: /**
106: * Is overridden in XACollectionTest.
107: */
108: protected Environment newEnvironment(File dir,
109: EnvironmentConfig config) throws DatabaseException,
110: IOException {
111:
112: return new Environment(dir, config);
113: }
114:
115: public File getDirectory(String testName) throws IOException {
116:
117: return getDirectory(testName, true);
118: }
119:
120: public File getDirectory(String testName, boolean create)
121: throws IOException {
122:
123: if (create) {
124: return DbTestUtil.getNewDir("db-test/" + testName);
125: } else {
126: return DbTestUtil.getExistingDir("db-test/" + testName);
127: }
128: }
129: }
|