1 16 17 package org.springframework.jdbc.datasource; 18 19 import java.sql.Connection ; 20 import java.util.Properties ; 21 22 import junit.framework.TestCase; 23 import org.easymock.MockControl; 24 25 import org.springframework.jdbc.CannotGetJdbcConnectionException; 26 27 30 public class DriverManagerDataSourceTests extends TestCase { 31 32 public void testStandardUsage() throws Exception { 33 final String jdbcUrl = "url"; 34 final String uname = "uname"; 35 final String pwd = "pwd"; 36 37 MockControl ctrlConnection = 38 MockControl.createControl(Connection .class); 39 final Connection mockConnection = (Connection ) ctrlConnection.getMock(); 40 ctrlConnection.replay(); 41 42 class TestDriverManagerDataSource extends DriverManagerDataSource { 43 protected Connection getConnectionFromDriverManager(String url, Properties 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.setUrl(jdbcUrl); 54 ds.setUsername(uname); 55 ds.setPassword(pwd); 56 57 Connection 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 { 68 final String jdbcUrl = "url"; 69 70 final Properties connProps = new Properties (); 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 .class); 78 final Connection mockConnection = (Connection ) ctrlConnection.getMock(); 79 ctrlConnection.replay(); 80 81 class TestDriverManagerDataSource extends DriverManagerDataSource { 82 protected Connection getConnectionFromDriverManager(String url, Properties 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.setUrl(jdbcUrl); 95 ds.setConnectionProperties(connProps); 96 97 Connection 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 { 106 final String jdbcUrl = "url"; 107 final String uname = "uname"; 108 final String pwd = "pwd"; 109 110 final Properties connProps = new Properties (); 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 .class); 118 final Connection mockConnection = (Connection ) ctrlConnection.getMock(); 119 ctrlConnection.replay(); 120 121 class TestDriverManagerDataSource extends DriverManagerDataSource { 122 protected Connection getConnectionFromDriverManager(String url, Properties 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.setUrl(jdbcUrl); 135 ds.setUsername(uname); 136 ds.setPassword(pwd); 137 ds.setConnectionProperties(connProps); 138 139 Connection 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 { 150 String 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 } 159 } 160 161 } 162 | Popular Tags |