KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > persistence > sleepycat > SleepycatMapStoreTest


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.objectserver.persistence.sleepycat;
6
7 import com.sleepycat.je.DatabaseConfig;
8 import com.sleepycat.je.EnvironmentConfig;
9 import com.tc.logging.TCLogger;
10 import com.tc.logging.TCLogging;
11 import com.tc.test.TCTestCase;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 public class SleepycatMapStoreTest extends TCTestCase {
21
22   private File JavaDoc envHome;
23   private EnvironmentConfig ecfg;
24   private DatabaseConfig dbcfg;
25   private static int count = 0;
26
27   protected void setUp() throws Exception JavaDoc {
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 JavaDoc(this.getTempDirectory(), ++count + "")).exists()) {
39       //
40
}
41     System.out.println("DB home: " + envHome);
42
43   }
44
45   private DBEnvironment newEnv(boolean paranoid) throws IOException JavaDoc {
46     return newEnv(new HashMap JavaDoc(), new ArrayList JavaDoc(), paranoid);
47   }
48
49   private DBEnvironment newEnv(Map JavaDoc map, List JavaDoc list, boolean paranoid) throws IOException JavaDoc {
50     return new DBEnvironment(map, list, paranoid, envHome, ecfg, dbcfg);
51   }
52
53   public void testBasic() throws Exception JavaDoc {
54     DBEnvironment env = newEnv(true);
55     assertTrue(env.open().isClean());
56     SleepycatPersistenceTransactionProvider persistenceTransactionProvider = new SleepycatPersistenceTransactionProvider(
57                                                                                                                          env
58                                                                                                                              .getEnvironment());
59     TCLogger logger = TCLogging.getLogger(SleepycatSequenceTest.class);
60
61     SleepycatMapStore mapStore = new SleepycatMapStore(persistenceTransactionProvider, logger, env
62         .getClusterStateStoreDatabase());
63     String JavaDoc val = mapStore.get("Hello");
64     assertNull(val);
65     mapStore.put("Hello", "Saro");
66     val = mapStore.get("Hello");
67     assertEquals("Saro", val);
68     boolean removed = mapStore.remove("Hello");
69     assertTrue(removed);
70     val = mapStore.get("Hello");
71     assertNull(val);
72
73     for (int i = 0; i < 1000; i++) {
74       mapStore.put("day" + i, "cya" + i);
75     }
76
77     removed = mapStore.remove("Day1"); // not found
78
assertFalse(removed);
79
80     for (int i = 0; i < 1000; i++) {
81       val = mapStore.get("day" + i);
82       assertEquals("cya" + i, val);
83       removed = mapStore.remove("day" + i);
84       assertTrue(removed);
85     }
86
87     for (int i = 0; i < 1000; i++) {
88       val = mapStore.get("day" + i);
89       assertNull(val);
90       removed = mapStore.remove("day" + i);
91       assertFalse(removed);
92     }
93   }
94
95 }
96
Popular Tags