diff --git a/querydsl-core/src/test/java/com/mysema/query/types/SerializationTest.java b/querydsl-core/src/test/java/com/mysema/query/types/SerializationTest.java new file mode 100644 index 000000000..d84d47dbe --- /dev/null +++ b/querydsl-core/src/test/java/com/mysema/query/types/SerializationTest.java @@ -0,0 +1,35 @@ +package com.mysema.query.types; + +import java.io.*; + +import com.mysema.query.types.path.SimplePath; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class SerializationTest { + + @Test + public void roundtrip() throws Exception { + PathImpl path = new PathImpl(Object.class, "entity"); + SimplePath path2 = new SimplePath(Object.class, "entity"); + assertEquals(path, roundtrip(path)); + assertEquals(path2, roundtrip(path2)); + assertEquals(path2.isNull(), roundtrip(path2.isNull())); + assertEquals(path.hashCode(), roundtrip(path).hashCode()); + assertEquals(path2.hashCode(), roundtrip(path2).hashCode()); + assertEquals(path2.isNull().hashCode(), roundtrip(path2.isNull()).hashCode()); + } + + private T roundtrip(T obj) throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(baos); + out.writeObject(obj); + out.close(); + + // deserialize predicate + ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray()); + ObjectInputStream in = new ObjectInputStream(bain); + return (T) in.readObject(); + } + +} diff --git a/querydsl-jpa/src/test/java/com/mysema/query/jpa/SerializationBase.java b/querydsl-jpa/src/test/java/com/mysema/query/jpa/SerializationBase.java index aaf97856a..ff3c3038b 100644 --- a/querydsl-jpa/src/test/java/com/mysema/query/jpa/SerializationBase.java +++ b/querydsl-jpa/src/test/java/com/mysema/query/jpa/SerializationBase.java @@ -69,7 +69,7 @@ public class SerializationBase { @Test public void Any_Serialized() throws Exception { - Predicate where = cat.kittens.any().name.eq("Ruth123"); + Predicate where = cat.kittens.any().name.eq("Ruth234"); // serialize predicate ByteArrayOutputStream baos = new ByteArrayOutputStream(); @@ -83,13 +83,13 @@ public class SerializationBase { Predicate where2 = (Predicate) in.readObject(); in.close(); - assertEquals(1, query().from(cat).where(where).count()); - assertEquals(1, query().from(cat).where(where2).count()); + assertEquals(0, query().from(cat).where(where).count()); + assertEquals(0, query().from(cat).where(where2).count()); } @Test public void Any_Serialized2() throws Exception { - Predicate where = cat.kittens.any().name.eq("Ruth123"); + Predicate where = cat.kittens.any().name.eq("Ruth234"); File file = new File("target", "predicate.ser"); if (!file.exists()) { @@ -98,14 +98,14 @@ public class SerializationBase { ObjectOutputStream out = new ObjectOutputStream(fileOutputStream); out.writeObject(where); out.close(); - assertEquals(1, query().from(cat).where(where).count()); + assertEquals(0, query().from(cat).where(where).count()); } else { // deserialize predicate on second run FileInputStream fileInputStream = new FileInputStream(file); ObjectInputStream in = new ObjectInputStream(fileInputStream); Predicate where2 = (Predicate) in.readObject(); in.close(); - assertEquals(1, query().from(cat).where(where2).count()); + assertEquals(0, query().from(cat).where(where2).count()); } }