1 21 package org.apache.derbyTesting.functionTests.tests.jdbc4; 22 23 import junit.framework.*; 24 25 import org.apache.derbyTesting.functionTests.util.TestDataSourceFactory; 26 import org.apache.derbyTesting.functionTests.util.SQLStateConstants; 27 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 28 29 import java.io.FileInputStream ; 30 import java.io.FileNotFoundException ; 31 import java.io.IOException ; 32 import java.io.OutputStream ; 33 import java.sql.*; 34 import java.util.Properties ; 35 import javax.sql.*; 36 37 47 public class ConnectionTest 48 extends BaseJDBCTestCase { 49 50 55 public ConnectionTest(String name) { 56 super(name); 57 } 58 59 61 73 public void embeddedCreateBlob() 74 throws SQLException { 75 Blob blob = getConnection().createBlob(); 76 if(blob.length() > 0) 78 fail("The new Blob should not have more than zero bytes " + 79 "contained in it"); 80 } 81 82 94 public void embeddedCreateClob() 95 throws SQLException { 96 Clob clob = getConnection().createClob(); 97 if(clob.length() > 0) 99 fail("The new Clob should not have a length of greater than " + 100 "zero"); 101 } 102 103 public void testCreateArrayNotImplemented() 104 throws SQLException { 105 try { 106 getConnection().createArrayOf(null, null); 107 fail("createArrayOf(String,Object[]) should not be implemented"); 108 } catch (SQLFeatureNotSupportedException sfnse) { 109 } 111 } 112 113 public void testCreateNClobNotImplemented() 114 throws SQLException { 115 try { 116 getConnection().createNClob(); 117 fail("createNClob() should not be implemented"); 118 } catch (SQLFeatureNotSupportedException sfnse) { 119 } 121 } 122 123 public void testCreateSQLXMLNotImplemented() 124 throws SQLException { 125 try { 126 getConnection().createSQLXML(); 127 fail("createSQLXML() should not be implemented"); 128 } catch (SQLFeatureNotSupportedException sfnse) { 129 } 131 } 132 133 public void testCreateStructNotImplemented() 134 throws SQLException { 135 try { 136 getConnection().createStruct(null, null); 137 fail("createStruct(String,Object[]) should not be implemented"); 138 } catch (SQLFeatureNotSupportedException sfnse) { 139 } 141 } 142 143 public void testGetClientInfo() 144 throws SQLException { 145 assertTrue("getClientInfo() must return an empty Properties object", 146 getConnection().getClientInfo().isEmpty()); 147 } 148 149 public void testGetClientInfoString() 150 throws SQLException { 151 assertNull("getClientInfo(null) must return null", 152 getConnection().getClientInfo(null)); 153 assertNull("getClientInfo(\"someProperty\") must return null", 154 getConnection().getClientInfo("someProperty")); 155 } 156 157 164 public void testIsValidImplemented() throws SQLException { 165 assertTrue(getConnection().isValid(0)); 167 168 assertTrue(getConnection().isValid(1)); 170 171 try { 173 getConnection().isValid(-1); 174 } catch (SQLException sqle) { 175 assertSQLState("Incorrect SQL state when calling isValid(-1)", 176 "XJ081", sqle); 177 } 178 } 179 180 185 public void testGetTypeMapReturnsEmptyMap() throws SQLException { 186 assertTrue(getConnection().getTypeMap().isEmpty()); 187 } 188 189 public void testIsWrapperReturnsFalse() 190 throws SQLException { 191 assertFalse(getConnection().isWrapperFor(ResultSet.class)); 192 } 193 194 public void testIsWrapperReturnsTrue() 195 throws SQLException { 196 assertTrue(getConnection().isWrapperFor(Connection.class)); 197 } 198 199 public void testSetClientInfoProperties() 200 throws SQLException { 201 getConnection().setClientInfo(null); 202 Properties p = new Properties (); 203 getConnection().setClientInfo(p); 204 205 p.setProperty("prop1", "val1"); 206 p.setProperty("prop2", "val2"); 207 try { 208 getConnection().setClientInfo(p); 209 fail("setClientInfo(String,String) should throw "+ 210 "SQLClientInfoException"); 211 } catch (SQLClientInfoException cie) { 212 assertSQLState("SQLStates must match", "XCY02", cie); 213 assertTrue("Setting property 'prop1' must fail with "+ 214 "REASON_UNKNOWN_PROPERTY", 215 cie.getFailedProperties(). 216 get("prop1"). 217 equals(ClientInfoStatus.REASON_UNKNOWN_PROPERTY)); 218 assertTrue("Setting property 'prop2' must fail with "+ 219 "REASON_UNKNOWN_PROPERTY", 220 cie.getFailedProperties(). 221 get("prop2"). 222 equals(ClientInfoStatus.REASON_UNKNOWN_PROPERTY)); 223 } 224 } 225 226 public void testSetClientInfoString() 227 throws SQLException { 228 getConnection().setClientInfo(null, null); 229 230 try { 231 getConnection().setClientInfo("foo", null); 232 fail("setClientInfo(String, null) should throw "+ 233 "NullPointerException"); 234 } catch (NullPointerException npe) {} 235 236 try { 237 getConnection().setClientInfo("name", "value"); 238 fail("setClientInfo(String,String) should throw "+ 239 "SQLClientInfoException"); 240 } catch (SQLClientInfoException cie) { 241 assertSQLState("SQLState must match 'unsupported'", 242 "XCY02", cie); 243 assertTrue("Setting property 'name' must fail with "+ 244 "REASON_UNKNOWN_PROPERTY", 245 cie.getFailedProperties(). 246 get("name"). 247 equals(ClientInfoStatus.REASON_UNKNOWN_PROPERTY)); 248 } 249 } 250 251 public void testUnwrapValid() 252 throws SQLException { 253 Connection unwrappedCon = getConnection().unwrap(Connection.class); 254 assertSame("Unwrap returned wrong object.", getConnection(), unwrappedCon); 255 } 256 257 public void testUnwrapInvalid() 258 throws SQLException { 259 try { 260 ResultSet unwrappedRs = getConnection().unwrap(ResultSet.class); 261 fail("unwrap should have thrown an exception"); 262 } catch (SQLException sqle) { 263 assertSQLState("Incorrect SQL state when unable to unwrap", 264 SQLStateConstants.UNABLE_TO_UNWRAP, 265 sqle); 266 } 267 } 268 269 271 274 private static TestSuite clientSuite(String name) { 275 TestSuite clientSuite = new TestSuite(name); 276 return clientSuite; 277 } 278 279 282 private static TestSuite embeddedSuite(String name) { 283 TestSuite embeddedSuite = new TestSuite(name); 284 embeddedSuite.addTest(new ConnectionTest( 285 "embeddedCreateBlob")); 286 embeddedSuite.addTest(new ConnectionTest( 287 "embeddedCreateClob")); 288 return embeddedSuite; 289 } 290 291 296 public static Test suite() { 297 TestSuite connSuite = 298 new TestSuite(ConnectionTest.class, "ConnectionTest suite"); 299 if (usingDerbyNetClient()) { 302 connSuite.addTest( 303 clientSuite("ConnectionTest client-only suite")); 304 } 305 if (usingEmbedded()) { 307 connSuite.addTest( 308 embeddedSuite("ConnectionTest embedded-only suite")); 309 } 310 return connSuite; 311 } 312 313 } | Popular Tags |