Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

StoredClassCatalogTestInit.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2000-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: StoredClassCatalogTestInit.java,v 12.1 2005/01/31 19:27:36 mark Exp $
00008  */
00009 package com.sleepycat.collections.test.serial;
00010 
00011 import java.util.Map;
00012 
00013 import junit.framework.Test;
00014 import junit.framework.TestCase;
00015 import junit.framework.TestSuite;
00016 
00017 import com.sleepycat.bind.serial.SerialBinding;
00018 import com.sleepycat.bind.serial.StoredClassCatalog;
00019 import com.sleepycat.collections.StoredMap;
00020 import com.sleepycat.collections.TransactionRunner;
00021 import com.sleepycat.collections.TransactionWorker;
00022 import com.sleepycat.collections.test.DbTestUtil;
00023 import com.sleepycat.collections.test.TestEnv;
00024 import com.sleepycat.compat.DbCompat;
00025 import com.sleepycat.db.Database;
00026 import com.sleepycat.db.DatabaseConfig;
00027 import com.sleepycat.db.Environment;
00028 
00038 public class StoredClassCatalogTestInit extends TestCase
00039     implements TransactionWorker {
00040 
00041     static final String CATALOG_FILE = StoredClassCatalogTest.CATALOG_FILE;
00042     static final String STORE_FILE = StoredClassCatalogTest.STORE_FILE;
00043 
00044     public static void main(String[] args)
00045         throws Exception {
00046 
00047         junit.framework.TestResult tr =
00048             junit.textui.TestRunner.run(suite());
00049         if (tr.errorCount() > 0 ||
00050             tr.failureCount() > 0) {
00051             System.exit(1);
00052         } else {
00053             System.exit(0);
00054         }
00055     }
00056 
00057     public static Test suite()
00058         throws Exception {
00059 
00060         TestSuite suite = new TestSuite();
00061         for (int i = 0; i < TestEnv.ALL.length; i += 1) {
00062             suite.addTest(new StoredClassCatalogTestInit(TestEnv.ALL[i]));
00063         }
00064         return suite;
00065     }
00066 
00067     private TestEnv testEnv;
00068     private Environment env;
00069     private StoredClassCatalog catalog;
00070     private Database store;
00071     private Map map;
00072     private TransactionRunner runner;
00073 
00074     public StoredClassCatalogTestInit(TestEnv testEnv) {
00075 
00076         super("StoredClassCatalogTestInit-" + testEnv.getName());
00077         this.testEnv = testEnv;
00078     }
00079 
00080     public void setUp()
00081         throws Exception {
00082 
00083         DbTestUtil.printTestName(getName());
00084         env = testEnv.open(StoredClassCatalogTest.makeTestName(testEnv));
00085         runner = new TransactionRunner(env);
00086 
00087         catalog = new StoredClassCatalog(openDb(CATALOG_FILE));
00088 
00089         SerialBinding keyBinding = new SerialBinding(catalog,
00090                                                   String.class);
00091         SerialBinding valueBinding = new SerialBinding(catalog,
00092                                                     TestSerial.class);
00093         store = openDb(STORE_FILE);
00094 
00095         map = new StoredMap(store, keyBinding, valueBinding, true);
00096     }
00097 
00098     private Database openDb(String file)
00099         throws Exception {
00100 
00101         DatabaseConfig config = new DatabaseConfig();
00102         DbCompat.setTypeBtree(config);
00103         config.setTransactional(testEnv.isTxnMode());
00104         config.setAllowCreate(true);
00105 
00106         return DbCompat.openDatabase(env, null, file, null, config);
00107     }
00108 
00109     public void tearDown() {
00110 
00111         try {
00112             if (catalog != null) {
00113                 catalog.close();
00114                 catalog.close(); // should have no effect
00115             }
00116             if (store != null) {
00117                 store.close();
00118             }
00119             if (env != null) {
00120                 env.close();
00121             }
00122         } catch (Exception e) {
00123             System.err.println("Ignored exception during tearDown: ");
00124             e.printStackTrace();
00125         } finally {
00126             /* Ensure that GC can cleanup. */
00127             catalog = null;
00128             store = null;
00129             env = null;
00130             testEnv = null;
00131             map = null;
00132             runner = null;
00133         }
00134     }
00135 
00136     public void runTest()
00137         throws Exception {
00138 
00139         runner.run(this);
00140     }
00141 
00142     public void doWork()
00143         throws Exception {
00144 
00145         TestSerial one = new TestSerial(null);
00146         TestSerial two = new TestSerial(one);
00147         assertNull("Likely the classpath contains the wrong version of the" +
00148                    " TestSerial class, the 'original' version is required",
00149                    one.getStringField());
00150         assertNull(two.getStringField());
00151         map.put("one", one);
00152         map.put("two", two);
00153         one = (TestSerial) map.get("one");
00154         two = (TestSerial) map.get("two");
00155         assertEquals(one, two.getOther());
00156         assertNull(one.getStringField());
00157         assertNull(two.getStringField());
00158     }
00159 }

Generated on Sun Dec 25 12:14:55 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2