• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/ap/gpl/timemachine/db-4.7.25.NC/test/scr024/src/com/sleepycat/util/test/
1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: TestEnv.java,v 12.1 2008/02/07 17:12:33 mark Exp $
7 */
8
9package com.sleepycat.util.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;
18
19/**
20 * @author Mark Hayes
21 */
22public 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        config.setTxnNoSync(Boolean.getBoolean(SharedTestUtils.NO_SYNC));
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    protected 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 EnvironmentConfig getConfig() {
81        return config;
82    }
83
84    void copyConfig(EnvironmentConfig copyToConfig) {
85        DbCompat.setInitializeCache
86            (copyToConfig, DbCompat.getInitializeCache(config));
87        DbCompat.setInitializeLocking
88            (copyToConfig, DbCompat.getInitializeLocking(config));
89        DbCompat.setInitializeCDB
90            (copyToConfig, DbCompat.getInitializeCDB(config));
91        copyToConfig.setTransactional(config.getTransactional());
92    }
93
94    public boolean isTxnMode() {
95
96        return config.getTransactional();
97    }
98
99    public boolean isCdbMode() {
100
101        return DbCompat.getInitializeCDB(config);
102    }
103
104    public Environment open(String testName)
105        throws IOException, DatabaseException {
106
107        return open(testName, true);
108    }
109
110    public Environment open(String testName, boolean create)
111        throws IOException, DatabaseException {
112
113        config.setAllowCreate(create);
114        /* OLDEST deadlock detection on DB matches the use of timeouts on JE.*/
115        DbCompat.setLockDetectModeOldest(config);
116        File dir = getDirectory(testName, create);
117        return newEnvironment(dir, config);
118    }
119
120    /**
121     * Is overridden in XACollectionTest.
122     */
123    protected Environment newEnvironment(File dir, EnvironmentConfig config)
124        throws DatabaseException, IOException {
125
126        return new Environment(dir, config);
127    }
128
129    public File getDirectory(String testName)
130        throws IOException {
131
132        return getDirectory(testName, true);
133    }
134
135    public File getDirectory(String testName, boolean create)
136        throws IOException {
137
138        if (create) {
139            return SharedTestUtils.getNewDir(testName);
140        } else {
141            return SharedTestUtils.getExistingDir(testName);
142        }
143    }
144}
145