-
Notifications
You must be signed in to change notification settings - Fork 231
[BEANUTILS-417] Refactor serialization methods to conform to Java Object Serialization Specification #430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 1.X
Are you sure you want to change the base?
[BEANUTILS-417] Refactor serialization methods to conform to Java Object Serialization Specification #430
Changes from 4 commits
de7ae87
7370240
2ce79e2
ceb91fd
eb2d854
c8e1ede
2c2a6b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| .settings/ | ||
| /site-content/ | ||
| /.idea/ | ||
| *.iml | ||
|
|
||
| # NetBeans files | ||
| nb-configuration.xml | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -247,8 +247,7 @@ private Class<?> readAnyClass(final ObjectInputStream in) throws IOException, Cl | |
| default: | ||
| // something's gone wrong | ||
| throw new StreamCorruptedException( | ||
| "Invalid primitive type. " | ||
| + "Check version of beanutils used to serialize is compatible."); | ||
| "Invalid primitive type. Check version of beanutils used to serialize is compatible."); | ||
|
|
||
| } | ||
| } | ||
|
|
@@ -262,12 +261,14 @@ private Class<?> readAnyClass(final ObjectInputStream in) throws IOException, Cl | |
| * @throws ClassNotFoundException Class of a serialized object cannot be found. | ||
| */ | ||
| private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { | ||
| // read default values | ||
| in.defaultReadObject(); | ||
|
|
||
| // read custom values | ||
| this.type = readAnyClass(in); | ||
| if (isMapped() || isIndexed()) { | ||
| this.contentType = readAnyClass(in); | ||
| } | ||
| // read other values | ||
| in.defaultReadObject(); | ||
| } | ||
|
Comment on lines
261
to
270
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll just bump serialVersionUID, as supporting both serialization strategies is non-trivial to impossible. |
||
|
|
||
| /** | ||
|
|
@@ -333,11 +334,13 @@ private void writeAnyClass(final Class<?> clazz, final ObjectOutputStream out) t | |
| * @throws IOException if I/O errors occur while writing to the underlying stream. | ||
| */ | ||
| private void writeObject(final ObjectOutputStream out) throws IOException { | ||
| writeAnyClass(this.type,out); | ||
| // write out default | ||
| out.defaultWriteObject(); | ||
|
|
||
| // write custom values | ||
| writeAnyClass(this.type, out); | ||
| if (isMapped() || isIndexed()) { | ||
| writeAnyClass(this.contentType,out); | ||
| writeAnyClass(this.contentType, out); | ||
| } | ||
| // write out other values | ||
| out.defaultWriteObject(); | ||
| } | ||
|
Comment on lines
334
to
343
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. writeObject() wouldn't need to be adapted, as we want to write the new format always. However, I'll just bump serialVersionUID, as supporting both serialization strategies is non-trivial to impossible. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,16 @@ | |
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
|
||
| import java.util.Collection; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.ObjectInputStream; | ||
| import java.io.ObjectOutputStream; | ||
| import java.util.List; | ||
|
|
||
| import org.jboss.marshalling.cloner.ClassLoaderClassCloner; | ||
| import org.jboss.marshalling.cloner.ClonerConfiguration; | ||
| import org.jboss.marshalling.cloner.ObjectCloner; | ||
| import org.jboss.marshalling.cloner.ObjectCloners; | ||
|
|
||
| import junit.framework.TestCase; | ||
|
|
||
|
|
@@ -59,8 +68,8 @@ protected void setUp() throws Exception { | |
| testProperty1Duplicate = new DynaProperty("test1"); | ||
| testPropertyWithNameAndType = new DynaProperty("test2", Integer.class); | ||
| testProperty2Duplicate = new DynaProperty("test2", Integer.class); | ||
| testPropertyWithNameAndTypeAndContentType = new DynaProperty("test3", Collection.class, Short.class); | ||
| testProperty3Duplicate = new DynaProperty("test3", Collection.class, Short.class); | ||
| testPropertyWithNameAndTypeAndContentType = new DynaProperty("test3", List.class, Short.class); | ||
| testProperty3Duplicate = new DynaProperty("test3", List.class, Short.class); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -96,4 +105,33 @@ public void testHashCode() { | |
| assertEquals(testPropertyWithNameAndTypeAndContentType.hashCode(), testProperty3Duplicate.hashCode()); | ||
| assertEquals(initialHashCode, testPropertyWithNameAndTypeAndContentType.hashCode()); | ||
| } | ||
|
|
||
| /** | ||
| * Tests basic serialization and deserialization mechanism. | ||
| */ | ||
| public void testSerialization() throws Exception { | ||
| ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | ||
| ObjectOutputStream oos = new ObjectOutputStream(buffer); | ||
| oos.writeObject(testPropertyWithNameAndTypeAndContentType); | ||
| oos.flush(); | ||
| oos.close(); | ||
|
|
||
| ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); | ||
| Object obj = ois.readObject(); | ||
|
|
||
| assertEquals(testPropertyWithNameAndTypeAndContentType, obj); | ||
| } | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
|
||
| /** | ||
| * Tests cloning mechanism via Wildfly Object Cloner. | ||
| */ | ||
| public void testCloneViaWildflyObjectCloner() throws Exception { | ||
| final ClonerConfiguration paramConfig = new ClonerConfiguration(); | ||
| paramConfig.setClassCloner(new ClassLoaderClassCloner(DynaPropertyTest.class.getClassLoader())); | ||
| final ObjectCloner objectCloner = ObjectCloners.getSerializingObjectClonerFactory().createCloner(paramConfig); | ||
|
|
||
| final Object cloned = objectCloner.clone(testPropertyWithNameAndTypeAndContentType); | ||
|
|
||
| assertEquals(testPropertyWithNameAndTypeAndContentType, cloned); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.