001: /*
002: * $Id: TestDatabaseLock.java,v 1.4 2005/04/09 02:04:40 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2003 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.functional;
042:
043: import java.io.File;
044: import java.sql.Connection;
045: import java.sql.DriverManager;
046: import java.sql.SQLException;
047: import java.sql.Statement;
048:
049: import javax.sql.DataSource;
050:
051: import junit.framework.Test;
052: import junit.framework.TestSuite;
053:
054: import org.axiondb.jdbc.AxionDataSource;
055:
056: /**
057: * @version $Revision: 1.4 $ $Date: 2005/04/09 02:04:40 $
058: * @author Rodney Waldhoff
059: */
060: public class TestDatabaseLock extends AbstractFunctionalTest {
061:
062: //------------------------------------------------------------ Conventional
063:
064: public TestDatabaseLock(String testName) {
065: super (testName);
066: }
067:
068: public static Test suite() {
069: return new TestSuite(TestDatabaseLock.class);
070: }
071:
072: //--------------------------------------------------------------- Lifecycle
073:
074: private String _originalValue = null;
075:
076: public void setUp() throws Exception {
077: super .setUp();
078: _originalValue = System
079: .getProperty("org.axiondb.engine.DiskDatabase.IGNORE_LOCK_FILE");
080: }
081:
082: public void tearDown() throws Exception {
083: super .tearDown();
084: if (null == _originalValue) {
085: System.getProperties().remove(
086: "org.axiondb.engine.DiskDatabase.IGNORE_LOCK_FILE");
087: } else {
088: System.setProperty(
089: "org.axiondb.engine.DiskDatabase.IGNORE_LOCK_FILE",
090: _originalValue);
091: }
092: _originalValue = null;
093: }
094:
095: //------------------------------------------------------------------- Tests
096:
097: protected File getDatabaseDirectory() {
098: return new File(new File("."), "testdb");
099: }
100:
101: public void testCantOpenDiskDatabaseTwice() throws Exception {
102: System.setProperty(
103: "org.axiondb.engine.DiskDatabase.IGNORE_LOCK_FILE",
104: "false");
105:
106: Connection conn = DriverManager
107: .getConnection("jdbc:axiondb:testdb:testdb");
108: assertNotNull(conn);
109:
110: Thread.sleep(50L); // give the OS a chance to catch up if needed
111:
112: try {
113: DriverManager.getConnection("jdbc:axiondb:testdb2:testdb");
114: fail("Expected SQLException");
115: } catch (SQLException e) {
116: // expected
117: }
118:
119: DataSource ds = new AxionDataSource(
120: "jdbc:axiondb:testdb2:testdb");
121: try {
122: ds.getConnection();
123: fail("Expected SQLException");
124: } catch (SQLException e) {
125: // expected
126: }
127:
128: }
129:
130: public void testCanOpenDiskDatabaseTwiceWhenOverridePropertyIsSet()
131: throws Exception {
132: System.setProperty(
133: "org.axiondb.engine.DiskDatabase.IGNORE_LOCK_FILE",
134: "true");
135:
136: try {
137: Connection conn = DriverManager
138: .getConnection("jdbc:axiondb:testdb:testdb");
139: assertNotNull(conn);
140:
141: Connection conn2 = DriverManager
142: .getConnection("jdbc:axiondb:testdb2:testdb");
143: assertNotNull(conn2);
144:
145: Statement stmt = conn.createStatement();
146: stmt.execute("shutdown");
147: stmt.close();
148: conn.close();
149:
150: Statement stmt2 = conn2.createStatement();
151: stmt2.execute("shutdown");
152: stmt2.close();
153: conn2.close();
154: } finally {
155: System.setProperty(
156: "org.axiondb.engine.DiskDatabase.IGNORE_LOCK_FILE",
157: "false");
158: }
159: }
160:
161: }
|