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.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
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
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
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