1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2000-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8package com.sleepycat.collections.test.serial; 9 10import java.util.Map; 11 12import junit.framework.Test; 13import junit.framework.TestCase; 14import junit.framework.TestSuite; 15 16import com.sleepycat.bind.serial.SerialBinding; 17import com.sleepycat.bind.serial.StoredClassCatalog; 18import com.sleepycat.collections.StoredMap; 19import com.sleepycat.collections.TransactionRunner; 20import com.sleepycat.collections.TransactionWorker; 21import com.sleepycat.compat.DbCompat; 22import com.sleepycat.db.Database; 23import com.sleepycat.db.DatabaseConfig; 24import com.sleepycat.db.Environment; 25import com.sleepycat.util.test.SharedTestUtils; 26import com.sleepycat.util.test.TestEnv; 27 28/** 29 * Runs part one of the StoredClassCatalogTest. This part is run with the 30 * old/original version of TestSerial in the classpath. It creates a fresh 31 * environment and databases containing serialized versions of the old class. 32 * When StoredClassCatalogTest is run, it will read these objects from the 33 * database created here. 34 * 35 * @author Mark Hayes 36 */ 37public 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 junit.framework.TestResult tr = 45 junit.textui.TestRunner.run(suite()); 46 if (tr.errorCount() > 0 || 47 tr.failureCount() > 0) { 48 System.exit(1); 49 } else { 50 System.exit(0); 51 } 52 } 53 54 public static Test suite() { 55 TestSuite suite = new TestSuite(); 56 for (int i = 0; i < TestEnv.ALL.length; i += 1) { 57 suite.addTest(new StoredClassCatalogTestInit(TestEnv.ALL[i])); 58 } 59 return suite; 60 } 61 62 private TestEnv testEnv; 63 private Environment env; 64 private StoredClassCatalog catalog; 65 private Database store; 66 private Map map; 67 private TransactionRunner runner; 68 69 public StoredClassCatalogTestInit(TestEnv testEnv) { 70 71 super("StoredClassCatalogTestInit-" + testEnv.getName()); 72 this.testEnv = testEnv; 73 } 74 75 @Override 76 public void setUp() 77 throws Exception { 78 79 SharedTestUtils.printTestName(getName()); 80 env = testEnv.open(StoredClassCatalogTest.makeTestName(testEnv)); 81 runner = new TransactionRunner(env); 82 83 catalog = new StoredClassCatalog(openDb(CATALOG_FILE)); 84 85 SerialBinding keyBinding = new SerialBinding(catalog, String.class); 86 SerialBinding valueBinding = 87 new SerialBinding(catalog, TestSerial.class); 88 store = openDb(STORE_FILE); 89 90 map = new StoredMap(store, keyBinding, valueBinding, true); 91 } 92 93 private Database openDb(String file) 94 throws Exception { 95 96 DatabaseConfig config = new DatabaseConfig(); 97 DbCompat.setTypeBtree(config); 98 config.setTransactional(testEnv.isTxnMode()); 99 config.setAllowCreate(true); 100 101 return DbCompat.testOpenDatabase(env, null, file, null, config); 102 } 103 104 @Override 105 public void tearDown() { 106 107 try { 108 if (catalog != null) { 109 catalog.close(); 110 catalog.close(); // should have no effect 111 } 112 if (store != null) { 113 store.close(); 114 } 115 if (env != null) { 116 env.close(); 117 } 118 } catch (Exception e) { 119 System.err.println("Ignored exception during tearDown: "); 120 e.printStackTrace(); 121 } finally { 122 /* Ensure that GC can cleanup. */ 123 catalog = null; 124 store = null; 125 env = null; 126 testEnv = null; 127 map = null; 128 runner = null; 129 } 130 } 131 132 @Override 133 public void runTest() 134 throws Exception { 135 136 runner.run(this); 137 } 138 139 public void doWork() { 140 TestSerial one = new TestSerial(null); 141 TestSerial two = new TestSerial(one); 142 assertNull("Likely the classpath contains the wrong version of the" + 143 " TestSerial class, the 'original' version is required", 144 one.getStringField()); 145 assertNull(two.getStringField()); 146 map.put("one", one); 147 map.put("two", two); 148 one = (TestSerial) map.get("one"); 149 two = (TestSerial) map.get("two"); 150 assertEquals(one, two.getOther()); 151 assertNull(one.getStringField()); 152 assertNull(two.getStringField()); 153 } 154} 155