001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.objectserver.persistence.sleepycat;
006:
007: import org.apache.commons.io.FileUtils;
008:
009: import com.sleepycat.je.DatabaseConfig;
010: import com.sleepycat.je.EnvironmentConfig;
011: import com.tc.logging.TCLogger;
012: import com.tc.logging.TCLogging;
013: import com.tc.test.TCTestCase;
014:
015: import java.io.File;
016: import java.io.IOException;
017: import java.util.ArrayList;
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021:
022: public class SleepycatSequenceTest extends TCTestCase {
023: private File envHome;
024: private EnvironmentConfig ecfg;
025: private DatabaseConfig dbcfg;
026: private DBEnvironment env;
027: private static int count = 0;
028:
029: protected void setUp() throws Exception {
030: super .setUp();
031: ecfg = new EnvironmentConfig();
032: ecfg.setAllowCreate(true);
033: ecfg.setReadOnly(false);
034: ecfg.setTransactional(true);
035:
036: dbcfg = new DatabaseConfig();
037: dbcfg.setAllowCreate(true);
038: dbcfg.setReadOnly(false);
039: dbcfg.setTransactional(true);
040: while ((envHome = new File(this .getTempDirectory(), ++count
041: + "")).exists()) {
042: //
043: }
044: System.out.println("DB home: " + envHome);
045: env = newEnv(true);
046:
047: }
048:
049: public void tearDown() throws Exception {
050: super .tearDown();
051: env.close();
052: envHome.delete();
053: FileUtils.cleanDirectory(envHome);
054: }
055:
056: private DBEnvironment newEnv(boolean paranoid) throws IOException {
057: return newEnv(new HashMap(), new ArrayList(), paranoid);
058: }
059:
060: private DBEnvironment newEnv(Map map, List list, boolean paranoid)
061: throws IOException {
062: return new DBEnvironment(map, list, paranoid, envHome, ecfg,
063: dbcfg);
064: }
065:
066: public void testUID() throws Exception {
067: assertTrue(env.open().isClean());
068: SleepycatPersistenceTransactionProvider persistenceTransactionProvider = new SleepycatPersistenceTransactionProvider(
069: env.getEnvironment());
070: TCLogger logger = TCLogging
071: .getLogger(SleepycatSequenceTest.class);
072: SleepycatSequence sequence = new SleepycatSequence(
073: persistenceTransactionProvider, logger, 1, 1, env
074: .getClientIDDatabase());
075: String uid1 = sequence.getUID();
076: assertNotNull(uid1);
077: System.err.println("UID is " + uid1);
078: sequence = new SleepycatSequence(
079: persistenceTransactionProvider, logger, 1, 1, env
080: .getClientIDDatabase());
081: String uid2 = sequence.getUID();
082: System.err.println("UID is " + uid2);
083: assertEquals(uid1, uid2);
084: sequence = new SleepycatSequence(
085: persistenceTransactionProvider, logger, 1, 1, env
086: .getTransactionSequenceDatabase());
087: String uid3 = sequence.getUID();
088: System.err.println("UID is " + uid3);
089: assertNotEquals(uid1, uid3);
090: }
091:
092: public void testBasic() throws Exception {
093: assertTrue(env.open().isClean());
094: SleepycatPersistenceTransactionProvider persistenceTransactionProvider = new SleepycatPersistenceTransactionProvider(
095: env.getEnvironment());
096: TCLogger logger = TCLogging
097: .getLogger(SleepycatSequenceTest.class);
098: SleepycatSequence sequence = new SleepycatSequence(
099: persistenceTransactionProvider, logger, 1, 1, env
100: .getClientIDDatabase());
101: long id = sequence.next();
102: assertEquals(1, id);
103: id = sequence.nextBatch(100);
104: assertEquals(2, id);
105: id = sequence.next();
106: assertEquals(102, id);
107: id = sequence.next();
108: assertEquals(103, id);
109: id = sequence.next();
110: assertEquals(104, id);
111: sequence.setNext(1000);
112: id = sequence.next();
113: assertEquals(1000, id);
114: id = sequence.nextBatch(100);
115: assertEquals(1001, id);
116: id = sequence.nextBatch(100);
117: assertEquals(1101, id);
118: boolean failed = false;
119: try {
120: sequence.setNext(100);
121: failed = true;
122: } catch (AssertionError er) {
123: // expected
124: }
125: id = sequence.next();
126: assertEquals(1201, id);
127: if (failed) {
128: throw new AssertionError("Didn't fail");
129: }
130: }
131:
132: }
|