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

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

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