KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.SQLException JavaDoc;
21
22 import javax.sql.DataSource JavaDoc;
23
24 import junit.framework.TestCase;
25 import org.easymock.MockControl;
26
27 /**
28  * @author Juergen Hoeller
29  * @since 28.05.2004
30  */

31 public class UserCredentialsDataSourceAdapterTests extends TestCase {
32
33     public void testStaticCredentials() throws SQLException JavaDoc {
34         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
35         DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
36         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
37         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
38         ds.getConnection("user", "pw");
39         dsControl.setReturnValue(con);
40         dsControl.replay();
41         conControl.replay();
42
43         UserCredentialsDataSourceAdapter adapter = new UserCredentialsDataSourceAdapter();
44         adapter.setTargetDataSource(ds);
45         adapter.setUsername("user");
46         adapter.setPassword("pw");
47         assertEquals(con, adapter.getConnection());
48     }
49
50     public void testNoCredentials() throws SQLException JavaDoc {
51         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
52         DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
53         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
54         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
55         ds.getConnection();
56         dsControl.setReturnValue(con);
57         dsControl.replay();
58         conControl.replay();
59
60         UserCredentialsDataSourceAdapter adapter = new UserCredentialsDataSourceAdapter();
61         adapter.setTargetDataSource(ds);
62         assertEquals(con, adapter.getConnection());
63     }
64
65     public void testThreadBoundCredentials() throws SQLException JavaDoc {
66         MockControl dsControl = MockControl.createControl(DataSource JavaDoc.class);
67         DataSource JavaDoc ds = (DataSource JavaDoc) dsControl.getMock();
68         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
69         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
70         ds.getConnection("user", "pw");
71         dsControl.setReturnValue(con);
72         dsControl.replay();
73         conControl.replay();
74
75         UserCredentialsDataSourceAdapter adapter = new UserCredentialsDataSourceAdapter();
76         adapter.setTargetDataSource(ds);
77
78         adapter.setCredentialsForCurrentThread("user", "pw");
79         try {
80             assertEquals(con, adapter.getConnection());
81         }
82         finally {
83             adapter.removeCredentialsFromCurrentThread();
84         }
85     }
86
87 }
88
Popular Tags