Skip to content

Commit

Permalink
A test to demonstrate (our current lack of) handling of transient pri…
Browse files Browse the repository at this point in the history
…mitive

fields in serialized objects.
  • Loading branch information
samskivert committed Jan 21, 2011
1 parent 8657960 commit 5078ace
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<path id="base.classpath">
<pathelement location="${classes.dir}"/>
<fileset dir="${deploy.dir}/lib" includes="*.jar"/>
<fileset dir="${javac.home}/lib" includes="${javac.jar}"/>
</path>
<path id="build.classpath">
<path refid="base.classpath"/>
Expand Down
132 changes: 132 additions & 0 deletions src/test/java/org/ductilej/tests/SerializePrimsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//
// $Id$

package org.ductilej.tests;

import java.io.*;

import org.junit.Test;
import static org.junit.Assert.*;

/**
* Tests the special handling for unserializing transient primitive fields.
*/
public class SerializePrimsTest
{
public static class Record implements Serializable
{
public transient boolean booleanValue;
public transient byte byteValue;
public transient short shortValue;
public transient char charValue;
public transient int intValue;
public transient long longValue;
public transient float floatValue;
public transient double doubleValue;
}

public static class CustomRecord implements Serializable
{
// these initializations will *not* be done automatically when this object is unserialized
// (whether detyped or not), but we're mimicking a situation where a programmer chooses to
// have a transient field for which they provide both a constructor-based initializer and a
// readObject-based initializer, and we want to make sure that we don't step on their toes
public transient boolean booleanValue = true;
public transient byte byteValue = 1;
public transient short shortValue = 1;
public transient char charValue = 1;
public transient int intValue = 1;
public transient long longValue = 1;
public transient float floatValue = 1;
public transient double doubleValue = 1;

// we want to make sure that these non-transient values are still properly handled by the
// call to in.defaultReadObject() below
public boolean booleanValueS = true;
public byte byteValueS = 1;
public short shortValueS = 1;
public char charValueS = 'a';
public int intValueS = 1;
public long longValueS = 1;
public float floatValueS = 1;
public double doubleValueS = 1;

private void readObject (ObjectInputStream in)
throws IOException, ClassNotFoundException {
// the default initializers will be inserted here (above the initializers provided by
// the programmer); so though we will do some unnecessary work with our default
// initialization, everything will still work correctly
booleanValue = true;
byteValue = 1;
shortValue = 1;
charValue = 1;
intValue = 1;
longValue = 1;
floatValue = 1;
doubleValue = 1;
in.defaultReadObject();
}
}

@Test public void testTransientPrimitives ()
{
Record r = new Record();
Record nr = (Record)fromBytes(toBytes(r));

assertEquals(r.booleanValue, nr.booleanValue);
assertEquals(r.byteValue, nr.byteValue);
assertEquals(r.shortValue, nr.shortValue);
assertEquals(r.charValue, nr.charValue);
assertEquals(r.intValue, nr.intValue);
assertEquals(r.longValue, nr.longValue);
assertEquals(r.floatValue, nr.floatValue, 0f);
assertEquals(r.doubleValue, nr.doubleValue, 0.0);
}

@Test public void testCustomTransientPrimitives ()
{
CustomRecord r = new CustomRecord();
CustomRecord nr = (CustomRecord)fromBytes(toBytes(r));

assertEquals(r.booleanValue, nr.booleanValue);
assertEquals(r.byteValue, nr.byteValue);
assertEquals(r.shortValue, nr.shortValue);
assertEquals(r.charValue, nr.charValue);
assertEquals(r.intValue, nr.intValue);
assertEquals(r.longValue, nr.longValue);
assertEquals(r.floatValue, nr.floatValue, 0f);
assertEquals(r.doubleValue, nr.doubleValue, 0.0);

assertEquals(r.booleanValueS, nr.booleanValueS);
assertEquals(r.byteValueS, nr.byteValueS);
assertEquals(r.shortValueS, nr.shortValueS);
assertEquals(r.charValueS, nr.charValueS);
assertEquals(r.intValueS, nr.intValueS);
assertEquals(r.longValueS, nr.longValueS);
assertEquals(r.floatValueS, nr.floatValueS, 0f);
assertEquals(r.doubleValueS, nr.doubleValueS, 0.0);
}

public static byte[] toBytes (Serializable s)
{
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(s);
out.close();
return bout.toByteArray();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public static Object fromBytes (byte[] bytes)
{
try {
ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(bytes));
return oin.readObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 5078ace

Please sign in to comment.