KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > persistable > ConnectionSingletonTest


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.persistable;
22
23 import java.sql.*;
24 import java.io.*;
25 import java.util.*;
26 import junit.framework.*;
27 import com.methodhead.test.*;
28 import org.apache.commons.dbcp.*;
29 import org.apache.log4j.*;
30
31 public class ConnectionSingletonTest extends TestCase {
32
33   Properties dbProps1_ = null;
34   Properties dbProps2_ = null;
35   Properties mysqlProps_ = null;
36   Properties psqlProps_ = null;
37   Properties sqlserverProps_ = null;
38
39   //
40
// uncomment this section if you want log output on stdout
41
//
42
// static {
43
// BasicConfigurator.configure( new WriterAppender() );
44
//
45
// BasicConfigurator.configure(
46
// new WriterAppender( new SimpleLayout(), System.out ) );
47
// }
48
//
49

50   public ConnectionSingletonTest( String JavaDoc name ) {
51     super( name );
52
53   }
54
55   protected void setUp()
56   throws
57     Exception JavaDoc {
58
59     InputStream in = new FileInputStream( "db.properties" );
60     dbProps1_ = new Properties();
61     dbProps1_.load( in );
62     in.close();
63
64     in = new FileInputStream( "db2.properties" );
65     dbProps2_ = new Properties();
66     dbProps2_.load( in );
67     in.close();
68
69     in = new FileInputStream( "psql.properties" );
70     psqlProps_ = new Properties();
71     psqlProps_.load( in );
72     in.close();
73
74     in = new FileInputStream( "mysql.properties" );
75     mysqlProps_ = new Properties();
76     mysqlProps_.load( in );
77     in.close();
78
79     in = new FileInputStream( "sqlserver.properties" );
80     sqlserverProps_ = new Properties();
81     sqlserverProps_.load( in );
82     in.close();
83
84     try {
85       ConnectionSingleton.release();
86       ConnectionSingleton.release( "test" );
87     }
88     catch( Exception JavaDoc e ) {
89     }
90   }
91
92   protected void tearDown() {
93   }
94
95   public void testInit() {
96     ResultSet rs = null;
97     try {
98       assertTrue( ConnectionSingleton.init( dbProps1_ ) );
99       assertEquals( 1, ConnectionSingleton.connections_.size() );
100       assertTrue( !ConnectionSingleton.init( dbProps2_ ) );
101
102       PoolingDataSource dataSource1 = ( PoolingDataSource )ConnectionSingleton.connections_.get( "" );
103
104       assertNotNull( dataSource1 );
105       assertNotNull( dataSource1.getConnection() );
106       assertTrue( ConnectionSingleton.init( "test", dbProps2_ ) );
107       assertEquals( 2, ConnectionSingleton.connections_.size() );
108
109       PoolingDataSource dataSource2 = ( PoolingDataSource )ConnectionSingleton.connections_.get( "test" );
110
111       assertTrue( dataSource1 != dataSource2 );
112       assertNotNull( dataSource2 );
113       assertNotNull( dataSource2.getConnection() );
114     }
115     catch ( SQLException e ) {
116       ConnectionSingleton.close( rs );
117       fail( e.getMessage() );
118     }
119   }
120
121   public void testRelease() {
122     try {
123       assertTrue( ConnectionSingleton.init( dbProps1_ ) );
124       assertTrue( ConnectionSingleton.init( "test", dbProps1_ ) );
125       assertNotNull( ConnectionSingleton.getConnection() );
126       assertNotNull( ConnectionSingleton.getConnection( "test" ) );
127       assertEquals( 2, ConnectionSingleton.connections_.size() );
128
129       ConnectionSingleton.release();
130
131       assertEquals( 1, ConnectionSingleton.connections_.size() );
132
133       ConnectionSingleton.release( "test" );
134
135       assertEquals( 0, ConnectionSingleton.connections_.size() );
136     }
137     catch ( Exception JavaDoc e ) {
138       fail( e.toString() );
139     }
140   }
141
142   public void testGetConnection() {
143     try {
144       assertTrue( ConnectionSingleton.init( dbProps1_ ) );
145       assertTrue( ConnectionSingleton.init( "test", dbProps2_ ) );
146
147       Connection conn1 = ConnectionSingleton.getConnection();
148       Connection conn2 = ConnectionSingleton.getConnection( "test" );
149
150       assertNotNull( conn1 );
151       assertNotNull( conn2 );
152
153       conn1.prepareStatement( "CREATE TABLE test (id INT)" ).executeUpdate();
154       conn1.close();
155       conn2.prepareStatement( "CREATE TABLE test (id INT)" ).executeUpdate();
156       conn2.close();
157
158       for ( int i = 0; i < 10; i++ ) {
159         conn1 = ConnectionSingleton.getConnection();
160
161         assertEquals( 1, conn1.prepareStatement( "INSERT INTO test VALUES (" + i + ")" ).executeUpdate() );
162
163         conn1.close();
164         conn1 = ConnectionSingleton.getConnection();
165         ResultSet rs = conn1.prepareStatement( "SELECT id FROM test WHERE id=" + i ).executeQuery();
166
167         assertNotNull( rs );
168         assertTrue( rs.next() );
169         assertEquals( i, rs.getInt( "id" ) );
170
171         conn1.close();
172       }
173
174       for ( int i = 0; i < 10; i++ ) {
175         conn2 = ConnectionSingleton.getConnection( "test" );
176
177         assertEquals( 1, conn2.prepareStatement( "INSERT INTO test VALUES (" + i + ")" ).executeUpdate() );
178
179         conn2.close();
180         conn2 = ConnectionSingleton.getConnection( "test" );
181         ResultSet rs = conn2.prepareStatement( "SELECT id FROM test WHERE id=" + i ).executeQuery();
182
183         assertNotNull( rs );
184         assertTrue( rs.next() );
185         assertEquals( i, rs.getInt( "id" ) );
186
187         conn2.close();
188       }
189
190       conn1 = ConnectionSingleton.getConnection();
191       conn1.prepareStatement( "DROP TABLE test" ).executeUpdate();
192       conn1.close();
193
194       conn2 = ConnectionSingleton.getConnection( "test" );
195       conn2.prepareStatement( "DROP TABLE test" ).executeUpdate();
196       conn2.close();
197     }
198     catch ( SQLException e ) {
199       fail( e.toString() );
200     }
201   }
202
203   public void testRunQuery() {
204     try {
205       assertTrue( ConnectionSingleton.init( dbProps1_ ) );
206       assertTrue( ConnectionSingleton.init( "test", dbProps2_ ) );
207
208       ConnectionSingleton.runUpdate( "CREATE TABLE test (id INT)" );
209       ConnectionSingleton.runUpdate( "INSERT INTO test VALUES (1)" );
210       ResultSet rs = ConnectionSingleton.runQuery( "SELECT id FROM test" );
211
212       assertNotNull( rs );
213       assertTrue( rs.next() );
214       assertEquals( 1, rs.getInt( "id" ) );
215
216       ConnectionSingleton.close( rs );
217       ConnectionSingleton.runUpdate( "test", "CREATE TABLE test (id INT)" );
218       ConnectionSingleton.runUpdate( "test", "INSERT INTO test VALUES (1)" );
219       rs = ConnectionSingleton.runQuery( "test", "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( "DROP TABLE test" );
227       ConnectionSingleton.runUpdate( "test", "DROP TABLE test" );
228     }
229     catch ( SQLException e ) {
230       fail( e.toString() );
231     }
232   }
233
234   public void testRunBatch() {
235     try {
236       assertTrue( ConnectionSingleton.init( dbProps1_ ) );
237
238       String JavaDoc batch =
239         "-- this is a comment\n" +
240         "CREATE TABLE test ( -- this is another comment\n" +
241         " id INT\n" +
242         ");\n" +
243         "\n" +
244         "INSERT INTO\n" +
245         " test\n" +
246         "VALUES (\n" +
247         " 666\n" +
248         ");" +
249         "-- this is a final comment\n";
250       ConnectionSingleton.runBatchUpdate( new StringReader( batch ) );
251       ResultSet rs = ConnectionSingleton.runQuery( "SELECT id FROM test;" );
252
253       assertNotNull( rs );
254       assertTrue( rs.next() );
255       assertEquals( 666, rs.getInt( "id" ) );
256
257       ConnectionSingleton.close( rs );
258       ConnectionSingleton.runUpdate( "DROP TABLE test" );
259     }
260     catch ( Exception JavaDoc e ) {
261       fail( e.toString() );
262     }
263   }
264
265   public void testGetDatabaseProductName() {
266     try {
267       ConnectionSingleton.init( "pool1", mysqlProps_ );
268       assertEquals( ConnectionSingleton.DBTYPE_MYSQL, ConnectionSingleton.getDatabaseType( "pool1" ) );
269
270       ConnectionSingleton.init( "pool2", psqlProps_ );
271       assertEquals( ConnectionSingleton.DBTYPE_PSQL, ConnectionSingleton.getDatabaseType( "pool2" ) );
272
273       ConnectionSingleton.init( "pool3", sqlserverProps_ );
274       assertEquals( ConnectionSingleton.DBTYPE_SQLSERVER, ConnectionSingleton.getDatabaseType( "pool3" ) );
275     }
276     catch ( Exception JavaDoc e ) {
277       fail( e.toString() );
278     }
279   }
280 }
281
Popular Tags