• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.9.5/BerkeleyDB-21/db/test/scr024/src/com/sleepycat/collections/test/serial/
1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2000,2008 Oracle.  All rights reserved.
5 *
6 * $Id: StoredClassCatalogTestInit.java,v 12.8 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.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        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        SharedTestUtils.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.testOpenDatabase(env, null, file, null, config);
105    }
106
107    public void tearDown() {
108
109        try {
110            if (catalog != null) {
111                catalog.close();
112                catalog.close(); // should have no effect
113            }
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            /* Ensure that GC can cleanup. */
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