mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-11-04 01:59:35 +00:00
61 lines
2.3 KiB
Java
Executable File
61 lines
2.3 KiB
Java
Executable File
package com.sap.sailing.domain.test;
|
|
|
|
import java.io.IOException;
|
|
import java.io.ObjectInputStream;
|
|
import java.io.ObjectOutputStream;
|
|
import java.io.PipedInputStream;
|
|
import java.io.PipedOutputStream;
|
|
import java.io.Serializable;
|
|
|
|
import com.sap.sailing.domain.base.DomainFactory;
|
|
|
|
public abstract class AbstractSerializationTest {
|
|
static <T extends Serializable> T cloneBySerialization(final T s, DomainFactory resolveAgainst) throws IOException, ClassNotFoundException {
|
|
PipedOutputStream pos = new PipedOutputStream();
|
|
PipedInputStream pis = new PipedInputStream(pos);
|
|
final ObjectOutputStream dos = new ObjectOutputStream(pos);
|
|
new Thread("clone writer") {
|
|
public void run() {
|
|
try {
|
|
dos.writeObject(s);
|
|
dos.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}.start();
|
|
ObjectInputStream dis = resolveAgainst.createObjectInputStreamResolvingAgainstThisFactory(pis);
|
|
@SuppressWarnings("unchecked")
|
|
T result = (T) dis.readObject();
|
|
dis.close();
|
|
return result;
|
|
}
|
|
|
|
static Object[] cloneManyBySerialization(DomainFactory resolveAgainst, final Serializable... objects) throws IOException, ClassNotFoundException {
|
|
PipedOutputStream pos = new PipedOutputStream();
|
|
PipedInputStream pis = new PipedInputStream(pos);
|
|
final ObjectOutputStream dos = new ObjectOutputStream(pos);
|
|
new Thread("clone writer") {
|
|
public void run() {
|
|
try {
|
|
for (Serializable s : objects) {
|
|
dos.writeObject(s);
|
|
}
|
|
dos.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}.start();
|
|
ObjectInputStream dis = resolveAgainst.createObjectInputStreamResolvingAgainstThisFactory(pis);
|
|
Object[] result = new Object[objects.length];
|
|
for (int i=0; i<objects.length; i++) {
|
|
result[i] = dis.readObject();
|
|
}
|
|
dis.close();
|
|
return result;
|
|
}
|
|
}
|