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.resource.ManagedResource;
41  import com.gargoylesoftware.base.resource.ResourceManager;
42  import java.lang.reflect.InvocationHandler;
43  import java.lang.reflect.Method;
44  import java.lang.reflect.Proxy;
45  import java.sql.Connection;
46  import java.sql.SQLException;
47  import junit.framework.TestCase;
48  
49  /***
50   *  Tests for com.gargoylesoftware.base.objectstore.jdbc.JDBCResourceFactory
51   *
52   * @version  $Revision: 1.7 $
53   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
54   */
55  public class JDBCResourceFactoryTest extends TestCase {
56  
57      /***
58       *  Create a test
59       *
60       * @param  name The name of the test
61       */
62      public JDBCResourceFactoryTest( final String name ) {
63          super( name );
64      }
65  
66  
67      /***
68       *  Create a proxy connection object
69       *
70       * @return  The proxy
71       */
72      public static Connection makeConnection() {
73          final InvocationHandler handler =
74              new InvocationHandler() {
75                  public Object invoke( Object proxy, Method method, Object[] args ) {
76                      return null;
77                  }
78              };
79  
80          return (Connection)Proxy.newProxyInstance(
81                  ManagedResource.class.getClassLoader(),
82                  new Class[]{Connection.class, ManagedResource.class},
83                  handler );
84      }
85  
86  
87      /***
88       *  Test creating an instance with a null database name
89       *
90       * @exception  Exception If an error occurs
91       */
92      public void testConstructor_NullDatabaseName()
93          throws Exception {
94          try {
95              new JDBCResourceFactory( null, "foo", "foo" );
96              fail( "Exception exception" );
97          }
98          catch( final NullPointerException e ) {
99              // Expected path
100         }
101     }
102 
103 
104     /***
105      *  Test creating an instance with a null user id
106      *
107      * @exception  Exception If an error occurs
108      */
109     public void testConstructor_NullUser()
110         throws Exception {
111         try {
112             new JDBCResourceFactory( "foo", null, "foo" );
113             fail( "Exception exception" );
114         }
115         catch( final NullPointerException e ) {
116             // Expected path
117         }
118     }
119 
120 
121     /***
122      *  Test creating an instance with a null password
123      *
124      * @exception  Exception If an error occurs
125      */
126     public void testConstructor_NullPassword()
127         throws Exception {
128         try {
129             new JDBCResourceFactory( "foo", "foo", null );
130             fail( "Exception exception" );
131         }
132         catch( final NullPointerException e ) {
133             // Expected path
134         }
135     }
136 
137 
138     /***
139      *  Make sure that the constructor will throw an exception if we cannot open
140      *  a connection to the database.
141      */
142     public void testConstructor_NoConnection() {
143         try {
144             /*** Fake class for testing */
145             class MyResourceFactory extends JDBCResourceFactory {
146                 /***
147                  * @param database The database
148                  * @param user The user
149                  * @param password The password
150                  * @throws SQLException If something fails
151                  */
152                 public MyResourceFactory(
153                         final String database, final String user, final String password )
154                     throws SQLException {
155                     super(database, user, password);
156                 }
157                 /***
158                  * @param manager The resource manager
159                  * @return The resource
160                  * @throws SQLException If something fails
161                  */
162                 protected ManagedResource getResourceImpl( final ResourceManager manager )
163                     throws SQLException {
164 
165                     throw new SQLException( "NoConnectionTest" );
166                 }
167             }
168             new MyResourceFactory( "database", "user", "password" );
169             fail( "Expected exception" );
170         }
171         catch( final SQLException e ) {
172             assertEquals( "NoConnectionTest", e.getMessage() );
173         }
174     }
175 }
176