mirror of https://github.com/apache/openjpa.git
OPENJPA-2035: setting svn:eol-style on new file
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1150700 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
03f054f8c9
commit
22356a66e7
|
@ -1,156 +1,156 @@
|
||||||
/*
|
/*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
* or more contributor license agreements. See the NOTICE file
|
* or more contributor license agreements. See the NOTICE file
|
||||||
* distributed with this work for additional information
|
* distributed with this work for additional information
|
||||||
* regarding copyright ownership. The ASF licenses this file
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
* to you under the Apache License, Version 2.0 (the
|
* to you under the Apache License, Version 2.0 (the
|
||||||
* "License"); you may not use this file except in compliance
|
* "License"); you may not use this file except in compliance
|
||||||
* with the License. You may obtain a copy of the License at
|
* with the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing,
|
* Unless required by applicable law or agreed to in writing,
|
||||||
* software distributed under the License is distributed on an
|
* software distributed under the License is distributed on an
|
||||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
* KIND, either express or implied. See the License for the
|
* KIND, either express or implied. See the License for the
|
||||||
* specific language governing permissions and limitations
|
* specific language governing permissions and limitations
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
package org.apache.openjpa.kernel;
|
package org.apache.openjpa.kernel;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.apache.openjpa.conf.OpenJPAConfiguration;
|
import org.apache.openjpa.conf.OpenJPAConfiguration;
|
||||||
import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
|
import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
|
||||||
import org.apache.openjpa.enhance.DynamicStorage;
|
import org.apache.openjpa.enhance.DynamicStorage;
|
||||||
import org.apache.openjpa.enhance.DynamicStorageGenerator;
|
import org.apache.openjpa.enhance.DynamicStorageGenerator;
|
||||||
import org.apache.openjpa.meta.ClassMetaData;
|
import org.apache.openjpa.meta.ClassMetaData;
|
||||||
import org.apache.openjpa.meta.JavaTypes;
|
import org.apache.openjpa.meta.JavaTypes;
|
||||||
import org.apache.openjpa.meta.MetaDataRepository;
|
import org.apache.openjpa.meta.MetaDataRepository;
|
||||||
import org.apache.openjpa.util.IntId;
|
import org.apache.openjpa.util.IntId;
|
||||||
import org.apache.openjpa.util.OpenJPAId;
|
import org.apache.openjpa.util.OpenJPAId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This test ensures that we can stream a PCData and OpenJPAId to a client which may not have the Entities on it's
|
* This test ensures that we can stream a PCData and OpenJPAId to a client which may not have the Entities on it's
|
||||||
* classpath. In a real use case we would have multiple processes, but for the sake of unit testing this behavior is
|
* classpath. In a real use case we would have multiple processes, but for the sake of unit testing this behavior is
|
||||||
* simulated via multiple classloaders.
|
* simulated via multiple classloaders.
|
||||||
*/
|
*/
|
||||||
public class TestPCDataSerialization extends TestCase {
|
public class TestPCDataSerialization extends TestCase {
|
||||||
ClassLoader _ccl;
|
ClassLoader _ccl;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
_ccl = Thread.currentThread().getContextClassLoader();
|
_ccl = Thread.currentThread().getContextClassLoader();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void tearDown() throws Exception {
|
protected void tearDown() throws Exception {
|
||||||
Thread.currentThread().setContextClassLoader(_ccl);
|
Thread.currentThread().setContextClassLoader(_ccl);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test() throws Exception {
|
public void test() throws Exception {
|
||||||
// Generate a new class. This will create a new class, and load it with a new classloader.
|
// Generate a new class. This will create a new class, and load it with a new classloader.
|
||||||
Class<?> cls = generateClass();
|
Class<?> cls = generateClass();
|
||||||
ClassLoader newLoader = cls.getClassLoader();
|
ClassLoader newLoader = cls.getClassLoader();
|
||||||
Thread.currentThread().setContextClassLoader(newLoader);
|
Thread.currentThread().setContextClassLoader(newLoader);
|
||||||
|
|
||||||
// Create moc objects
|
// Create moc objects
|
||||||
MetaDataRepository repo = new DummyMetaDataRepository();
|
MetaDataRepository repo = new DummyMetaDataRepository();
|
||||||
ClassMetaData cmd = new DummyClassMetaData(cls, repo);
|
ClassMetaData cmd = new DummyClassMetaData(cls, repo);
|
||||||
|
|
||||||
OpenJPAId oid = new IntId(cls, 7);
|
OpenJPAId oid = new IntId(cls, 7);
|
||||||
PCDataImpl pcdi = new PCDataImpl(oid, cmd);
|
PCDataImpl pcdi = new PCDataImpl(oid, cmd);
|
||||||
|
|
||||||
// Write the object out using the newly created classloader
|
// Write the object out using the newly created classloader
|
||||||
byte[] bytes = writeObject(pcdi);
|
byte[] bytes = writeObject(pcdi);
|
||||||
|
|
||||||
// Switch contextclassloader back to the original and try to deserialize
|
// Switch contextclassloader back to the original and try to deserialize
|
||||||
Thread.currentThread().setContextClassLoader(_ccl);
|
Thread.currentThread().setContextClassLoader(_ccl);
|
||||||
|
|
||||||
pcdi = (PCDataImpl) readObject(bytes);
|
pcdi = (PCDataImpl) readObject(bytes);
|
||||||
assertNotNull(pcdi);
|
assertNotNull(pcdi);
|
||||||
try {
|
try {
|
||||||
// This will throw a wrapped ClassNotFoundException because the domain class isn't available.
|
// This will throw a wrapped ClassNotFoundException because the domain class isn't available.
|
||||||
pcdi.getType();
|
pcdi.getType();
|
||||||
fail("Should have thrown an exception.");
|
fail("Should have thrown an exception.");
|
||||||
} catch (RuntimeException cnfe) {
|
} catch (RuntimeException cnfe) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
// Write object without the class
|
// Write object without the class
|
||||||
bytes = writeObject(pcdi);
|
bytes = writeObject(pcdi);
|
||||||
|
|
||||||
// Switch to loader that has the new class and make sure we find it again.
|
// Switch to loader that has the new class and make sure we find it again.
|
||||||
Thread.currentThread().setContextClassLoader(newLoader);
|
Thread.currentThread().setContextClassLoader(newLoader);
|
||||||
pcdi = (PCDataImpl) readObject(bytes);
|
pcdi = (PCDataImpl) readObject(bytes);
|
||||||
assertNotNull(pcdi);
|
assertNotNull(pcdi);
|
||||||
assertEquals(cls, pcdi.getType());
|
assertEquals(cls, pcdi.getType());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] writeObject(Object o) throws Exception {
|
private byte[] writeObject(Object o) throws Exception {
|
||||||
ByteArrayOutputStream baos = null;
|
ByteArrayOutputStream baos = null;
|
||||||
ObjectOutputStream oos = null;
|
ObjectOutputStream oos = null;
|
||||||
try {
|
try {
|
||||||
baos = new ByteArrayOutputStream();
|
baos = new ByteArrayOutputStream();
|
||||||
oos = new ObjectOutputStream(baos);
|
oos = new ObjectOutputStream(baos);
|
||||||
oos.writeObject(o);
|
oos.writeObject(o);
|
||||||
|
|
||||||
return baos.toByteArray();
|
return baos.toByteArray();
|
||||||
} finally {
|
} finally {
|
||||||
if (oos != null) {
|
if (oos != null) {
|
||||||
oos.close();
|
oos.close();
|
||||||
}
|
}
|
||||||
if (baos != null) {
|
if (baos != null) {
|
||||||
baos.close();
|
baos.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object readObject(byte[] bytes) throws Exception {
|
private Object readObject(byte[] bytes) throws Exception {
|
||||||
ByteArrayInputStream bais = null;
|
ByteArrayInputStream bais = null;
|
||||||
ObjectInputStream ois = null;
|
ObjectInputStream ois = null;
|
||||||
try {
|
try {
|
||||||
bais = new ByteArrayInputStream(bytes);
|
bais = new ByteArrayInputStream(bytes);
|
||||||
ois = new ObjectInputStream(bais);
|
ois = new ObjectInputStream(bais);
|
||||||
return ois.readObject();
|
return ois.readObject();
|
||||||
} finally {
|
} finally {
|
||||||
if (ois != null) {
|
if (ois != null) {
|
||||||
ois.close();
|
ois.close();
|
||||||
}
|
}
|
||||||
if (bais != null) {
|
if (bais != null) {
|
||||||
bais.close();
|
bais.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Class<?> generateClass() {
|
private Class<?> generateClass() {
|
||||||
DynamicStorageGenerator gen = new DynamicStorageGenerator();
|
DynamicStorageGenerator gen = new DynamicStorageGenerator();
|
||||||
int[] types =
|
int[] types =
|
||||||
new int[] { JavaTypes.BOOLEAN, JavaTypes.BYTE, JavaTypes.CHAR, JavaTypes.INT, JavaTypes.SHORT,
|
new int[] { JavaTypes.BOOLEAN, JavaTypes.BYTE, JavaTypes.CHAR, JavaTypes.INT, JavaTypes.SHORT,
|
||||||
JavaTypes.LONG, JavaTypes.FLOAT, JavaTypes.DOUBLE, JavaTypes.STRING, JavaTypes.OBJECT };
|
JavaTypes.LONG, JavaTypes.FLOAT, JavaTypes.DOUBLE, JavaTypes.STRING, JavaTypes.OBJECT };
|
||||||
DynamicStorage storage = gen.generateStorage(types, "org.apache.openjpa.enhance.Test");
|
DynamicStorage storage = gen.generateStorage(types, "org.apache.openjpa.enhance.Test");
|
||||||
storage = storage.newInstance();
|
storage = storage.newInstance();
|
||||||
|
|
||||||
return storage.getClass();
|
return storage.getClass();
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
class DummyClassMetaData extends ClassMetaData {
|
class DummyClassMetaData extends ClassMetaData {
|
||||||
public DummyClassMetaData(Class<?> cls, MetaDataRepository repo) {
|
public DummyClassMetaData(Class<?> cls, MetaDataRepository repo) {
|
||||||
super(cls, repo);
|
super(cls, repo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
class DummyMetaDataRepository extends MetaDataRepository {
|
class DummyMetaDataRepository extends MetaDataRepository {
|
||||||
@Override
|
@Override
|
||||||
public OpenJPAConfiguration getConfiguration() {
|
public OpenJPAConfiguration getConfiguration() {
|
||||||
return new OpenJPAConfigurationImpl();
|
return new OpenJPAConfigurationImpl();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue