001: /*
002: * $Id: TestMultipleConnections.java,v 1.2 2005/04/24 21:36:00 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002 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.jdbc;
042:
043: import java.io.File;
044: import java.sql.Connection;
045: import java.sql.DriverManager;
046: import java.sql.ResultSet;
047: import java.sql.Statement;
048:
049: import junit.framework.Test;
050: import junit.framework.TestCase;
051: import junit.framework.TestSuite;
052:
053: import org.apache.commons.collections.Bag;
054: import org.apache.commons.collections.HashBag;
055: import org.axiondb.Database;
056: import org.axiondb.jdbc.AxionConnection;
057:
058: /**
059: * @version $Revision: 1.2 $ $Date: 2005/04/24 21:36:00 $
060: * @author Rodney Waldhoff
061: */
062: public class TestMultipleConnections extends TestCase {
063: public TestMultipleConnections(String testName) {
064: super (testName);
065: }
066:
067: public static Test suite() {
068: return new TestSuite(TestMultipleConnections.class);
069: }
070:
071: //--------------------------------------------------------------- Lifecycle
072:
073: private String _name = "testdb";
074: private String _connectString = "jdbc:axiondb:diskdb:testdb";
075:
076: public void setUp() throws Exception {
077: Class.forName("org.axiondb.jdbc.AxionDriver");
078: }
079:
080: public void tearDown() throws Exception {
081: {
082: Connection conn = DriverManager
083: .getConnection(_connectString);
084: Statement stmt = conn.createStatement();
085: stmt.execute("shutdown");
086: stmt.close();
087: conn.close();
088: }
089: deleteFile(new File(_name));
090: }
091:
092: private boolean deleteFile(File file) throws Exception {
093: if (file.exists()) {
094: if (file.isDirectory()) {
095: File[] files = file.listFiles();
096: for (int i = 0; i < files.length; i++) {
097: deleteFile(files[i]);
098: }
099: }
100: if (!file.delete()) {
101: return false;
102: }
103: return true;
104: }
105: return true;
106: }
107:
108: //------------------------------------------------------------------- Tests
109:
110: public void testSameDatabase() throws Exception {
111: AxionConnection conn1 = (AxionConnection) (DriverManager
112: .getConnection(_connectString));
113: AxionConnection conn2 = (AxionConnection) (DriverManager
114: .getConnection(_connectString));
115: assertSame(conn1.getDatabase(), conn2.getDatabase());
116: }
117:
118: public void testSameDatabase2() throws Exception {
119: AxionConnection conn1 = (AxionConnection) (DriverManager
120: .getConnection(_connectString));
121: Database db = conn1.getDatabase();
122: conn1.close();
123: AxionConnection conn2 = (AxionConnection) (DriverManager
124: .getConnection(_connectString));
125: assertSame(db, conn2.getDatabase());
126: }
127:
128: public void testCreateOneSelectAnother() throws Exception {
129: Connection conn = DriverManager.getConnection(_connectString);
130: Connection conn2 = DriverManager.getConnection(_connectString);
131:
132: Statement stmt = conn.createStatement();
133: createTableFoo(stmt);
134: populateTableFoo(stmt);
135: stmt.close();
136:
137: stmt = conn2.createStatement();
138: selectFromFoo(stmt);
139: stmt.close();
140: conn.close();
141: conn2.close();
142: }
143:
144: //-------------------------------------------------------------------- Util
145:
146: private void createTableFoo(Statement stmt) throws Exception {
147: stmt
148: .execute("create table FOO ( NUM integer, STR varchar2(25), NUMTWO integer )");
149: createIndexOnFoo(stmt);
150: }
151:
152: private void createIndexOnFoo(Statement stmt) throws Exception {
153: stmt.execute("create index FOO_NUM_NDX on FOO ( NUM )");
154: }
155:
156: private void populateTableFoo(Statement stmt) throws Exception {
157: for (int i = 0; i < 30; i++) {
158: stmt
159: .execute("insert into FOO ( NUM, STR, NUMTWO ) values ( "
160: + i + ", '" + i + "', " + (i / 2) + ")");
161: }
162: }
163:
164: private void selectFromFoo(Statement stmt) throws Exception {
165: ResultSet rset = stmt.executeQuery("select STR from FOO");
166: assertNotNull("Should have been able to create ResultSet", rset);
167:
168: Bag expected = new HashBag();
169: Bag found = new HashBag();
170:
171: for (int i = 0; i < 30; i++) {
172: assertTrue("ResultSet should contain more rows", rset
173: .next());
174: expected.add(String.valueOf(i));
175: String val = rset.getString(1);
176: assertNotNull("Returned String should not be null", val);
177: assertTrue("ResultSet shouldn't think value was null",
178: !rset.wasNull());
179: assertTrue("Shouldn't have seen \"" + val + "\" yet",
180: !found.contains(val));
181: found.add(val);
182: }
183: assertTrue("ResultSet shouldn't have any more rows", !rset
184: .next());
185: rset.close();
186: assertEquals(expected, found);
187: }
188:
189: }
|