1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2000-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8package com.sleepycat.collections.test.serial; 9 10import junit.framework.Test; 11import junit.framework.TestCase; 12import junit.framework.TestSuite; 13 14import com.sleepycat.bind.serial.StoredClassCatalog; 15import com.sleepycat.compat.DbCompat; 16import com.sleepycat.db.Database; 17import com.sleepycat.db.DatabaseConfig; 18import com.sleepycat.db.Environment; 19import com.sleepycat.util.test.SharedTestUtils; 20import com.sleepycat.util.test.TestEnv; 21 22/** 23 * @author Mark Hayes 24 */ 25public class CatalogCornerCaseTest extends TestCase { 26 27 public static void main(String[] args) { 28 junit.framework.TestResult tr = 29 junit.textui.TestRunner.run(suite()); 30 if (tr.errorCount() > 0 || 31 tr.failureCount() > 0) { 32 System.exit(1); 33 } else { 34 System.exit(0); 35 } 36 } 37 38 public static Test suite() { 39 return new TestSuite(CatalogCornerCaseTest.class); 40 } 41 42 private Environment env; 43 44 public CatalogCornerCaseTest(String name) { 45 46 super(name); 47 } 48 49 @Override 50 public void setUp() 51 throws Exception { 52 53 SharedTestUtils.printTestName(getName()); 54 env = TestEnv.BDB.open(getName()); 55 } 56 57 @Override 58 public void tearDown() { 59 60 try { 61 if (env != null) { 62 env.close(); 63 } 64 } catch (Exception e) { 65 System.out.println("Ignored exception during tearDown: " + e); 66 } finally { 67 /* Ensure that GC can cleanup. */ 68 env = null; 69 } 70 } 71 72 public void testReadOnlyEmptyCatalog() 73 throws Exception { 74 75 String file = "catalog.db"; 76 77 /* Create an empty database. */ 78 DatabaseConfig config = new DatabaseConfig(); 79 config.setAllowCreate(true); 80 DbCompat.setTypeBtree(config); 81 Database db = 82 DbCompat.testOpenDatabase(env, null, file, null, config); 83 db.close(); 84 85 /* Open the empty database read-only. */ 86 config.setAllowCreate(false); 87 config.setReadOnly(true); 88 db = DbCompat.testOpenDatabase(env, null, file, null, config); 89 90 /* Expect exception when creating the catalog. */ 91 try { 92 new StoredClassCatalog(db); 93 fail(); 94 } catch (IllegalStateException e) { } 95 db.close(); 96 } 97} 98