Add tests

This commit is contained in:
Timo Westkämper 2014-12-02 18:46:47 +02:00
parent 795178ab84
commit 403cbe4cec
2 changed files with 41 additions and 6 deletions

View File

@ -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> 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();
}
}

View File

@ -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());
}
}