KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > ProxyStatementTest


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import java.sql.CallableStatement JavaDoc;
9 import java.sql.Connection JavaDoc;
10 import java.sql.DriverManager JavaDoc;
11 import java.sql.PreparedStatement JavaDoc;
12 import java.sql.Statement JavaDoc;
13 import java.util.Properties JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18 /**
19  * Test whether ProxyStatement works
20  *
21  * @version $Revision: 1.13 $, $Date: 2006/03/03 09:58:26 $
22  * @author bill
23  * @author $Author: billhorsman $ (current maintainer)
24  * @since Proxool 0.8
25  */

26 public class ProxyStatementTest extends AbstractProxoolTest {
27
28     private static final Log LOG = LogFactory.getLog(ProxyStatementTest.class);
29
30     public ProxyStatementTest(String JavaDoc alias) {
31         super(alias);
32     }
33
34     /**
35      * That we can get the delegate driver's Statement from the one given by Proxool
36      */

37     public void testDelegateStatement() throws Exception JavaDoc
38     {
39         String JavaDoc testName = "delegateStatement";
40
41         // Register pool
42
String JavaDoc url = registerPool(testName);
43
44         // get a connection from the pool and create a statement with it
45
Connection JavaDoc c = DriverManager.getConnection(url);
46         Statement JavaDoc s = c.createStatement();
47         Statement JavaDoc delegateStatement = ProxoolFacade.getDelegateStatement(s);
48         Class JavaDoc delegateStatementClass = delegateStatement.getClass();
49         s.close();
50         
51         LOG.debug("Statement " + s.getClass() + " is delegating to " + delegateStatementClass);
52         
53         // get a *real* connection directly from the native driver (bypassing the pool)
54
Connection JavaDoc realConnection = TestHelper.getDirectConnection();
55         Statement JavaDoc realStatement = realConnection.createStatement();
56         Class JavaDoc realStatementClass = realStatement.getClass();
57         
58         realStatement.close();
59         realConnection.close();
60
61         // are they of the same type ?
62
assertEquals("Delegate statement isn't of the expected type.", realStatementClass, delegateStatementClass);
63     }
64
65     
66     /**
67      * Test what interfaces are supported when getting a PreparedStatement
68      */

69     public void testPreparedStatement() throws Exception JavaDoc {
70
71         String JavaDoc testName = "preparedStatement";
72
73         // Register pool
74
String JavaDoc url = registerPool(testName);
75
76         // get a connection from the pool and create a prepare statement with it
77
Connection JavaDoc c = DriverManager.getConnection(url);
78         PreparedStatement JavaDoc s = c.prepareStatement(TestConstants.HYPERSONIC_TEST_SQL);
79         Statement JavaDoc delegateStatement = ProxoolFacade.getDelegateStatement(s);
80         Class JavaDoc delegateStatementClass = delegateStatement.getClass();
81         s.close();
82         c.close();
83
84         LOG.debug("Statement " + s.getClass() + " is delegating to " + delegateStatementClass);
85         
86         // get a *real* connection directly from the native driver (bypassing the pool)
87
Connection JavaDoc realConnection = TestHelper.getDirectConnection();
88         Statement JavaDoc realStatement = realConnection.prepareStatement(TestConstants.HYPERSONIC_TEST_SQL);
89         Class JavaDoc realStatementClass = realStatement.getClass();
90         
91         realStatement.close();
92         realConnection.close();
93         
94         // are they of the same type ?
95
assertEquals("Delegate statement isn't of the expected type.", realStatementClass, delegateStatementClass);
96     }
97
98     
99     /**
100      * Test what interfaces are supported when getting a CallableStatement
101      */

102     public void testCallableStatement() throws Exception JavaDoc {
103
104         String JavaDoc testName = "callableStatement";
105
106         // Register pool
107
String JavaDoc url = registerPool(testName);
108
109         // get a connection from the pool and create a callable statement with it
110
Connection JavaDoc c = DriverManager.getConnection(url);
111         CallableStatement JavaDoc s = c.prepareCall(TestConstants.HYPERSONIC_TEST_SQL);
112         Statement JavaDoc delegateStatement = ProxoolFacade.getDelegateStatement(s);
113         Class JavaDoc delegateStatementClass = delegateStatement.getClass();
114         s.close();
115         
116         LOG.debug("Statement " + s.getClass() + " is delegating to " + delegateStatementClass);
117         
118         // get a *real* connection directly from the native driver (bypassing the pool)
119
Connection JavaDoc realConnection = TestHelper.getDirectConnection();
120         Statement JavaDoc realStatement = realConnection.prepareCall(TestConstants.HYPERSONIC_TEST_SQL);
121         Class JavaDoc realStatementClass = realStatement.getClass();
122         
123         realStatement.close();
124         realConnection.close();
125         
126         // are they of the same type ?
127
assertEquals("Delegate statement isn't of the expected type.", realStatementClass, delegateStatementClass );
128     }
129
130
131     /**
132      * That we can get the delegate driver's Connection from the one given by Proxool
133      */

134     public void testDelegateConnection() throws Exception JavaDoc {
135
136         String JavaDoc testName = "delegateConnection";
137
138         // Register pool
139
String JavaDoc url = registerPool(testName);
140
141         // get a connection from the pool
142
Connection JavaDoc c = DriverManager.getConnection(url);
143         Connection JavaDoc delegateConnection = ProxoolFacade.getDelegateConnection(c);
144         Class JavaDoc delegateConnectionClass = delegateConnection.getClass();
145         
146         LOG.debug("Connection " + c + " is delegating to " + delegateConnectionClass);
147         c.close();
148         
149         // get a *real* connection directly from the native driver (bypassing the pool)
150
Connection JavaDoc realConnection = TestHelper.getDirectConnection();
151         Class JavaDoc realConnectionClass = realConnection.getClass();
152         realConnection.close();
153
154         assertEquals("Connection isn't of the expected.", realConnectionClass, delegateConnectionClass);
155     }
156     
157     
158     /**
159      *
160      * @param alias
161      * @throws Exception
162      */

163     private String JavaDoc registerPool(String JavaDoc alias) throws Exception JavaDoc
164     {
165         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
166                 TestConstants.HYPERSONIC_DRIVER,
167                 TestConstants.HYPERSONIC_TEST_URL);
168         Properties JavaDoc info = new Properties JavaDoc();
169         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
170         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
171         ProxoolFacade.registerConnectionPool(url, info);
172         
173         return url;
174     }
175
176     public void testCloseByStatement() throws Exception JavaDoc {
177
178         String JavaDoc testName = "snapshot";
179         final String JavaDoc alias = testName;
180         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
181                 TestConstants.HYPERSONIC_DRIVER,
182                 TestConstants.HYPERSONIC_TEST_URL);
183         Properties JavaDoc info = new Properties JavaDoc();
184         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
185         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
186         ProxoolFacade.registerConnectionPool(url, info);
187
188         // get a connection from the pool and create a statement with it
189
Connection JavaDoc c = DriverManager.getConnection(url);
190         Statement JavaDoc s = c.createStatement();
191         // Even if we ask the statement to close it it should still work.
192
s.getConnection().close();
193         assertEquals("servedCount", 0, ProxoolFacade.getSnapshot(alias).getActiveConnectionCount());
194
195     }
196 }
197
198
199 /*
200  Revision history:
201  $Log: ProxyStatementTest.java,v $
202  Revision 1.13 2006/03/03 09:58:26 billhorsman
203  Fix for statement.getConnection(). See bug 1149834.
204
205  Revision 1.12 2006/01/18 14:40:06 billhorsman
206  Unbundled Jakarta's Commons Logging.
207
208  Revision 1.11 2004/07/13 21:32:41 billhorsman
209  Close the first connection first before opening the real connection (directly) otherwise you get a "database already in use"error on Windows.
210
211  Revision 1.10 2004/05/26 17:19:09 brenuart
212  Allow JUnit tests to be executed against another database.
213  By default the test configuration will be taken from the 'testconfig-hsqldb.properties' file located in the org.logicalcobwebs.proxool package.
214  This behavior can be overriden by setting the 'testConfig' environment property to another location.
215
216  Revision 1.9 2004/03/23 21:17:23 billhorsman
217  added preparedStatement and callableStatement tests
218
219  Revision 1.8 2003/12/09 18:52:19 billhorsman
220  checkstyle
221
222  Revision 1.7 2003/11/05 00:00:52 billhorsman
223  Remove redundant test (already in FatalSqlExceptionTest)
224
225  Revision 1.6 2003/08/27 18:03:20 billhorsman
226  added new getDelegateConnection() method
227
228  Revision 1.5 2003/03/04 10:24:40 billhorsman
229  removed try blocks around each test
230
231  Revision 1.4 2003/03/03 17:09:05 billhorsman
232  all tests now extend AbstractProxoolTest
233
234  Revision 1.3 2003/03/03 11:12:05 billhorsman
235  fixed licence
236
237  Revision 1.2 2003/03/01 15:27:24 billhorsman
238  checkstyle
239
240  Revision 1.1 2003/02/27 18:01:48 billhorsman
241  completely rethought the test structure. it's now
242  more obvious. no new tests yet though.
243
244  */
Popular Tags