KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * All content copyright (c) 2003-2006 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 org.apache.commons.io.FileUtils;
8
9 import com.sleepycat.je.DatabaseConfig;
10 import com.sleepycat.je.EnvironmentConfig;
11 import com.tc.logging.TCLogger;
12 import com.tc.logging.TCLogging;
13 import com.tc.test.TCTestCase;
14
15 import java.io.File JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 public class SleepycatSequenceTest extends TCTestCase {
23   private File JavaDoc envHome;
24   private EnvironmentConfig ecfg;
25   private DatabaseConfig dbcfg;
26   private DBEnvironment env;
27   private static int count = 0;
28
29   protected void setUp() throws Exception JavaDoc {
30     super.setUp();
31     ecfg = new EnvironmentConfig();
32     ecfg.setAllowCreate(true);
33     ecfg.setReadOnly(false);
34     ecfg.setTransactional(true);
35
36     dbcfg = new DatabaseConfig();
37     dbcfg.setAllowCreate(true);
38     dbcfg.setReadOnly(false);
39     dbcfg.setTransactional(true);
40     while ((envHome = new File JavaDoc(this.getTempDirectory(), ++count + "")).exists()) {
41       //
42
}
43     System.out.println("DB home: " + envHome);
44     env = newEnv(true);
45
46   }
47
48   public void tearDown() throws Exception JavaDoc {
49     super.tearDown();
50     env.close();
51     envHome.delete();
52     FileUtils.cleanDirectory(envHome);
53   }
54
55   private DBEnvironment newEnv(boolean paranoid) throws IOException JavaDoc {
56     return newEnv(new HashMap JavaDoc(), new ArrayList JavaDoc(), paranoid);
57   }
58
59   private DBEnvironment newEnv(Map JavaDoc map, List JavaDoc list, boolean paranoid) throws IOException JavaDoc {
60     return new DBEnvironment(map, list, paranoid, envHome, ecfg, dbcfg);
61   }
62
63   public void testUID() throws Exception JavaDoc {
64     assertTrue(env.open().isClean());
65     SleepycatPersistenceTransactionProvider persistenceTransactionProvider = new SleepycatPersistenceTransactionProvider(
66                                                                                                                          env
67                                                                                                                              .getEnvironment());
68     TCLogger logger = TCLogging.getLogger(SleepycatSequenceTest.class);
69     SleepycatSequence sequence = new SleepycatSequence(persistenceTransactionProvider, logger, 1, 1, env
70         .getClientIDDatabase());
71     String JavaDoc uid1 = sequence.getUID();
72     assertNotNull(uid1);
73     System.err.println("UID is " + uid1);
74     sequence = new SleepycatSequence(persistenceTransactionProvider, logger, 1, 1, env.getClientIDDatabase());
75     String JavaDoc uid2 = sequence.getUID();
76     System.err.println("UID is " + uid2);
77     assertEquals(uid1, uid2);
78     sequence = new SleepycatSequence(persistenceTransactionProvider, logger, 1, 1, env.getTransactionSequenceDatabase());
79     String JavaDoc uid3 = sequence.getUID();
80     System.err.println("UID is " + uid3);
81     assertNotEquals(uid1, uid3);
82   }
83
84   public void testBasic() throws Exception JavaDoc {
85     assertTrue(env.open().isClean());
86     SleepycatPersistenceTransactionProvider persistenceTransactionProvider = new SleepycatPersistenceTransactionProvider(
87                                                                                                                          env
88                                                                                                                              .getEnvironment());
89     TCLogger logger = TCLogging.getLogger(SleepycatSequenceTest.class);
90     SleepycatSequence sequence = new SleepycatSequence(persistenceTransactionProvider, logger, 1, 1, env
91         .getClientIDDatabase());
92     long id = sequence.next();
93     assertEquals(1, id);
94     id = sequence.nextBatch(100);
95     assertEquals(2, id);
96     id = sequence.next();
97     assertEquals(102, id);
98     id = sequence.next();
99     assertEquals(103, id);
100     id = sequence.next();
101     assertEquals(104, id);
102     sequence.setNext(1000);
103     id = sequence.next();
104     assertEquals(1000, id);
105     id = sequence.nextBatch(100);
106     assertEquals(1001, id);
107     id = sequence.nextBatch(100);
108     assertEquals(1101, id);
109     boolean failed = false;
110     try {
111       sequence.setNext(100);
112       failed = true;
113     } catch (AssertionError JavaDoc er) {
114       // expected
115
}
116     id = sequence.next();
117     assertEquals(1201, id);
118     if (failed) { throw new AssertionError JavaDoc("Didn't fail"); }
119   }
120
121 }
122
Popular Tags