1 8 package com.sleepycat.collections.test.serial; 9 10 import java.io.ObjectStreamClass ; 11 import java.util.Map ; 12 13 import junit.framework.Test; 14 import junit.framework.TestCase; 15 import junit.framework.TestSuite; 16 17 import com.sleepycat.bind.serial.SerialBinding; 18 import com.sleepycat.bind.serial.StoredClassCatalog; 19 import com.sleepycat.collections.StoredMap; 20 import com.sleepycat.collections.TransactionRunner; 21 import com.sleepycat.collections.TransactionWorker; 22 import com.sleepycat.collections.test.DbTestUtil; 23 import com.sleepycat.collections.test.TestEnv; 24 import com.sleepycat.compat.DbCompat; 25 import com.sleepycat.je.Database; 26 import com.sleepycat.je.DatabaseConfig; 27 import com.sleepycat.je.Environment; 28 29 38 public class StoredClassCatalogTest extends TestCase 39 implements TransactionWorker { 40 41 static final String CATALOG_FILE = "catalogtest-catalog.db"; 42 static final String STORE_FILE = "catalogtest-store.db"; 43 44 public static void main(String [] args) 45 throws Exception { 46 47 junit.framework.TestResult tr = 48 junit.textui.TestRunner.run(suite()); 49 if (tr.errorCount() > 0 || 50 tr.failureCount() > 0) { 51 System.exit(1); 52 } else { 53 System.exit(0); 54 } 55 } 56 57 public static Test suite() 58 throws Exception { 59 60 TestSuite suite = new TestSuite(); 61 for (int i = 0; i < TestEnv.ALL.length; i += 1) { 62 suite.addTest(new StoredClassCatalogTest(TestEnv.ALL[i])); 63 } 64 return suite; 65 } 66 67 private TestEnv testEnv; 68 private Environment env; 69 private StoredClassCatalog catalog; 70 private StoredClassCatalog catalog2; 71 private Database store; 72 private Map map; 73 private TransactionRunner runner; 74 75 public StoredClassCatalogTest(TestEnv testEnv) { 76 77 super(makeTestName(testEnv)); 78 this.testEnv = testEnv; 79 } 80 81 static String makeTestName(TestEnv testEnv) { 82 return "StoredClassCatalogTest-" + testEnv.getName(); 83 } 84 85 public void setUp() 86 throws Exception { 87 88 DbTestUtil.printTestName(getName()); 89 env = testEnv.open(makeTestName(testEnv), false); 90 runner = new TransactionRunner(env); 91 92 catalog = new StoredClassCatalog(openDb(CATALOG_FILE, false)); 93 catalog2 = new StoredClassCatalog(openDb("catalog2.db", true)); 94 95 SerialBinding keyBinding = new SerialBinding(catalog, 96 String .class); 97 SerialBinding valueBinding = new SerialBinding(catalog, 98 TestSerial.class); 99 store = openDb(STORE_FILE, false); 100 101 map = new StoredMap(store, keyBinding, valueBinding, true); 102 } 103 104 private Database openDb(String file, boolean create) 105 throws Exception { 106 107 DatabaseConfig config = new DatabaseConfig(); 108 DbCompat.setTypeBtree(config); 109 config.setTransactional(testEnv.isTxnMode()); 110 config.setAllowCreate(create); 111 112 return DbCompat.openDatabase(env, null, file, null, config); 113 } 114 115 public void tearDown() { 116 117 try { 118 if (catalog != null) { 119 catalog.close(); 120 catalog.close(); } 122 if (catalog2 != null) { 123 catalog2.close(); 124 } 125 if (store != null) { 126 store.close(); 127 } 128 if (env != null) { 129 env.close(); 130 } 131 } catch (Exception e) { 132 System.err.println("Ignored exception during tearDown: "); 133 e.printStackTrace(); 134 } finally { 135 136 catalog = null; 137 catalog2 = null; 138 store = null; 139 env = null; 140 testEnv = null; 141 map = null; 142 runner = null; 143 } 144 } 145 146 public void runTest() 147 throws Exception { 148 149 runner.run(this); 150 } 151 152 public void doWork() 153 throws Exception { 154 155 TestSerial one = (TestSerial) map.get("one"); 156 TestSerial two = (TestSerial) map.get("two"); 157 assertNotNull(one); 158 assertNotNull(two); 159 assertEquals(one, two.getOther()); 160 assertNull(one.getStringField()); 161 assertNull(two.getStringField()); 162 163 TestSerial three = new TestSerial(two); 164 assertNotNull(three.getStringField()); 165 map.put("three", three); 166 three = (TestSerial) map.get("three"); 167 assertEquals(two, three.getOther()); 168 169 ObjectStreamClass desc = ObjectStreamClass.lookup(TestSerial.class); 170 171 assertNotNull(catalog.getClassID(desc)); 172 assertNotNull(catalog.getClassID(desc)); 173 174 assertNotNull(catalog2.getClassID(desc)); 176 assertNotNull(catalog2.getClassID(desc)); 177 } 178 } 179 | Popular Tags |