1 16 17 package org.springframework.jdbc.datasource; 18 19 import java.sql.Connection ; 20 import java.sql.SQLException ; 21 22 import javax.sql.DataSource ; 23 24 import junit.framework.TestCase; 25 import org.easymock.MockControl; 26 27 31 public class UserCredentialsDataSourceAdapterTests extends TestCase { 32 33 public void testStaticCredentials() throws SQLException { 34 MockControl dsControl = MockControl.createControl(DataSource .class); 35 DataSource ds = (DataSource ) dsControl.getMock(); 36 MockControl conControl = MockControl.createControl(Connection .class); 37 Connection con = (Connection ) 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 { 51 MockControl dsControl = MockControl.createControl(DataSource .class); 52 DataSource ds = (DataSource ) dsControl.getMock(); 53 MockControl conControl = MockControl.createControl(Connection .class); 54 Connection con = (Connection ) 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 { 66 MockControl dsControl = MockControl.createControl(DataSource .class); 67 DataSource ds = (DataSource ) dsControl.getMock(); 68 MockControl conControl = MockControl.createControl(Connection .class); 69 Connection con = (Connection ) 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 |