1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002,2008 Oracle. All rights reserved. 5 * 6 * $Id: TestSR15721.java,v 12.2 2008/02/08 20:12:37 mark Exp $ 7 */ 8 9package com.sleepycat.collections.test; 10 11import junit.framework.Test; 12import junit.framework.TestCase; 13import junit.framework.TestSuite; 14 15import com.sleepycat.collections.CurrentTransaction; 16import com.sleepycat.db.Environment; 17import com.sleepycat.util.test.TestEnv; 18 19/** 20 * @author Chao Huang 21 */ 22public class TestSR15721 extends TestCase { 23 24 /** 25 * Runs a command line collection test. 26 * @see #usage 27 */ 28 public static void main(String[] args) 29 throws Exception { 30 31 if (args.length == 1 && 32 (args[0].equals("-h") || args[0].equals("-help"))) { 33 usage(); 34 } else { 35 junit.framework.TestResult tr = 36 junit.textui.TestRunner.run(suite()); 37 if (tr.errorCount() > 0 || 38 tr.failureCount() > 0) { 39 System.exit(1); 40 } else { 41 System.exit(0); 42 } 43 } 44 } 45 46 private static void usage() { 47 48 System.out.println( 49 "Usage: java com.sleepycat.collections.test.TestSR15721" 50 + " [-h | -help]\n"); 51 System.exit(2); 52 } 53 54 public static Test suite() 55 throws Exception { 56 57 TestSuite suite = new TestSuite(TestSR15721.class); 58 return suite; 59 } 60 61 private Environment env; 62 private CurrentTransaction currentTxn; 63 64 public void setUp() 65 throws Exception { 66 67 env = TestEnv.TXN.open("TestSR15721"); 68 currentTxn = CurrentTransaction.getInstance(env); 69 } 70 71 public void tearDown() { 72 try { 73 if (env != null) { 74 env.close(); 75 } 76 } catch (Exception e) { 77 System.out.println("Ignored exception during tearDown: " + e); 78 } finally { 79 /* Ensure that GC can cleanup. */ 80 env = null; 81 currentTxn = null; 82 } 83 } 84 85 /** 86 * Tests that the CurrentTransaction instance doesn't indeed allow GC to 87 * reclaim while attached environment is open. [#15721] 88 */ 89 public void testSR15721Fix() 90 throws Exception { 91 92 int hash = currentTxn.hashCode(); 93 int hash2 = -1; 94 95 currentTxn = CurrentTransaction.getInstance(env); 96 hash2 = currentTxn.hashCode(); 97 assertTrue(hash == hash2); 98 99 currentTxn.beginTransaction(null); 100 currentTxn = null; 101 hash2 = -1; 102 103 for (int i = 0; i < 10; i += 1) { 104 byte[] x = null; 105 try { 106 x = new byte[Integer.MAX_VALUE - 1]; 107 fail(); 108 } catch (OutOfMemoryError expected) { 109 } 110 assertNull(x); 111 112 System.gc(); 113 } 114 115 currentTxn = CurrentTransaction.getInstance(env); 116 hash2 = currentTxn.hashCode(); 117 currentTxn.commitTransaction(); 118 119 assertTrue(hash == hash2); 120 } 121} 122