Further improvements

This commit is contained in:
Timo Westkämper 2014-12-01 22:37:52 +02:00
parent 79be07f5ad
commit 795178ab84
3 changed files with 70 additions and 62 deletions

View File

@ -13,10 +13,11 @@
*/
package com.mysema.query.types.expr;
import com.mysema.query.types.Expression;
import com.mysema.query.types.Ops;
import com.mysema.query.types.Path;
import com.mysema.query.types.PathImpl;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.reflect.Field;
import com.mysema.query.types.*;
/**
* DslExpression is the base class for DSL expressions, but {@link SimpleExpression} is the base class
@ -31,7 +32,7 @@ public abstract class DslExpression<T> implements Expression<T> {
protected final Expression<T> mixin;
protected final int hashCode;
protected transient final int hashCode;
public DslExpression(Expression<T> mixin) {
this.mixin = mixin;
@ -77,4 +78,16 @@ public abstract class DslExpression<T> implements Expression<T> {
return mixin.toString();
}
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
try {
ois.defaultReadObject();
Field field = DslExpression.class.getDeclaredField("hashCode");
field.setAccessible(true);
field.set(this, mixin.hashCode());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@ -283,49 +283,6 @@ public abstract class AbstractJPATest {
assertEquals(1, query().from(cat).where(cat.kittens.any().name.eq("Ruth123")).count());
}
@Test
public void Any_Serialized() throws Exception {
Predicate where = cat.kittens.any().name.eq("Ruth123");
// serialize predicate
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(where);
out.close();
// deserialize predicate
ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bain);
Predicate where2 = (Predicate) in.readObject();
in.close();
assertEquals(1, query().from(cat).where(where).count());
assertEquals(1, query().from(cat).where(where2).count());
}
@Test
public void Any_Serialized2() throws Exception {
Predicate where = cat.kittens.any().name.eq("Ruth123");
File file = new File("target", "predicate.ser");
if (!file.exists()) {
// serialize predicate on first run
FileOutputStream fileOutputStream = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fileOutputStream);
out.writeObject(where);
out.close();
assertEquals(1, 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());
}
}
@SuppressWarnings("unchecked")
@Test
public void ArrayProjection() {

View File

@ -13,25 +13,17 @@
*/
package com.mysema.query.jpa;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.persistence.EntityManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.*;
import com.mysema.query.QueryMetadata;
import com.mysema.query.jpa.domain.QCat;
import com.mysema.query.jpa.impl.JPAQuery;
import com.mysema.query.types.Predicate;
import com.mysema.testutil.JPATestRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
@RunWith(JPATestRunner.class)
public class SerializationBase {
@ -43,7 +35,7 @@ public class SerializationBase {
@Test
public void test() throws IOException, ClassNotFoundException{
// create query
JPAQuery query = new JPAQuery(entityManager);
JPAQuery query = query();
query.from(cat).where(cat.name.eq("Kate")).list(cat);
// get metadata
@ -75,6 +67,52 @@ public class SerializationBase {
query2.list(cat);
}
@Test
public void Any_Serialized() throws Exception {
Predicate where = cat.kittens.any().name.eq("Ruth123");
// serialize predicate
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(where);
out.close();
// deserialize predicate
ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bain);
Predicate where2 = (Predicate) in.readObject();
in.close();
assertEquals(1, query().from(cat).where(where).count());
assertEquals(1, query().from(cat).where(where2).count());
}
@Test
public void Any_Serialized2() throws Exception {
Predicate where = cat.kittens.any().name.eq("Ruth123");
File file = new File("target", "predicate.ser");
if (!file.exists()) {
// serialize predicate on first run
FileOutputStream fileOutputStream = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fileOutputStream);
out.writeObject(where);
out.close();
assertEquals(1, 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());
}
}
private JPAQuery query() {
return new JPAQuery(entityManager);
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}