-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16302 from famod/test-paramresolver-record
Junit5 parameter: Introduce serialization alternative to XStream
- Loading branch information
Showing
5 changed files
with
162 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...framework/junit5/src/main/java/io/quarkus/test/junit/internal/SerializationDeepClone.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.quarkus.test.junit.internal; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.io.ObjectStreamClass; | ||
|
||
/** | ||
* Cloning strategy that just serializes and deserializes using plain old java serialization. | ||
*/ | ||
class SerializationDeepClone implements DeepClone { | ||
|
||
private final ClassLoader classLoader; | ||
|
||
SerializationDeepClone(ClassLoader classLoader) { | ||
this.classLoader = classLoader; | ||
} | ||
|
||
@Override | ||
public Object clone(Object objectToClone) { | ||
ByteArrayOutputStream byteOut = new ByteArrayOutputStream(512); | ||
try (ObjectOutputStream objOut = new ObjectOutputStream(byteOut)) { | ||
objOut.writeObject(objectToClone); | ||
try (ObjectInputStream objIn = new ClassLoaderAwareObjectInputStream(byteOut)) { | ||
return objIn.readObject(); | ||
} | ||
} catch (IOException | ClassNotFoundException e) { | ||
throw new IllegalStateException("Unable to deep clone object of type '" + objectToClone.getClass().getName() | ||
+ "'. Please report the issue on the Quarkus issue tracker.", e); | ||
} | ||
} | ||
|
||
private class ClassLoaderAwareObjectInputStream extends ObjectInputStream { | ||
|
||
public ClassLoaderAwareObjectInputStream(ByteArrayOutputStream byteOut) throws IOException { | ||
super(new ByteArrayInputStream(byteOut.toByteArray())); | ||
} | ||
|
||
@Override | ||
protected Class<?> resolveClass(ObjectStreamClass desc) throws ClassNotFoundException { | ||
return Class.forName(desc.getName(), true, classLoader); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...c/main/java/io/quarkus/test/junit/internal/SerializationWithXStreamFallbackDeepClone.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.quarkus.test.junit.internal; | ||
|
||
import java.io.Serializable; | ||
import java.util.Optional; | ||
|
||
import org.jboss.logging.Logger; | ||
|
||
/** | ||
* Cloning strategy delegating to {@link SerializationDeepClone}, falling back to {@link XStreamDeepClone} in case of error. | ||
*/ | ||
public class SerializationWithXStreamFallbackDeepClone implements DeepClone { | ||
|
||
private static final Logger LOG = Logger.getLogger(SerializationWithXStreamFallbackDeepClone.class); | ||
|
||
private final SerializationDeepClone serializationDeepClone; | ||
private final XStreamDeepClone xStreamDeepClone; | ||
|
||
public SerializationWithXStreamFallbackDeepClone(ClassLoader classLoader) { | ||
this.serializationDeepClone = new SerializationDeepClone(classLoader); | ||
this.xStreamDeepClone = new XStreamDeepClone(classLoader); | ||
} | ||
|
||
@Override | ||
public Object clone(Object objectToClone) { | ||
if (objectToClone instanceof Serializable) { | ||
try { | ||
return serializationDeepClone.clone(objectToClone); | ||
} catch (RuntimeException re) { | ||
LOG.debugf("SerializationDeepClone failed (will fall back to XStream): %s", | ||
Optional.ofNullable(re.getCause()).orElse(re)); | ||
} | ||
} | ||
return xStreamDeepClone.clone(objectToClone); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters