1   /*
2    * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    *
7    * 1. Redistributions of source code must retain the above copyright notice,
8    *    this list of conditions and the following disclaimer.
9    * 2. Redistributions in binary form must reproduce the above copyright notice,
10   *    this list of conditions and the following disclaimer in the documentation
11   *    and/or other materials provided with the distribution.
12   * 3. The end-user documentation included with the redistribution, if any, must
13   *    include the following acknowledgment:
14   *
15   *       "This product includes software developed by Gargoyle Software Inc.
16   *        (http://www.GargoyleSoftware.com/)."
17   *
18   *    Alternately, this acknowledgment may appear in the software itself, if
19   *    and wherever such third-party acknowledgments normally appear.
20   * 4. The name "Gargoyle Software" must not be used to endorse or promote
21   *    products derived from this software without prior written permission.
22   *    For written permission, please contact info@GargoyleSoftware.com.
23   * 5. Products derived from this software may not be called "GSBase", nor may
24   *    "GSBase" appear in their name, without prior written permission of
25   *    Gargoyle Software Inc.
26   *
27   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30   * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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              // Expected path
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             // Expected path
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             // Expected path
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