KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > collections > test > TestEnv


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: TestEnv.java,v 1.34 2006/10/30 21:14:39 bostic Exp $
7  */

8
9 package com.sleepycat.collections.test;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 import com.sleepycat.compat.DbCompat;
15 import com.sleepycat.je.DatabaseException;
16 import com.sleepycat.je.Environment;
17 import com.sleepycat.je.EnvironmentConfig;
18
19 /**
20  * @author Mark Hayes
21  */

22 public class TestEnv {
23
24     public static final TestEnv BDB;
25     public static final TestEnv CDB;
26     public static final TestEnv TXN;
27     static {
28         EnvironmentConfig config;
29
30         config = newEnvConfig();
31         BDB = new TestEnv("bdb", config);
32
33         if (DbCompat.CDB) {
34             config = newEnvConfig();
35             DbCompat.setInitializeCDB(config, true);
36             CDB = new TestEnv("cdb", config);
37         } else {
38             CDB = null;
39         }
40
41         config = newEnvConfig();
42         config.setTransactional(true);
43         DbCompat.setInitializeLocking(config, true);
44         TXN = new TestEnv("txn", config);
45     }
46
47     private static EnvironmentConfig newEnvConfig() {
48
49         EnvironmentConfig config = new EnvironmentConfig();
50         if (DbCompat.MEMORY_SUBSYSTEM) {
51             DbCompat.setInitializeCache(config, true);
52         }
53         return config;
54     }
55
56     public static final TestEnv[] ALL;
57     static {
58         if (DbCompat.CDB) {
59             ALL = new TestEnv[] { BDB, CDB, TXN };
60         } else {
61             ALL = new TestEnv[] { BDB, TXN };
62         }
63     }
64
65     private String JavaDoc name;
66     private EnvironmentConfig config;
67
68     TestEnv(String JavaDoc name, EnvironmentConfig config) {
69
70         this.name = name;
71         this.config = config;
72     }
73
74     public String JavaDoc getName() {
75
76         return name;
77     }
78
79     public boolean isTxnMode() {
80
81         return config.getTransactional();
82     }
83
84     public boolean isCdbMode() {
85
86         return DbCompat.getInitializeCDB(config);
87     }
88
89     public Environment open(String JavaDoc testName)
90         throws IOException JavaDoc, DatabaseException {
91
92         return open(testName, true);
93     }
94
95     public Environment open(String JavaDoc testName, boolean create)
96         throws IOException JavaDoc, DatabaseException {
97
98         config.setAllowCreate(create);
99         /* OLDEST deadlock detection on DB matches the use of timeouts on JE.*/
100         DbCompat.setLockDetectModeOldest(config);
101         File JavaDoc dir = getDirectory(testName, create);
102         return newEnvironment(dir, config);
103     }
104
105     /**
106      * Is overridden in XACollectionTest.
107      */

108     protected Environment newEnvironment(File JavaDoc dir, EnvironmentConfig config)
109         throws DatabaseException, IOException JavaDoc {
110
111         return new Environment(dir, config);
112     }
113
114     public File JavaDoc getDirectory(String JavaDoc testName)
115         throws IOException JavaDoc {
116
117         return getDirectory(testName, true);
118     }
119
120     public File JavaDoc getDirectory(String JavaDoc testName, boolean create)
121         throws IOException JavaDoc {
122
123         if (create) {
124             return DbTestUtil.getNewDir("db-test/" + testName);
125         } else {
126             return DbTestUtil.getExistingDir("db-test/" + testName);
127         }
128     }
129 }
130
Popular Tags