1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2000,2008 Oracle. All rights reserved. 5 * 6 * $Id: TupleSerialFactoryTest.java,v 12.1 2008/02/07 17:12:32 mark Exp $ 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.StoredClassCatalog; 17import com.sleepycat.bind.serial.test.MarshalledObject; 18import com.sleepycat.collections.TransactionRunner; 19import com.sleepycat.collections.TransactionWorker; 20import com.sleepycat.collections.TupleSerialFactory; 21import com.sleepycat.compat.DbCompat; 22import com.sleepycat.db.Database; 23import com.sleepycat.db.DatabaseConfig; 24import com.sleepycat.db.Environment; 25import com.sleepycat.db.ForeignKeyDeleteAction; 26import com.sleepycat.db.SecondaryConfig; 27import com.sleepycat.db.SecondaryDatabase; 28import com.sleepycat.util.test.SharedTestUtils; 29import com.sleepycat.util.test.TestEnv; 30 31/** 32 * @author Mark Hayes 33 */ 34public class TupleSerialFactoryTest extends TestCase 35 implements TransactionWorker { 36 37 public static void main(String[] args) 38 throws Exception { 39 40 junit.framework.TestResult tr = 41 junit.textui.TestRunner.run(suite()); 42 if (tr.errorCount() > 0 || 43 tr.failureCount() > 0) { 44 System.exit(1); 45 } else { 46 System.exit(0); 47 } 48 } 49 50 public static Test suite() 51 throws Exception { 52 53 TestSuite suite = new TestSuite(); 54 for (int i = 0; i < TestEnv.ALL.length; i += 1) { 55 for (int sorted = 0; sorted < 2; sorted += 1) { 56 suite.addTest(new TupleSerialFactoryTest(TestEnv.ALL[i], 57 sorted != 0)); 58 } 59 } 60 return suite; 61 } 62 63 private TestEnv testEnv; 64 private Environment env; 65 private StoredClassCatalog catalog; 66 private TransactionRunner runner; 67 private TupleSerialFactory factory; 68 private Database store1; 69 private Database store2; 70 private SecondaryDatabase index1; 71 private SecondaryDatabase index2; 72 private boolean isSorted; 73 private Map storeMap1; 74 private Map storeMap2; 75 private Map indexMap1; 76 private Map indexMap2; 77 78 public TupleSerialFactoryTest(TestEnv testEnv, boolean isSorted) { 79 80 super(null); 81 82 this.testEnv = testEnv; 83 this.isSorted = isSorted; 84 85 String name = "TupleSerialFactoryTest-" + testEnv.getName(); 86 name += isSorted ? "-sorted" : "-unsorted"; 87 setName(name); 88 } 89 90 public void setUp() 91 throws Exception { 92 93 SharedTestUtils.printTestName(getName()); 94 env = testEnv.open(getName()); 95 runner = new TransactionRunner(env); 96 97 createDatabase(); 98 } 99 100 public void tearDown() { 101 102 try { 103 if (index1 != null) { 104 index1.close(); 105 } 106 if (index2 != null) { 107 index2.close(); 108 } 109 if (store1 != null) { 110 store1.close(); 111 } 112 if (store2 != null) { 113 store2.close(); 114 } 115 if (catalog != null) { 116 catalog.close(); 117 } 118 if (env != null) { 119 env.close(); 120 } 121 } catch (Exception e) { 122 System.out.println("Ignored exception during tearDown: " + e); 123 } finally { 124 /* Ensure that GC can cleanup. */ 125 index1 = null; 126 index2 = null; 127 store1 = null; 128 store2 = null; 129 catalog = null; 130 env = null; 131 testEnv = null; 132 runner = null; 133 factory = null; 134 storeMap1 = null; 135 storeMap2 = null; 136 indexMap1 = null; 137 indexMap2 = null; 138 } 139 } 140 141 public void runTest() 142 throws Exception { 143 144 runner.run(this); 145 } 146 147 public void doWork() 148 throws Exception { 149 150 createViews(); 151 writeAndRead(); 152 } 153 154 private void createDatabase() 155 throws Exception { 156 157 catalog = new StoredClassCatalog(openDb("catalog.db")); 158 factory = new TupleSerialFactory(catalog); 159 assertSame(catalog, factory.getCatalog()); 160 161 store1 = openDb("store1.db"); 162 store2 = openDb("store2.db"); 163 index1 = openSecondaryDb(factory, "1", store1, "index1.db", null); 164 index2 = openSecondaryDb(factory, "2", store2, "index2.db", store1); 165 } 166 167 private Database openDb(String file) 168 throws Exception { 169 170 DatabaseConfig config = new DatabaseConfig(); 171 config.setTransactional(testEnv.isTxnMode()); 172 config.setAllowCreate(true); 173 DbCompat.setTypeBtree(config); 174 175 return DbCompat.testOpenDatabase(env, null, file, null, config); 176 } 177 178 private SecondaryDatabase openSecondaryDb(TupleSerialFactory factory, 179 String keyName, 180 Database primary, 181 String file, 182 Database foreignStore) 183 throws Exception { 184 185 SecondaryConfig secConfig = new SecondaryConfig(); 186 secConfig.setTransactional(testEnv.isTxnMode()); 187 secConfig.setAllowCreate(true); 188 DbCompat.setTypeBtree(secConfig); 189 secConfig.setKeyCreator(factory.getKeyCreator(MarshalledObject.class, 190 keyName)); 191 if (foreignStore != null) { 192 secConfig.setForeignKeyDatabase(foreignStore); 193 secConfig.setForeignKeyDeleteAction( 194 ForeignKeyDeleteAction.CASCADE); 195 } 196 197 return DbCompat.testOpenSecondaryDatabase 198 (env, null, file, null, primary, secConfig); 199 } 200 201 private void createViews() 202 throws Exception { 203 204 if (isSorted) { 205 storeMap1 = factory.newSortedMap(store1, String.class, 206 MarshalledObject.class, true); 207 storeMap2 = factory.newSortedMap(store2, String.class, 208 MarshalledObject.class, true); 209 indexMap1 = factory.newSortedMap(index1, String.class, 210 MarshalledObject.class, true); 211 indexMap2 = factory.newSortedMap(index2, String.class, 212 MarshalledObject.class, true); 213 } else { 214 storeMap1 = factory.newMap(store1, String.class, 215 MarshalledObject.class, true); 216 storeMap2 = factory.newMap(store2, String.class, 217 MarshalledObject.class, true); 218 indexMap1 = factory.newMap(index1, String.class, 219 MarshalledObject.class, true); 220 indexMap2 = factory.newMap(index2, String.class, 221 MarshalledObject.class, true); 222 } 223 } 224 225 private void writeAndRead() 226 throws Exception { 227 228 MarshalledObject o1 = new MarshalledObject("data1", "pk1", "ik1", ""); 229 assertNull(storeMap1.put(null, o1)); 230 231 assertEquals(o1, storeMap1.get("pk1")); 232 assertEquals(o1, indexMap1.get("ik1")); 233 234 MarshalledObject o2 = new MarshalledObject("data2", "pk2", "", "pk1"); 235 assertNull(storeMap2.put(null, o2)); 236 237 assertEquals(o2, storeMap2.get("pk2")); 238 assertEquals(o2, indexMap2.get("pk1")); 239 240 /* 241 * store1 contains o1 with primary key "pk1" and index key "ik1" 242 * store2 contains o2 with primary key "pk2" and foreign key "pk1" 243 * which is the primary key of store1 244 */ 245 246 storeMap1.remove("pk1"); 247 assertNull(storeMap1.get("pk1")); 248 assertNull(indexMap1.get("ik1")); 249 assertNull(storeMap2.get("pk2")); 250 assertNull(indexMap2.get("pk1")); 251 } 252} 253