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