1 19 20 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 21 22 import java.lang.reflect.Method ; 23 import java.sql.Connection ; 24 import java.sql.SQLException ; 25 import javax.sql.ConnectionPoolDataSource ; 26 import javax.sql.DataSource ; 27 import javax.sql.PooledConnection ; 28 import javax.sql.XAConnection ; 29 import javax.sql.XADataSource ; 30 import junit.framework.Test; 31 import junit.framework.TestSuite; 32 import org.apache.derbyTesting.functionTests.util.TestDataSourceFactory; 33 import org.apache.derbyTesting.functionTests.util.TestUtil; 34 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 35 import org.apache.derbyTesting.junit.JDBC; 36 37 40 public class DataSourcePropertiesTest extends BaseJDBCTestCase { 41 42 46 public DataSourcePropertiesTest(String name) { 47 super(name); 48 } 49 50 52 53 public static Test suite() { 54 55 56 TestSuite suite = new TestSuite(); 57 58 if (JDBC.vmSupportsJDBC3()) { 63 64 67 if (usingEmbedded()) { 68 69 Method [] methods = DataSourcePropertiesTest.class.getMethods(); 71 for (int i = 0; i < methods.length; i++) { 72 Method m = methods[i]; 73 if (m.getParameterTypes().length > 0 || 74 m.getReturnType().equals(Void.TYPE)) { 75 continue; 76 } 77 String name = m.getName(); 78 if (name.startsWith("embedded")) { 79 suite.addTest(new DataSourcePropertiesTest(name)); 80 } 81 } 82 } 83 } 84 return suite; 85 } 86 87 89 98 private void setDataSourceProperty(Object dataSource, String name, 99 Object value, Class type) 100 throws Exception 101 { 102 Method setter = dataSource.getClass(). 103 getMethod(TestUtil.getSetterName(name), new Class [] { type }); 104 setter.invoke(dataSource, new Object [] { value }); 105 } 106 107 114 private void setDataSourceProperty(Object dataSource, String name, 115 Object value) throws Exception { 116 setDataSourceProperty(dataSource, name, value, value.getClass()); 117 } 118 119 121 126 public void embeddedTestAttributesAsPasswordWithoutPassword_ds() 127 throws Exception 128 { 129 DataSource ds = TestDataSourceFactory.getDataSource(); 130 setDataSourceProperty(ds, "password", "mypassword"); 131 setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE, 132 Boolean.TYPE); 133 Connection c = ds.getConnection(); 134 c.close(); 135 } 136 137 142 public void embeddedTestAttributesAsPasswordWithoutPassword_pooled() 143 throws Exception 144 { 145 ConnectionPoolDataSource ds = 146 TestDataSourceFactory.getConnectionPoolDataSource(); 147 setDataSourceProperty(ds, "password", "mypassword"); 148 setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE, 149 Boolean.TYPE); 150 PooledConnection pc = ds.getPooledConnection(); 152 Connection c = pc.getConnection(); 153 c.close(); 154 } 155 156 161 public void embeddedTestAttributesAsPasswordWithoutPassword_xa() 162 throws Exception 163 { 164 XADataSource ds = TestDataSourceFactory.getXADataSource(); 165 setDataSourceProperty(ds, "password", "mypassword"); 166 setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE, 167 Boolean.TYPE); 168 XAConnection xa = ds.getXAConnection(); 169 Connection c = xa.getConnection(); 170 c.close(); 171 } 172 173 178 public void embeddedTestAttributesAsPasswordWithPassword_ds() 179 throws Exception 180 { 181 DataSource ds = TestDataSourceFactory.getDataSource(); 182 setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE, 183 Boolean.TYPE); 184 try { 185 Connection c = ds.getConnection("username", "mypassword"); 186 fail("Expected getConnection to fail."); 187 } catch (SQLException e) { 188 assertSQLState("XJ028", e); 190 } 191 } 192 193 198 public void embeddedTestAttributesAsPasswordWithPassword_pooled() 199 throws Exception 200 { 201 ConnectionPoolDataSource ds = 202 TestDataSourceFactory.getConnectionPoolDataSource(); 203 setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE, 204 Boolean.TYPE); 205 try { 206 PooledConnection pc = 207 ds.getPooledConnection("username", "mypassword"); 208 fail("Expected getPooledConnection to fail."); 209 } catch (SQLException e) { 210 assertSQLState("XJ028", e); 212 } 213 } 214 215 220 public void embeddedTestAttributesAsPasswordWithPassword_xa() 221 throws Exception 222 { 223 XADataSource ds = TestDataSourceFactory.getXADataSource(); 224 setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE, 225 Boolean.TYPE); 226 try { 227 XAConnection xa = ds.getXAConnection("username", "mypassword"); 228 fail("Expected getXAConnection to fail."); 229 } catch (SQLException e) { 230 assertSQLState("XJ028", e); 232 } 233 } 234 } 235 | Popular Tags |