001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.persistable;
022:
023: import java.sql.*;
024: import java.io.*;
025: import java.util.*;
026: import junit.framework.*;
027: import com.methodhead.test.*;
028: import org.apache.commons.dbcp.*;
029: import org.apache.log4j.*;
030:
031: public class ConnectionSingletonTest extends TestCase {
032:
033: Properties dbProps1_ = null;
034: Properties dbProps2_ = null;
035: Properties mysqlProps_ = null;
036: Properties psqlProps_ = null;
037: Properties sqlserverProps_ = null;
038:
039: //
040: // uncomment this section if you want log output on stdout
041: //
042: // static {
043: // BasicConfigurator.configure( new WriterAppender() );
044: //
045: // BasicConfigurator.configure(
046: // new WriterAppender( new SimpleLayout(), System.out ) );
047: // }
048: //
049:
050: public ConnectionSingletonTest(String name) {
051: super (name);
052:
053: }
054:
055: protected void setUp() throws Exception {
056:
057: InputStream in = new FileInputStream("db.properties");
058: dbProps1_ = new Properties();
059: dbProps1_.load(in);
060: in.close();
061:
062: in = new FileInputStream("db2.properties");
063: dbProps2_ = new Properties();
064: dbProps2_.load(in);
065: in.close();
066:
067: in = new FileInputStream("psql.properties");
068: psqlProps_ = new Properties();
069: psqlProps_.load(in);
070: in.close();
071:
072: in = new FileInputStream("mysql.properties");
073: mysqlProps_ = new Properties();
074: mysqlProps_.load(in);
075: in.close();
076:
077: in = new FileInputStream("sqlserver.properties");
078: sqlserverProps_ = new Properties();
079: sqlserverProps_.load(in);
080: in.close();
081:
082: try {
083: ConnectionSingleton.release();
084: ConnectionSingleton.release("test");
085: } catch (Exception e) {
086: }
087: }
088:
089: protected void tearDown() {
090: }
091:
092: public void testInit() {
093: ResultSet rs = null;
094: try {
095: assertTrue(ConnectionSingleton.init(dbProps1_));
096: assertEquals(1, ConnectionSingleton.connections_.size());
097: assertTrue(!ConnectionSingleton.init(dbProps2_));
098:
099: PoolingDataSource dataSource1 = (PoolingDataSource) ConnectionSingleton.connections_
100: .get("");
101:
102: assertNotNull(dataSource1);
103: assertNotNull(dataSource1.getConnection());
104: assertTrue(ConnectionSingleton.init("test", dbProps2_));
105: assertEquals(2, ConnectionSingleton.connections_.size());
106:
107: PoolingDataSource dataSource2 = (PoolingDataSource) ConnectionSingleton.connections_
108: .get("test");
109:
110: assertTrue(dataSource1 != dataSource2);
111: assertNotNull(dataSource2);
112: assertNotNull(dataSource2.getConnection());
113: } catch (SQLException e) {
114: ConnectionSingleton.close(rs);
115: fail(e.getMessage());
116: }
117: }
118:
119: public void testRelease() {
120: try {
121: assertTrue(ConnectionSingleton.init(dbProps1_));
122: assertTrue(ConnectionSingleton.init("test", dbProps1_));
123: assertNotNull(ConnectionSingleton.getConnection());
124: assertNotNull(ConnectionSingleton.getConnection("test"));
125: assertEquals(2, ConnectionSingleton.connections_.size());
126:
127: ConnectionSingleton.release();
128:
129: assertEquals(1, ConnectionSingleton.connections_.size());
130:
131: ConnectionSingleton.release("test");
132:
133: assertEquals(0, ConnectionSingleton.connections_.size());
134: } catch (Exception e) {
135: fail(e.toString());
136: }
137: }
138:
139: public void testGetConnection() {
140: try {
141: assertTrue(ConnectionSingleton.init(dbProps1_));
142: assertTrue(ConnectionSingleton.init("test", dbProps2_));
143:
144: Connection conn1 = ConnectionSingleton.getConnection();
145: Connection conn2 = ConnectionSingleton
146: .getConnection("test");
147:
148: assertNotNull(conn1);
149: assertNotNull(conn2);
150:
151: conn1.prepareStatement("CREATE TABLE test (id INT)")
152: .executeUpdate();
153: conn1.close();
154: conn2.prepareStatement("CREATE TABLE test (id INT)")
155: .executeUpdate();
156: conn2.close();
157:
158: for (int i = 0; i < 10; i++) {
159: conn1 = ConnectionSingleton.getConnection();
160:
161: assertEquals(1, conn1.prepareStatement(
162: "INSERT INTO test VALUES (" + i + ")")
163: .executeUpdate());
164:
165: conn1.close();
166: conn1 = ConnectionSingleton.getConnection();
167: ResultSet rs = conn1.prepareStatement(
168: "SELECT id FROM test WHERE id=" + i)
169: .executeQuery();
170:
171: assertNotNull(rs);
172: assertTrue(rs.next());
173: assertEquals(i, rs.getInt("id"));
174:
175: conn1.close();
176: }
177:
178: for (int i = 0; i < 10; i++) {
179: conn2 = ConnectionSingleton.getConnection("test");
180:
181: assertEquals(1, conn2.prepareStatement(
182: "INSERT INTO test VALUES (" + i + ")")
183: .executeUpdate());
184:
185: conn2.close();
186: conn2 = ConnectionSingleton.getConnection("test");
187: ResultSet rs = conn2.prepareStatement(
188: "SELECT id FROM test WHERE id=" + i)
189: .executeQuery();
190:
191: assertNotNull(rs);
192: assertTrue(rs.next());
193: assertEquals(i, rs.getInt("id"));
194:
195: conn2.close();
196: }
197:
198: conn1 = ConnectionSingleton.getConnection();
199: conn1.prepareStatement("DROP TABLE test").executeUpdate();
200: conn1.close();
201:
202: conn2 = ConnectionSingleton.getConnection("test");
203: conn2.prepareStatement("DROP TABLE test").executeUpdate();
204: conn2.close();
205: } catch (SQLException e) {
206: fail(e.toString());
207: }
208: }
209:
210: public void testRunQuery() {
211: try {
212: assertTrue(ConnectionSingleton.init(dbProps1_));
213: assertTrue(ConnectionSingleton.init("test", dbProps2_));
214:
215: ConnectionSingleton.runUpdate("CREATE TABLE test (id INT)");
216: ConnectionSingleton
217: .runUpdate("INSERT INTO test VALUES (1)");
218: ResultSet rs = ConnectionSingleton
219: .runQuery("SELECT id FROM test");
220:
221: assertNotNull(rs);
222: assertTrue(rs.next());
223: assertEquals(1, rs.getInt("id"));
224:
225: ConnectionSingleton.close(rs);
226: ConnectionSingleton.runUpdate("test",
227: "CREATE TABLE test (id INT)");
228: ConnectionSingleton.runUpdate("test",
229: "INSERT INTO test VALUES (1)");
230: rs = ConnectionSingleton.runQuery("test",
231: "SELECT id FROM test");
232:
233: assertNotNull(rs);
234: assertTrue(rs.next());
235: assertEquals(1, rs.getInt("id"));
236:
237: ConnectionSingleton.close(rs);
238: ConnectionSingleton.runUpdate("DROP TABLE test");
239: ConnectionSingleton.runUpdate("test", "DROP TABLE test");
240: } catch (SQLException e) {
241: fail(e.toString());
242: }
243: }
244:
245: public void testRunBatch() {
246: try {
247: assertTrue(ConnectionSingleton.init(dbProps1_));
248:
249: String batch = "-- this is a comment\n"
250: + "CREATE TABLE test ( -- this is another comment\n"
251: + " id INT\n" + ");\n" + "\n" + "INSERT INTO\n"
252: + " test\n" + "VALUES (\n" + " 666\n" + ");"
253: + "-- this is a final comment\n";
254: ConnectionSingleton.runBatchUpdate(new StringReader(batch));
255: ResultSet rs = ConnectionSingleton
256: .runQuery("SELECT id FROM test;");
257:
258: assertNotNull(rs);
259: assertTrue(rs.next());
260: assertEquals(666, rs.getInt("id"));
261:
262: ConnectionSingleton.close(rs);
263: ConnectionSingleton.runUpdate("DROP TABLE test");
264: } catch (Exception e) {
265: fail(e.toString());
266: }
267: }
268:
269: public void testGetDatabaseProductName() {
270: try {
271: ConnectionSingleton.init("pool1", mysqlProps_);
272: assertEquals(ConnectionSingleton.DBTYPE_MYSQL,
273: ConnectionSingleton.getDatabaseType("pool1"));
274:
275: ConnectionSingleton.init("pool2", psqlProps_);
276: assertEquals(ConnectionSingleton.DBTYPE_PSQL,
277: ConnectionSingleton.getDatabaseType("pool2"));
278:
279: ConnectionSingleton.init("pool3", sqlserverProps_);
280: assertEquals(ConnectionSingleton.DBTYPE_SQLSERVER,
281: ConnectionSingleton.getDatabaseType("pool3"));
282: } catch (Exception e) {
283: fail(e.toString());
284: }
285: }
286: }
|