1 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 ; 16 import java.io.IOException ; 17 import java.util.ArrayList ; 18 import java.util.HashMap ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 public class SleepycatSequenceTest extends TCTestCase { 23 private File 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 { 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 (this.getTempDirectory(), ++count + "")).exists()) { 41 } 43 System.out.println("DB home: " + envHome); 44 env = newEnv(true); 45 46 } 47 48 public void tearDown() throws Exception { 49 super.tearDown(); 50 env.close(); 51 envHome.delete(); 52 FileUtils.cleanDirectory(envHome); 53 } 54 55 private DBEnvironment newEnv(boolean paranoid) throws IOException { 56 return newEnv(new HashMap (), new ArrayList (), paranoid); 57 } 58 59 private DBEnvironment newEnv(Map map, List list, boolean paranoid) throws IOException { 60 return new DBEnvironment(map, list, paranoid, envHome, ecfg, dbcfg); 61 } 62 63 public void testUID() throws Exception { 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 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 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 uid3 = sequence.getUID(); 80 System.err.println("UID is " + uid3); 81 assertNotEquals(uid1, uid3); 82 } 83 84 public void testBasic() throws Exception { 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 er) { 114 } 116 id = sequence.next(); 117 assertEquals(1201, id); 118 if (failed) { throw new AssertionError ("Didn't fail"); } 119 } 120 121 } 122 | Popular Tags |