KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > datasource > DriverManagerDataSourceTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jdbc.datasource;
18
19 import java.sql.Connection JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import junit.framework.TestCase;
23 import org.easymock.MockControl;
24
25 import org.springframework.jdbc.CannotGetJdbcConnectionException;
26
27 /**
28  * @author Rod Johnson
29  */

30 public class DriverManagerDataSourceTests extends TestCase {
31
32     public void testStandardUsage() throws Exception JavaDoc {
33         final String JavaDoc jdbcUrl = "url";
34         final String JavaDoc uname = "uname";
35         final String JavaDoc pwd = "pwd";
36
37         MockControl ctrlConnection =
38             MockControl.createControl(Connection JavaDoc.class);
39         final Connection JavaDoc mockConnection = (Connection JavaDoc) ctrlConnection.getMock();
40         ctrlConnection.replay();
41
42         class TestDriverManagerDataSource extends DriverManagerDataSource {
43             protected Connection JavaDoc getConnectionFromDriverManager(String JavaDoc url, Properties JavaDoc props) {
44                 assertEquals(jdbcUrl, url);
45                 assertEquals(uname, props.getProperty("user"));
46                 assertEquals(pwd, props.getProperty("password"));
47                 return mockConnection;
48             }
49         }
50
51         DriverManagerDataSource ds = new TestDriverManagerDataSource();
52         //ds.setDriverClassName("foobar");
53
ds.setUrl(jdbcUrl);
54         ds.setUsername(uname);
55         ds.setPassword(pwd);
56
57         Connection JavaDoc actualCon = ds.getConnection();
58         assertTrue(actualCon == mockConnection);
59
60         assertTrue(ds.getUrl().equals(jdbcUrl));
61         assertTrue(ds.getPassword().equals(pwd));
62         assertTrue(ds.getUsername().equals(uname));
63
64         ctrlConnection.verify();
65     }
66
67     public void testUsageWithConnectionProperties() throws Exception JavaDoc {
68         final String JavaDoc jdbcUrl = "url";
69
70         final Properties JavaDoc connProps = new Properties JavaDoc();
71         connProps.setProperty("myProp", "myValue");
72         connProps.setProperty("yourProp", "yourValue");
73         connProps.setProperty("user", "uname");
74         connProps.setProperty("password", "pwd");
75
76         MockControl ctrlConnection =
77             MockControl.createControl(Connection JavaDoc.class);
78         final Connection JavaDoc mockConnection = (Connection JavaDoc) ctrlConnection.getMock();
79         ctrlConnection.replay();
80
81         class TestDriverManagerDataSource extends DriverManagerDataSource {
82             protected Connection JavaDoc getConnectionFromDriverManager(String JavaDoc url, Properties JavaDoc props) {
83                 assertEquals(jdbcUrl, url);
84                 assertEquals("uname", props.getProperty("user"));
85                 assertEquals("pwd", props.getProperty("password"));
86                 assertEquals("myValue", props.getProperty("myProp"));
87                 assertEquals("yourValue", props.getProperty("yourProp"));
88                 return mockConnection;
89             }
90         }
91
92         DriverManagerDataSource ds = new TestDriverManagerDataSource();
93         //ds.setDriverClassName("foobar");
94
ds.setUrl(jdbcUrl);
95         ds.setConnectionProperties(connProps);
96
97         Connection JavaDoc actualCon = ds.getConnection();
98         assertTrue(actualCon == mockConnection);
99
100         assertTrue(ds.getUrl().equals(jdbcUrl));
101
102         ctrlConnection.verify();
103     }
104
105     public void testUsageWithConnectionPropertiesAndUserCredentials() throws Exception JavaDoc {
106         final String JavaDoc jdbcUrl = "url";
107         final String JavaDoc uname = "uname";
108         final String JavaDoc pwd = "pwd";
109
110         final Properties JavaDoc connProps = new Properties JavaDoc();
111         connProps.setProperty("myProp", "myValue");
112         connProps.setProperty("yourProp", "yourValue");
113         connProps.setProperty("user", "uname2");
114         connProps.setProperty("password", "pwd2");
115
116         MockControl ctrlConnection =
117             MockControl.createControl(Connection JavaDoc.class);
118         final Connection JavaDoc mockConnection = (Connection JavaDoc) ctrlConnection.getMock();
119         ctrlConnection.replay();
120
121         class TestDriverManagerDataSource extends DriverManagerDataSource {
122             protected Connection JavaDoc getConnectionFromDriverManager(String JavaDoc url, Properties JavaDoc props) {
123                 assertEquals(jdbcUrl, url);
124                 assertEquals(uname, props.getProperty("user"));
125                 assertEquals(pwd, props.getProperty("password"));
126                 assertEquals("myValue", props.getProperty("myProp"));
127                 assertEquals("yourValue", props.getProperty("yourProp"));
128                 return mockConnection;
129             }
130         }
131
132         DriverManagerDataSource ds = new TestDriverManagerDataSource();
133         //ds.setDriverClassName("foobar");
134
ds.setUrl(jdbcUrl);
135         ds.setUsername(uname);
136         ds.setPassword(pwd);
137         ds.setConnectionProperties(connProps);
138
139         Connection JavaDoc actualCon = ds.getConnection();
140         assertTrue(actualCon == mockConnection);
141
142         assertTrue(ds.getUrl().equals(jdbcUrl));
143         assertTrue(ds.getPassword().equals(pwd));
144         assertTrue(ds.getUsername().equals(uname));
145
146         ctrlConnection.verify();
147     }
148
149     public void testInvalidClassName() throws Exception JavaDoc {
150         String JavaDoc bogusClassName = "foobar";
151         DriverManagerDataSource ds = new DriverManagerDataSource();
152         try {
153             ds.setDriverClassName(bogusClassName);
154             fail("Should have thrown CannotGetJdbcConnectionException");
155         }
156         catch (CannotGetJdbcConnectionException ex) {
157             // OK
158
}
159     }
160
161 }
162
Popular Tags