1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8 9package com.sleepycat.collections.test; 10 11import java.io.File; 12import java.io.IOException; 13 14import com.sleepycat.compat.DbCompat; 15import com.sleepycat.db.DatabaseException; 16import com.sleepycat.db.Environment; 17import com.sleepycat.db.EnvironmentConfig; 18import com.sleepycat.util.test.SharedTestUtils; 19 20/** 21 * @author Mark Hayes 22 */ 23public class TestEnv { 24 25 public static final TestEnv BDB; 26 public static final TestEnv CDB; 27 public static final TestEnv TXN; 28 static { 29 EnvironmentConfig config; 30 31 config = newEnvConfig(); 32 BDB = new TestEnv("bdb", config); 33 34 if (DbCompat.CDB) { 35 config = newEnvConfig(); 36 DbCompat.setInitializeCDB(config, true); 37 CDB = new TestEnv("cdb", config); 38 } else { 39 CDB = null; 40 } 41 42 config = newEnvConfig(); 43 config.setTransactional(true); 44 DbCompat.setInitializeLocking(config, true); 45 TXN = new TestEnv("txn", config); 46 } 47 48 private static EnvironmentConfig newEnvConfig() { 49 50 EnvironmentConfig config = new EnvironmentConfig(); 51 if (DbCompat.MEMORY_SUBSYSTEM) { 52 DbCompat.setInitializeCache(config, true); 53 } 54 return config; 55 } 56 57 public static final TestEnv[] ALL; 58 static { 59 if (DbCompat.CDB) { 60 ALL = new TestEnv[] { BDB, CDB, TXN }; 61 } else { 62 ALL = new TestEnv[] { BDB, TXN }; 63 } 64 } 65 66 private String name; 67 private EnvironmentConfig config; 68 69 TestEnv(String name, EnvironmentConfig config) { 70 71 this.name = name; 72 this.config = config; 73 } 74 75 public String getName() { 76 77 return name; 78 } 79 80 public boolean isTxnMode() { 81 82 return config.getTransactional(); 83 } 84 85 public boolean isCdbMode() { 86 87 return DbCompat.getInitializeCDB(config); 88 } 89 90 public Environment open(String testName) 91 throws IOException, DatabaseException { 92 93 return open(testName, true); 94 } 95 96 public Environment open(String testName, boolean create) 97 throws IOException, DatabaseException { 98 99 config.setAllowCreate(create); 100 /* OLDEST deadlock detection on DB matches the use of timeouts on JE.*/ 101 DbCompat.setLockDetectModeOldest(config); 102 File dir = getDirectory(testName, create); 103 return newEnvironment(dir, config); 104 } 105 106 /** 107 * Is overridden in XACollectionTest. 108 */ 109 protected Environment newEnvironment(File dir, EnvironmentConfig config) 110 throws DatabaseException, IOException { 111 112 return new Environment(dir, config); 113 } 114 115 public File getDirectory(String testName) 116 throws IOException { 117 118 return getDirectory(testName, true); 119 } 120 121 public File getDirectory(String testName, boolean create) 122 throws IOException { 123 124 if (create) { 125 return SharedTestUtils.getNewDir("db-test/" + testName); 126 } else { 127 return SharedTestUtils.getExistingDir("db-test/" + testName); 128 } 129 } 130} 131