001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: TestSR15721.java,v 1.1.2.5 2008/01/07 15:14:24 cwl Exp $
007: */
008:
009: package com.sleepycat.collections.test;
010:
011: import java.io.File;
012: import java.util.Properties;
013:
014: import junit.framework.Test;
015: import junit.framework.TestCase;
016: import junit.framework.TestSuite;
017:
018: import com.sleepycat.collections.CurrentTransaction;
019: import com.sleepycat.je.Environment;
020: import com.sleepycat.je.EnvironmentConfig;
021:
022: /**
023: * @author Chao Huang
024: */
025: public class TestSR15721 extends TestCase {
026:
027: /**
028: * Runs a command line collection test.
029: * @see #usage
030: */
031: public static void main(String[] args) throws Exception {
032:
033: if (args.length == 1
034: && (args[0].equals("-h") || args[0].equals("-help"))) {
035: usage();
036: } else {
037: junit.framework.TestResult tr = junit.textui.TestRunner
038: .run(suite());
039: if (tr.errorCount() > 0 || tr.failureCount() > 0) {
040: System.exit(1);
041: } else {
042: System.exit(0);
043: }
044: }
045: }
046:
047: private static void usage() {
048:
049: System.out
050: .println("Usage: java com.sleepycat.collections.test.TestSR15721"
051: + " [-h | -help]\n");
052: System.exit(2);
053: }
054:
055: public static Test suite() throws Exception {
056:
057: TestSuite suite = new TestSuite(TestSR15721.class);
058: return suite;
059: }
060:
061: private Environment env;
062: private CurrentTransaction currentTxn;
063:
064: public void setUp() throws Exception {
065:
066: File dir = DbTestUtil.getNewDir();
067: Properties p = new Properties();
068: p.setProperty("je.env.isTransactional", "true");
069: p.setProperty("je.env.isLocking", "true");
070: p.setProperty("je.env.isReadOnly", "false");
071: p.setProperty("je.env.recovery", "true");
072:
073: EnvironmentConfig envConfig = new EnvironmentConfig(p);
074: envConfig.setAllowCreate(true);
075:
076: env = new Environment(dir, envConfig);
077: currentTxn = CurrentTransaction.getInstance(env);
078: }
079:
080: public void tearDown() {
081:
082: try {
083: if (env != null) {
084: env.close();
085: }
086: } catch (Exception e) {
087: System.out.println("Ignored exception during tearDown: "
088: + e);
089: } finally {
090: /* Ensure that GC can cleanup. */
091: env = null;
092: currentTxn = null;
093: }
094: }
095:
096: /**
097: * Tests that the CurrentTransaction instance doesn't indeed allow GC to
098: * reclaim while attached environment is open. [#15721]
099: */
100: public void testSR15721Fix() throws Exception {
101:
102: int hash = currentTxn.hashCode();
103: int hash2 = -1;
104:
105: currentTxn = CurrentTransaction.getInstance(env);
106: hash2 = currentTxn.hashCode();
107: assertTrue(hash == hash2);
108:
109: currentTxn.beginTransaction(null);
110: currentTxn = null;
111: hash2 = -1;
112:
113: for (int i = 0; i < 10; i += 1) {
114: byte[] x = null;
115: try {
116: x = new byte[Integer.MAX_VALUE - 1];
117: fail();
118: } catch (OutOfMemoryError expected) {
119: }
120: assertNull(x);
121:
122: System.gc();
123: }
124:
125: currentTxn = CurrentTransaction.getInstance(env);
126: hash2 = currentTxn.hashCode();
127: currentTxn.commitTransaction();
128:
129: assertTrue(hash == hash2);
130: }
131: }
|