01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.objectserver.persistence.sleepycat;
06:
07: import com.sleepycat.je.DatabaseConfig;
08: import com.sleepycat.je.EnvironmentConfig;
09: import com.tc.logging.TCLogger;
10: import com.tc.logging.TCLogging;
11: import com.tc.test.TCTestCase;
12:
13: import java.io.File;
14: import java.io.IOException;
15: import java.util.ArrayList;
16: import java.util.HashMap;
17: import java.util.List;
18: import java.util.Map;
19:
20: public class SleepycatMapStoreTest extends TCTestCase {
21:
22: private File envHome;
23: private EnvironmentConfig ecfg;
24: private DatabaseConfig dbcfg;
25: private static int count = 0;
26:
27: protected void setUp() throws Exception {
28: super .setUp();
29: ecfg = new EnvironmentConfig();
30: ecfg.setAllowCreate(true);
31: ecfg.setReadOnly(false);
32: ecfg.setTransactional(true);
33:
34: dbcfg = new DatabaseConfig();
35: dbcfg.setAllowCreate(true);
36: dbcfg.setReadOnly(false);
37: dbcfg.setTransactional(true);
38: while ((envHome = new File(this .getTempDirectory(), ++count
39: + "")).exists()) {
40: //
41: }
42: System.out.println("DB home: " + envHome);
43:
44: }
45:
46: private DBEnvironment newEnv(boolean paranoid) throws IOException {
47: return newEnv(new HashMap(), new ArrayList(), paranoid);
48: }
49:
50: private DBEnvironment newEnv(Map map, List list, boolean paranoid)
51: throws IOException {
52: return new DBEnvironment(map, list, paranoid, envHome, ecfg,
53: dbcfg);
54: }
55:
56: public void testBasic() throws Exception {
57: DBEnvironment env = newEnv(true);
58: assertTrue(env.open().isClean());
59: SleepycatPersistenceTransactionProvider persistenceTransactionProvider = new SleepycatPersistenceTransactionProvider(
60: env.getEnvironment());
61: TCLogger logger = TCLogging
62: .getLogger(SleepycatSequenceTest.class);
63:
64: SleepycatMapStore mapStore = new SleepycatMapStore(
65: persistenceTransactionProvider, logger, env
66: .getClusterStateStoreDatabase());
67: String val = mapStore.get("Hello");
68: assertNull(val);
69: mapStore.put("Hello", "Saro");
70: val = mapStore.get("Hello");
71: assertEquals("Saro", val);
72: boolean removed = mapStore.remove("Hello");
73: assertTrue(removed);
74: val = mapStore.get("Hello");
75: assertNull(val);
76:
77: for (int i = 0; i < 1000; i++) {
78: mapStore.put("day" + i, "cya" + i);
79: }
80:
81: removed = mapStore.remove("Day1"); // not found
82: assertFalse(removed);
83:
84: for (int i = 0; i < 1000; i++) {
85: val = mapStore.get("day" + i);
86: assertEquals("cya" + i, val);
87: removed = mapStore.remove("day" + i);
88: assertTrue(removed);
89: }
90:
91: for (int i = 0; i < 1000; i++) {
92: val = mapStore.get("day" + i);
93: assertNull(val);
94: removed = mapStore.remove("day" + i);
95: assertFalse(removed);
96: }
97: }
98:
99: }
|