1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.ConnectionTest
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to you under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.jdbc4;
import java.sql.Blob;
import java.sql.ClientInfoStatus;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Tests for the JDBC 4.0 specific methods in the connection object(s).
*
* Which connection implementation is tested, depends on what connection
* object the <code>BaseJDBCTestCase.getConnection()</code>-method returns.
* Currently, the property <code>derbyTesting.xa.single</code> can be set to
* <code>true</code> to test the XA connection object, which happens to be the
* same as the one used for poooled connections.
* The connection returned also depends on which framework is being used.
*/
public class ConnectionTest
extends BaseJDBCTestCase {
/**
* Create a test with the given name.
*
* @param name name of the test.
*/
public ConnectionTest(String name) {
super(name);
}
//------------------------- T E S T M E T H O D S ------------------------
/**
*
* Tests the Embedded implementation for the createBlob method. The Embedded
* server does'nt currently have the set methods implemented. Hence the
* create methods cannot be tested by inserting data into the empty LOB
* object. Here we do a simple test of checking that the length of the
* LOB object is 0.
*
* @throws SQLException upon failure in the createBlob or the length
* methods.
*
*/
public void embeddedCreateBlob()
throws SQLException {
Blob blob = getConnection().createBlob();
//Check if the blob is empty
if(blob.length() > 0)
fail("The new Blob should not have more than zero bytes " +
"contained in it");
}
/**
*
* Tests the Embedded implementation for the createClob method. The Embedded
* server does'nt currently have the set methods implemented. Hence the
* create methods cannot be tested by inserting data into the empty LOB
* object. Here we do a simple test of checking that the length of the
* LOB object is 0.
*
* @throws SQLException upon failure in the createClob or the length
* methods.
*
*/
public void embeddedCreateClob()
throws SQLException {
Clob clob = getConnection().createClob();
//check if the Clob is empty
if(clob.length() > 0)
fail("The new Clob should not have a length of greater than " +
"zero");
}
public void testCreateArrayNotImplemented()
throws SQLException {
try {
getConnection().createArrayOf(null, null);
fail("createArrayOf(String,Object[]) should not be implemented");
} catch (SQLFeatureNotSupportedException sfnse) {
// Do nothing, we are fine
}
}
public void testCreateNClobNotImplemented()
throws SQLException {
try {
getConnection().createNClob();
fail("createNClob() should not be implemented");
} catch (SQLFeatureNotSupportedException sfnse) {
// Do nothing, we are fine
}
}
public void testCreateSQLXMLNotImplemented()
throws SQLException {
try {
getConnection().createSQLXML();
fail("createSQLXML() should not be implemented");
} catch (SQLFeatureNotSupportedException sfnse) {
// Do nothing, we are fine
}
}
public void testCreateStructNotImplemented()
throws SQLException {
try {
getConnection().createStruct(null, null);
fail("createStruct(String,Object[]) should not be implemented");
} catch (SQLFeatureNotSupportedException sfnse) {
// Do nothing, we are fine
}
}
public void testGetClientInfo()
throws SQLException {
assertTrue("getClientInfo() must return an empty Properties object",
getConnection().getClientInfo().isEmpty());
}
public void testGetClientInfoString()
throws SQLException {
assertNull("getClientInfo(null) must return null",
getConnection().getClientInfo(null));
assertNull("getClientInfo(\"someProperty\") must return null",
getConnection().getClientInfo("someProperty"));
}
/**
* Tests that <code>isValid</code> is implemented and returns true
* for the connection. This test is very limited but is tested
* for all connection types. A more complete test of isValid is
* found in the ConnectionMethodsTest.java test that is run for
* embedded and network client connections.
*/
public void testIsValidImplemented() throws SQLException {
// Test with an infinite (0) timeout
assertTrue(getConnection().isValid(0));
// Test with a large timeout. We expect
// to complete and succeed much sooner.
// see DERBY-5912
assertTrue(getConnection().isValid(200));
// Test with an illegal timeout
try {
getConnection().isValid(-1);
} catch (SQLException sqle) {
assertSQLState("Incorrect SQL state when calling isValid(-1)",
"XJ081", sqle);
}
}
/**
* Tests that <code>isValid</code> times out when expected.
* This test will need a modification to the source code;
* activate the commented out Thread.sleep(2000) (2 seconds) in
* DRDAConnThread.ProcessCommands, case CodePoint.OPNQRY
* To activate the test, remove the extra 'x' before building
*/
public void xtestIsValidWithTimeout() throws SQLException {
// isValid(timeoutvalue) is a no-op in Embedded
if (usingEmbedded()) {
return;
}
// Test with a large timeout, see DERBY-5912.
boolean convalid=true;
Connection conn=getConnection();
// with a longer time out, the isValid call should not
// time out when the sleep is shorter.
convalid=conn.isValid(200);
assertTrue(convalid);
// setting the timeout to 1 should timeout if the sleep
// is 2 seconds.
convalid=conn.isValid(1);
assertFalse(convalid);
// rollback should work even though isvalid timed out...
// But there's a bug in that the connection becomes invalid and
// it is not getting re-established, see DERBY-5919.
// Catch the exception saying No current Connection and swallow.
try {
conn.rollback();
//conn.close();
} catch (Exception e) {
//println("exception: " + e.getStackTrace());
}
}
/**
* Tests that <code>getTypeMap()</code> returns an empty map when
* no type map has been installed.
* @exception SQLException if an error occurs
*/
public void testGetTypeMapReturnsEmptyMap() throws SQLException {
assertTrue(getConnection().getTypeMap().isEmpty());
}
/**
* Tests that <code>getTypeMap()</code> returns the input map
* @exception SQLException if an error occurs
*/
public void testGetTypeMapReturnsAsExpected() throws SQLException {
Statement s = getConnection().createStatement();
int ret;
try {
ret = s.executeUpdate("DROP TABLE T1");
ret = s.executeUpdate("DROP TYPE JAVA_UTIL_LIST RESTRICT");
} catch (Exception e) {}
ret = s.executeUpdate("CREATE TYPE JAVA_UTIL_LIST " +
"EXTERNAL NAME 'java.util.List'" +
"LANGUAGE JAVA");
s.execute("CREATE TABLE T1 (A1 JAVA_UTIL_LIST)");
PreparedStatement ps = getConnection().prepareStatement(
"INSERT INTO T1(A1) VALUES (?)");
ArrayList<String> lst = new ArrayList<String>();
lst.add("First element");
lst.add("Second element");
ps.setObject(1, lst);
ps.execute();
Map<String, Class<?>> map = getConnection().getTypeMap();
try {
map.put("JAVA_UTIL_LIST", List.class);
fail("returned map should be immutable");
} catch (UnsupportedOperationException uoe) {
// Ignore expected exception
}
//Pass empty Map to setTypeMap(). It won't raise any erros because
//the method does nothing when the Map is empty.
java.util.Map<String,Class<?>> emptyMap = new java.util.HashMap<String,Class<?>>();
getConnection().setTypeMap(emptyMap);
// Create a non-empty map to test setTypeMap(). setTypeMap() raises
// a feature not supported exception if the map isn't empty.
map = new HashMap<String, Class<?>>();
map.put("JAVA_UTIL_LIST", List.class);
try {
getConnection().setTypeMap(map);
fail( "Should raise an Unimplemented Feature exception." );
}
catch (SQLException se)
{
assertEquals( SQLFeatureNotSupportedException.class.getName(), se.getClass().getName() );
}
ResultSet rs = s.executeQuery("select * from T1");
assertTrue(rs.next());
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
Object o = rs.getObject(i);
assertEquals(lst, o);
//System.out.print(o + "(Type " + o.getClass().getName() + " )");
}
s.executeUpdate("DROP TABLE T1");
s.executeUpdate("DROP TYPE JAVA_UTIL_LIST RESTRICT");
s.close();
ps.close();
}
public void testIsWrapperReturnsFalse()
throws SQLException {
assertFalse(getConnection().isWrapperFor(ResultSet.class));
}
public void testIsWrapperReturnsTrue()
throws SQLException {
assertTrue(getConnection().isWrapperFor(Connection.class));
}
public void testSetClientInfoProperties()
throws SQLException {
getConnection().setClientInfo(null);
Properties p = new Properties();
getConnection().setClientInfo(p);
p.setProperty("prop1", "val1");
p.setProperty("prop2", "val2");
try {
getConnection().setClientInfo(p);
fail("setClientInfo(String,String) should throw "+
"SQLClientInfoException");
} catch (SQLClientInfoException cie) {
assertSQLState("SQLStates must match", "XCY02", cie);
assertTrue("Setting property 'prop1' must fail with "+
"REASON_UNKNOWN_PROPERTY",
cie.getFailedProperties().
get("prop1").
equals(ClientInfoStatus.REASON_UNKNOWN_PROPERTY));
assertTrue("Setting property 'prop2' must fail with "+
"REASON_UNKNOWN_PROPERTY",
cie.getFailedProperties().
get("prop2").
equals(ClientInfoStatus.REASON_UNKNOWN_PROPERTY));
}
}
public void testSetClientInfoString()
throws SQLException {
getConnection().setClientInfo(null, null);
try {
getConnection().setClientInfo("foo", null);
fail("setClientInfo(String, null) should throw "+
"NullPointerException");
} catch (NullPointerException npe) {}
try {
getConnection().setClientInfo("name", "value");
fail("setClientInfo(String,String) should throw "+
"SQLClientInfoException");
} catch (SQLClientInfoException cie) {
assertSQLState("SQLState must match 'unsupported'",
"XCY02", cie);
assertTrue("Setting property 'name' must fail with "+
"REASON_UNKNOWN_PROPERTY",
cie.getFailedProperties().
get("name").
equals(ClientInfoStatus.REASON_UNKNOWN_PROPERTY));
}
}
public void testUnwrapValid()
throws SQLException {
Connection unwrappedCon = getConnection().unwrap(Connection.class);
assertSame("Unwrap returned wrong object.", getConnection(), unwrappedCon);
}
public void testUnwrapInvalid()
throws SQLException {
try {
ResultSet unwrappedRs = getConnection().unwrap(ResultSet.class);
fail("unwrap should have thrown an exception");
} catch (SQLException sqle) {
assertSQLState("Incorrect SQL state when unable to unwrap",
SQLStateConstants.UNABLE_TO_UNWRAP,
sqle);
}
}
//------------------ E N D O F T E S T M E T H O D S -------------------
/**
* Create suite containing client-only tests.
*/
private static BaseTestSuite clientSuite(String name) {
BaseTestSuite clientSuite = new BaseTestSuite(name);
return clientSuite;
}
/**
* Create suite containing embedded-only tests.
*/
private static BaseTestSuite embeddedSuite(String name) {
BaseTestSuite embeddedSuite = new BaseTestSuite(name);
embeddedSuite.addTest(new ConnectionTest(
"embeddedCreateBlob"));
embeddedSuite.addTest(new ConnectionTest(
"embeddedCreateClob"));
return embeddedSuite;
}
/**
* Create a test suite containing tests for a JDB connection.
* In addition, separate suites for embedded- and client-only are added
* when appropriate.
*/
public static Test suite() {
BaseTestSuite connSuite = new BaseTestSuite("ConnectionTest suite");
BaseTestSuite embedded = new BaseTestSuite("ConnectionTest:embedded");
embedded.addTestSuite(ConnectionTest.class);
embedded.addTest(embeddedSuite("ConnectionTest:embedded-only"));
connSuite.addTest(embedded);
// repeat the embedded tests obtaining a connection from
// an XA data source.
embedded = new BaseTestSuite("ConnectionTest:embedded XADataSource");
embedded.addTestSuite(ConnectionTest.class);
embedded.addTest(embeddedSuite("ConnectionTest:embedded-only XADataSource"));
connSuite.addTest(TestConfiguration.connectionXADecorator(embedded));
BaseTestSuite client = new BaseTestSuite("ConnectionTest:client");
client.addTestSuite(ConnectionTest.class);
client.addTest(clientSuite("ConnectionTest:client-only"));
connSuite.addTest(TestConfiguration.clientServerDecorator(client));
// repeat the client tests obtaining a connection from
// an XA data source.
client = new BaseTestSuite("ConnectionTest:client XADataSource");
client.addTestSuite(ConnectionTest.class);
client.addTest(clientSuite("ConnectionTest:client-only XADataSource"));
connSuite.addTest(
TestConfiguration.clientServerDecorator(
TestConfiguration.connectionXADecorator(client)));
return connSuite;
}
} // End class BaseJDBCTestCase
|