Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.settings/
/site-content/
/.idea/
*.iml

# NetBeans files
nb-configuration.xml
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb3</artifactId>
<version>9.0.2.Final</version>
<scope>test</scope>
</dependency>
Comment on lines +106 to +111

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/org/apache/commons/beanutils/DynaProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

}
}
Expand All @@ -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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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.


/**
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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.

}
44 changes: 41 additions & 3 deletions src/test/java/org/apache/commons/beanutils/DynaPropertyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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);
}
}
Loading