1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.gargoylesoftware.base.resource.jdbc;
39
40 import com.gargoylesoftware.base.util.ProxyUtil;
41 import java.sql.Connection;
42 import java.sql.ResultSet;
43 import java.sql.Statement;
44 import junit.framework.Test;
45 import junit.framework.TestCase;
46 import junit.framework.TestSuite;
47
48 /***
49 * Tests for StatementWrapper
50 *
51 * @version $Revision: 1.6 $
52 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
53 */
54 public class StatementWrapperTest extends TestCase {
55
56 /***
57 * Create a test
58 *
59 * @param name The name of the test
60 */
61 public StatementWrapperTest( final String name ) {
62 super( name );
63 }
64
65
66 /***
67 * Return a test suite containing all the tests for this class
68 *
69 * @return The test suite
70 */
71 public static Test suite() {
72 final String excludedMethods[] = {
73 "close", "getConnection"
74 };
75 final String wrappedMethods[] = {
76 "executeQuery", "getResultSet", "getConnection"
77 };
78 final TestSuite suite = WrapperTestCase.getWrapperTestSuite(
79 Statement.class, StatementWrapper.class, excludedMethods, wrappedMethods );
80
81 suite.addTest( new TestSuite( StatementWrapperTest.class ) );
82
83 return suite;
84 }
85
86
87 /***
88 * Test the constructor with a null statement
89 */
90 public void testConstructor_NullStatement() {
91 try {
92 new StatementWrapper( null );
93 fail( "Expected exception for null statement" );
94 }
95 catch( final NullPointerException e ) {
96
97 }
98 }
99
100
101 /***
102 * Test the constructor
103 *
104 * @exception Exception If an error occurs
105 */
106 public void testConstructor()
107 throws Exception {
108 final Statement statement = (Statement)ProxyUtil.createProxy( Statement.class );
109 final StatementWrapper wrapper = new StatementWrapper( statement );
110
111 assertSame( "delegate", statement, wrapper.getDelegate() );
112 }
113
114
115 /***
116 * Test setConnection()
117 *
118 * @exception Exception If an error occurs
119 */
120 public void testSetConnection()
121 throws Exception {
122 final Connection connection = (Connection)ProxyUtil.createProxy( Connection.class );
123 final Statement statement = (Statement)ProxyUtil.createProxy( Statement.class );
124 final StatementWrapper wrapper = new StatementWrapper( statement );
125 wrapper.setConnection( connection );
126
127 assertSame( "connection", connection, wrapper.getConnection() );
128 }
129
130
131 /***
132 * Test setConnection() with a null connection
133 */
134 public void testSetConnection_NullConnection() {
135 final StatementWrapper wrapper
136 = new StatementWrapper( (Statement)ProxyUtil.createProxy( Statement.class ) );
137 try {
138 wrapper.setConnection( null );
139 fail( "Expected exception for null connection" );
140 }
141 catch( final NullPointerException e ) {
142
143 }
144 }
145
146
147 /***
148 * Try closing the statement twice
149 *
150 * @exception Exception If an error occurs
151 */
152 public void testCloseTwice()
153 throws Exception {
154 final Statement statement = (Statement)ProxyUtil.createProxy( Statement.class );
155 final StatementWrapper wrapper = new StatementWrapper( statement );
156
157 wrapper.close();
158 try {
159 wrapper.close();
160 fail( "Expected exception when calling close() on a closed object" );
161 }
162 catch( final AlreadyClosedException e ) {
163
164 }
165 }
166
167
168 /***
169 * Test close where one of the result sets has already been closed
170 *
171 * @exception Exception If an error occurs
172 */
173 public void testCloseWhenResultSetAlreadyClosed()
174 throws Exception {
175 final ResultSet resultSet
176 = (ResultSet)ProxyUtil.createProxy( ResultSet.class );
177
178 final Statement statement
179 = (Statement)ProxyUtil.createProxy( Statement.class,
180 new Object[][]{{"executeQuery", new ResultSetWrapper( resultSet )}} );
181
182 final StatementWrapper statementWrapper
183 = new StatementWrapper( statement );
184
185 final ResultSetWrapper resultSetWrapper
186 = (ResultSetWrapper)statementWrapper.executeQuery( "foo" );
187 resultSetWrapper.close();
188
189 statementWrapper.close();
190 }
191 }
192