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
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.DataSourceSerializationTest
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.jdbcapi;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import javax.sql.DataSource;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.Derby;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.SupportFilesSetup;
/**
* Makes sure that old serialized data sources can be de-serialized with the
* current version of the data source.
* <p>
* Serialized data source from old versions are expected to be found in
* <tt>testData/serializedDataSources</tt>, with the following filename
* format CLASSNAME-VERSION.ser, where CLASSNAME is the unqualified name of the
* data source class, and VERSION is the Derby version. An example:
* <tt>ClientPooledConnectionDataSource-10_1.ser</tt>
* <p>
* A separation between JDBC 4.0 specific classes and the other classes is not
* made before release 10.10.
* <p>
* This test should detect the typical incompatible changes in the current
* data source implementations, for instance deleting a field or changing its
* type.
*/
public class DataSourceSerializationTest
extends BaseJDBCTestCase {
/** Constant for Derby version 10.0.2.1. */
private static final String VERSION_10_0_2_1 = "10_0_2_1";
/** Constant for Derby version 10.1.3.1. */
private static final String VERSION_10_1_3_1 = "10_1_3_1";
/** Constant for Derby version 10.2.2.0 */
private static final String VERSION_10_2_2_0 = "10_2_2_0";
/** Constant for Derby version 10.3.2.1. */
private static final String VERSION_10_3_2_1 = "10_3_2_1";
/** Constant for Derby version 10.10.1.0. */
private static final String VERSION_10_10_1_0 = "10_10_1_0";
/** Constant for Derby version 10.11.1.0. */
private static final String VERSION_10_11_1_0 = "10_11_1_0";
private final String _40Suffix = "40";
public DataSourceSerializationTest(String name) {
super(name);
}
/**
* Tests the de-serialization of the basic embedded data source.
*
* @throws Exception for a number of error conditions
*/
public void serTestEmbeddedDataSource()
throws Exception {
if (JDBC.vmSupportsJNDI()) {
final String EMBEDDED_CLASS = "EmbeddedDataSource";
deSerializeDs(EMBEDDED_CLASS, VERSION_10_0_2_1, true);
deSerializeDs(EMBEDDED_CLASS, VERSION_10_1_3_1, true);
deSerializeDs(EMBEDDED_CLASS, VERSION_10_2_2_0, true);
deSerializeDs(EMBEDDED_CLASS, VERSION_10_3_2_1, true);
deSerializeDs(EMBEDDED_CLASS, VERSION_10_10_1_0, true);
deSerializeDs(EMBEDDED_CLASS + _40Suffix, VERSION_10_10_1_0, true);
}
final String EMBEDDED_CLASS = "BasicEmbeddedDataSource40";
deSerializeDs(EMBEDDED_CLASS, VERSION_10_10_1_0, false);
}
/**
* Tests the de-serialization of the embedded connection pool data source.
*
* @throws Exception for a number of error conditions
*/
public void serTestEmbeddedConnectionPoolDataSource()
throws Exception {
if (JDBC.vmSupportsJNDI()) {
final String EMBEDDED_CLASS = "EmbeddedConnectionPoolDataSource";
deSerializeDs(EMBEDDED_CLASS, VERSION_10_0_2_1, true);
deSerializeDs(EMBEDDED_CLASS, VERSION_10_1_3_1, true);
deSerializeDs(EMBEDDED_CLASS, VERSION_10_2_2_0, true);
deSerializeDs(EMBEDDED_CLASS, VERSION_10_3_2_1, true);
deSerializeDs(EMBEDDED_CLASS, VERSION_10_10_1_0, true);
deSerializeDs(EMBEDDED_CLASS + _40Suffix, VERSION_10_10_1_0, true);
}
final String EMBEDDED_CLASS =
"BasicEmbeddedConnectionPoolDataSource40";
deSerializeDs(EMBEDDED_CLASS, VERSION_10_10_1_0, false);
}
/**
* Tests the de-serialization of the embedded XA data source.
*
* @throws Exception for a number of error conditions
*/
public void serTestEmbeddedXADataSource()
throws Exception {
if (JDBC.vmSupportsJNDI()) {
final String EMBEDDED_CLASS = "EmbeddedXADataSource";
deSerializeDs(EMBEDDED_CLASS, VERSION_10_0_2_1, true);
deSerializeDs(EMBEDDED_CLASS, VERSION_10_1_3_1, true);
deSerializeDs(EMBEDDED_CLASS, VERSION_10_2_2_0, true);
deSerializeDs(EMBEDDED_CLASS, VERSION_10_3_2_1, true);
deSerializeDs(EMBEDDED_CLASS, VERSION_10_10_1_0, true);
deSerializeDs(EMBEDDED_CLASS + _40Suffix, VERSION_10_10_1_0, true);
}
final String EMBEDDED_CLASS = "BasicEmbeddedXADataSource40";
deSerializeDs(EMBEDDED_CLASS, VERSION_10_10_1_0, false);
}
/**
* Tests the de-serialization of the basic client data source.
*
* @throws Exception for a number of error conditions
*/
public void serTestClientDataSource()
throws Exception {
if (JDBC.vmSupportsJNDI()) {
final String CLIENT_CLASS = "ClientDataSource";
// No client driver for Derby 10.0
deSerializeDs(CLIENT_CLASS, VERSION_10_1_3_1, true);
deSerializeDs(CLIENT_CLASS, VERSION_10_2_2_0, true);
deSerializeDs(CLIENT_CLASS, VERSION_10_3_2_1, true);
deSerializeDs(CLIENT_CLASS, VERSION_10_10_1_0, true);
deSerializeDs(CLIENT_CLASS + _40Suffix, VERSION_10_10_1_0, true);
}
final String CLIENT_CLASS = "BasicClientDataSource40";
deSerializeDs(CLIENT_CLASS, VERSION_10_10_1_0, false);
}
/**
* Tests the de-serialization of the client connection pool data source.
*
* @throws Exception for a number of error conditions
*/
public void serTestClientConnectionPoolDataSource()
throws Exception {
if (JDBC.vmSupportsJNDI()) {
final String CLIENT_CLASS = "ClientConnectionPoolDataSource";
// No client driver for Derby 10.0
deSerializeDs(CLIENT_CLASS, VERSION_10_1_3_1, true);
deSerializeDs(CLIENT_CLASS, VERSION_10_2_2_0, true);
deSerializeDs(CLIENT_CLASS, VERSION_10_3_2_1, true);
deSerializeDs(CLIENT_CLASS, VERSION_10_10_1_0, true);
deSerializeDs(CLIENT_CLASS + _40Suffix, VERSION_10_10_1_0, true);
}
final String CLIENT_CLASS = "BasicClientConnectionPoolDataSource40";
deSerializeDs(CLIENT_CLASS, VERSION_10_10_1_0, false);
}
/**
* Tests the de-serialization of the client XA data source.
*
* @throws Exception for a number of error conditions
*/
public void serTestClientXADataSource()
throws Exception {
if (JDBC.vmSupportsJNDI()) {
final String CLIENT_CLASS = "ClientXADataSource";
// No client driver for Derby 10.0
deSerializeDs(CLIENT_CLASS, VERSION_10_1_3_1, true);
deSerializeDs(CLIENT_CLASS, VERSION_10_2_2_0, true);
deSerializeDs(CLIENT_CLASS, VERSION_10_3_2_1, true);
deSerializeDs(CLIENT_CLASS, VERSION_10_10_1_0, true);
deSerializeDs(CLIENT_CLASS + _40Suffix, VERSION_10_10_1_0, true);
}
final String CLIENT_CLASS = "BasicClientXADataSource40";
deSerializeDs(CLIENT_CLASS, VERSION_10_10_1_0, false);
}
/**
* Attempts to de-serialize a data source object from a file.
* <p>
* <ol> <li>Derby version string - UTF</li>
* <li>Derby build number - UTF</li>
* <li>Derby data source - object</li>
* <li>Derby data source reference - object</li>
* </ol>
* <p>
* If the object is successfully instantiated and cast to
* {@link javax.sql.DataSource}
*
* @param className name of the class to de-serialize
* @param version Derby version
*
* @throws Exception on a number of error conditions
*/
private void deSerializeDs(
String className, String version, boolean dsHasJNDI)
throws Exception {
if (!JDBC.vmSupportsJDBC4() && className.contains("40")) {
// Running old Java, bail out if JDBC4
return;
}
// Construct the filename
final StringBuffer fname = new StringBuffer(className);
fname.append('-');
fname.append(version);
fname.append(".ser");
println( "Deserializing " + fname.toString() );
// De-serialize the data source.
InputStream is;
try {
is = AccessController.doPrivileged(
new PrivilegedExceptionAction<InputStream>() {
public InputStream run() throws FileNotFoundException {
return new FileInputStream(
SupportFilesSetup.getReadOnly(fname.toString()));
}
});
} catch (PrivilegedActionException e) {
// e.getException() should be a FileNotFoundException.
throw (FileNotFoundException)e.getException();
}
assertNotNull("FileInputStream is null", is);
Object dsObj = null;
DataSource ds = null;
Object dsRef = null;
// Used to preserve original error information in case of exception when
// closing the input stream.
boolean testSequencePassed = false;
try {
ObjectInputStream ois = new ObjectInputStream(is);
String buildVersion = ois.readUTF();
String buildNumber = ois.readUTF();
println("Data source " + className + ", version " +
buildVersion + ", build " + buildNumber);
dsObj = ois.readObject();
assertNotNull("De-serialized data source is null", dsObj);
assertTrue("Unexpected class instantiated: " +
dsObj.getClass().getName(),
dsObj.getClass().getName().indexOf(className) > 0);
ds = (DataSource)dsObj;
// Just see if the object is usable.
int newTimeout = ds.getLoginTimeout() +9;
assertFalse(ds.getLoginTimeout() == newTimeout);
ds.setLoginTimeout(newTimeout);
assertEquals(newTimeout, ds.getLoginTimeout());
if (dsHasJNDI) {
// Recreate the data source using reference.
dsRef = ois.readObject();
}
ois.close();
testSequencePassed = true;
} finally {
if (testSequencePassed) {
is.close();
} else {
try {
is.close();
} catch (IOException ioe) {
// Ignore this to preserve the original exception.
}
}
}
if (dsHasJNDI) {
// Recreate ds via the Reference's factory class. We use
// reflection here to make the test runnable for non JNDI
// environments. (Even though this code would not be executed in
// that environment, the VM will try to load the classes if
// reference directly in the source. So, resort to reflection...)
Method getFactoryClassName =
Class.forName("javax.naming.Reference").getMethod(
"getFactoryClassName", null);
String factoryClassName =
(String)getFactoryClassName.invoke(dsRef, null);
Class<?> clazz = Class.forName(factoryClassName);
Object factory = clazz.getConstructor().newInstance();
Method getObjectInstance =
factory.getClass().getMethod("getObjectInstance",
new Class[] {
Class.forName("java.lang.Object"),
Class.forName("javax.naming.Name"),
Class.forName("javax.naming.Context"),
Class.forName( "java.util.Hashtable")});
Object recreatedDs =
getObjectInstance.invoke(
factory, new Object[] {dsRef, null, null, null});
ds = (DataSource)recreatedDs;
assertTrue("Unexpected class instantiated by Reference: " +
dsObj.getClass().getName(),
dsObj.getClass().getName().indexOf(className) > 0);
}
}
/**
* Returns an appropariate suite of tests to run.
*
* @return A test suite.
*/
public static Test suite() {
BaseTestSuite suite =
new BaseTestSuite("DataSourceSerializationTest");
String filePrefix = "functionTests/testData/serializedDataSources/";
// De-serialize embedded data sources only if we have the engine code.
if (Derby.hasEmbedded()) {
suite.addTest(new DataSourceSerializationTest(
"serTestEmbeddedDataSource"));
suite.addTest(new DataSourceSerializationTest(
"serTestEmbeddedConnectionPoolDataSource"));
suite.addTest(new DataSourceSerializationTest(
"serTestEmbeddedXADataSource"));
}
// De-serialize client data sources only if we have the client code.
if (Derby.hasClient()) {
suite.addTest(new DataSourceSerializationTest(
"serTestClientDataSource"));
suite.addTest(new DataSourceSerializationTest(
"serTestClientConnectionPoolDataSource"));
suite.addTest(new DataSourceSerializationTest(
"serTestClientXADataSource"));
}
return new SupportFilesSetup(suite, new String[] {
// 10.0 resources
filePrefix + "EmbeddedDataSource-10_0_2_1.ser",
filePrefix + "EmbeddedConnectionPoolDataSource-10_0_2_1.ser",
filePrefix + "EmbeddedXADataSource-10_0_2_1.ser",
// 10.1 resources
filePrefix + "EmbeddedDataSource-10_1_3_1.ser",
filePrefix + "EmbeddedConnectionPoolDataSource-10_1_3_1.ser",
filePrefix + "EmbeddedXADataSource-10_1_3_1.ser",
filePrefix + "ClientDataSource-10_1_3_1.ser",
filePrefix + "ClientConnectionPoolDataSource-10_1_3_1.ser",
filePrefix + "ClientXADataSource-10_1_3_1.ser",
// 10.2 resources
filePrefix + "EmbeddedDataSource-10_2_2_0.ser",
filePrefix + "EmbeddedConnectionPoolDataSource-10_2_2_0.ser",
filePrefix + "EmbeddedXADataSource-10_2_2_0.ser",
filePrefix + "ClientDataSource-10_2_2_0.ser",
filePrefix + "ClientConnectionPoolDataSource-10_2_2_0.ser",
filePrefix + "ClientXADataSource-10_2_2_0.ser",
// 10.3 resources
filePrefix + "EmbeddedDataSource-10_3_2_1.ser",
filePrefix + "EmbeddedConnectionPoolDataSource-10_3_2_1.ser",
filePrefix + "EmbeddedXADataSource-10_3_2_1.ser",
filePrefix + "ClientDataSource-10_3_2_1.ser",
filePrefix + "ClientConnectionPoolDataSource-10_3_2_1.ser",
filePrefix + "ClientXADataSource-10_3_2_1.ser",
// 10.10 resources
filePrefix + "EmbeddedDataSource-10_10_1_0.ser",
filePrefix + "EmbeddedDataSource40-10_10_1_0.ser",
filePrefix + "EmbeddedConnectionPoolDataSource-10_10_1_0.ser",
filePrefix + "EmbeddedConnectionPoolDataSource40-10_10_1_0.ser",
filePrefix + "EmbeddedXADataSource-10_10_1_0.ser",
filePrefix + "EmbeddedXADataSource40-10_10_1_0.ser",
filePrefix + "ClientDataSource-10_10_1_0.ser",
filePrefix + "ClientDataSource40-10_10_1_0.ser",
filePrefix + "ClientConnectionPoolDataSource-10_10_1_0.ser",
filePrefix + "ClientConnectionPoolDataSource40-10_10_1_0.ser",
filePrefix + "ClientXADataSource-10_10_1_0.ser",
filePrefix + "ClientXADataSource40-10_10_1_0.ser",
filePrefix + "BasicEmbeddedDataSource40-10_10_1_0.ser",
filePrefix +
"BasicEmbeddedConnectionPoolDataSource40-10_10_1_0.ser",
filePrefix + "BasicEmbeddedXADataSource40-10_10_1_0.ser",
filePrefix + "BasicClientDataSource40-10_10_1_0.ser",
filePrefix +
"BasicClientConnectionPoolDataSource40-10_10_1_0.ser",
filePrefix + "BasicClientXADataSource40-10_10_1_0.ser",
// 10.11 resources
filePrefix + "EmbeddedDataSource-10_11_1_0.ser",
filePrefix + "EmbeddedDataSource40-10_11_1_0.ser",
filePrefix + "EmbeddedConnectionPoolDataSource-10_11_1_0.ser",
filePrefix + "EmbeddedConnectionPoolDataSource40-10_11_1_0.ser",
filePrefix + "EmbeddedXADataSource-10_11_1_0.ser",
filePrefix + "EmbeddedXADataSource40-10_11_1_0.ser",
filePrefix + "ClientDataSource-10_11_1_0.ser",
filePrefix + "ClientDataSource40-10_11_1_0.ser",
filePrefix + "ClientConnectionPoolDataSource-10_11_1_0.ser",
filePrefix + "ClientConnectionPoolDataSource40-10_11_1_0.ser",
filePrefix + "ClientXADataSource-10_11_1_0.ser",
filePrefix + "ClientXADataSource40-10_11_1_0.ser",
filePrefix + "BasicEmbeddedDataSource40-10_11_1_0.ser",
filePrefix +
"BasicEmbeddedConnectionPoolDataSource40-10_11_1_0.ser",
filePrefix + "BasicEmbeddedXADataSource40-10_11_1_0.ser",
filePrefix + "BasicClientDataSource40-10_11_1_0.ser",
filePrefix +
"BasicClientConnectionPoolDataSource40-10_11_1_0.ser",
filePrefix + "BasicClientXADataSource40-10_11_1_0.ser",
});
}
}
|