mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
improved regex/like handling
simplified test runners
This commit is contained in:
parent
616415fa85
commit
8c717f869b
@ -7,6 +7,7 @@ package com.mysema.query.jpa;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.mysema.query.BooleanBuilder;
|
||||
import com.mysema.query.JoinExpression;
|
||||
import com.mysema.query.JoinFlag;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
@ -74,7 +75,11 @@ public class JPQLQueryMixin<T> extends QueryMixin<T> {
|
||||
}
|
||||
|
||||
private Predicate normalize(Predicate predicate) {
|
||||
return (Predicate) predicate.accept(JPQLCollectionAnyVisitor.DEFAULT, new CollectionAnyVisitor.Context());
|
||||
if (predicate instanceof BooleanBuilder && ((BooleanBuilder)predicate).getValue() == null){
|
||||
return predicate;
|
||||
}else{
|
||||
return (Predicate) predicate.accept(JPQLCollectionAnyVisitor.DEFAULT, new CollectionAnyVisitor.Context());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -23,7 +23,6 @@ import com.mysema.query.JoinType;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
import com.mysema.query.support.SerializerBase;
|
||||
import com.mysema.query.types.*;
|
||||
import com.mysema.query.types.expr.SimpleOperation;
|
||||
import com.mysema.util.MathUtils;
|
||||
|
||||
/**
|
||||
@ -104,30 +103,7 @@ public class JPQLSerializer extends SerializerBase<JPQLSerializer> {
|
||||
handle(je.getTarget());
|
||||
}
|
||||
|
||||
// TODO : generalize this!
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> Expression<?> regexToLike(Operation<T> operation) {
|
||||
List<Expression<?>> args = new ArrayList<Expression<?>>();
|
||||
for (Expression<?> arg : operation.getArgs()){
|
||||
if (!arg.getType().equals(String.class)){
|
||||
args.add(arg);
|
||||
}else if (arg instanceof Constant){
|
||||
args.add(regexToLike(arg.toString()));
|
||||
}else if (arg instanceof Operation){
|
||||
args.add(regexToLike((Operation)arg));
|
||||
}else{
|
||||
args.add(arg);
|
||||
}
|
||||
}
|
||||
return SimpleOperation.create(
|
||||
operation.getType(),
|
||||
operation.getOperator(),
|
||||
args.<Expression<?>>toArray(new Expression[args.size()]));
|
||||
}
|
||||
|
||||
private Expression<?> regexToLike(String str){
|
||||
return ConstantImpl.create(str.replace(".*", "%").replace(".", "_"));
|
||||
}
|
||||
|
||||
public void serialize(QueryMetadata metadata, boolean forCountRow, @Nullable String projection) {
|
||||
List<? extends Expression<?>> select = metadata.getProjection();
|
||||
@ -339,13 +315,8 @@ public class JPQLSerializer extends SerializerBase<JPQLSerializer> {
|
||||
append(")");
|
||||
|
||||
} else if (operator.equals(Ops.MATCHES)){
|
||||
List<Expression<?>> newArgs = new ArrayList<Expression<?>>(args);
|
||||
if (newArgs.get(1) instanceof Constant){
|
||||
newArgs.set(1, regexToLike(newArgs.get(1).toString()));
|
||||
}else if (newArgs.get(1) instanceof Operation){
|
||||
newArgs.set(1, regexToLike((Operation)newArgs.get(1)));
|
||||
}
|
||||
super.visitOperation(type, operator, newArgs);
|
||||
super.visitOperation(type, Ops.LIKE,
|
||||
Arrays.asList(args.get(0), ExpressionUtils.regexToLike((Expression<String>) args.get(1))));
|
||||
|
||||
}else if(NUMERIC.contains(operator)){
|
||||
super.visitOperation(type, operator, normalizeNumericArgs(args));
|
||||
|
||||
@ -6,15 +6,18 @@
|
||||
package com.mysema.testutil;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.cfg.AnnotationConfiguration;
|
||||
import org.junit.internal.runners.InitializationError;
|
||||
import org.junit.internal.runners.JUnit4ClassRunner;
|
||||
import org.junit.rules.MethodRule;
|
||||
import org.junit.runner.notification.RunNotifier;
|
||||
import org.junit.runners.BlockJUnit4ClassRunner;
|
||||
import org.junit.runners.model.FrameworkMethod;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
import com.mysema.query.jpa.domain.Domain;
|
||||
|
||||
@ -24,9 +27,7 @@ import com.mysema.query.jpa.domain.Domain;
|
||||
* @author tiwe
|
||||
* @version $Id$
|
||||
*/
|
||||
public class HibernateTestRunner extends JUnit4ClassRunner {
|
||||
|
||||
private Session session;
|
||||
public class HibernateTestRunner extends BlockJUnit4ClassRunner {
|
||||
|
||||
private SessionFactory sessionFactory;
|
||||
|
||||
@ -34,23 +35,33 @@ public class HibernateTestRunner extends JUnit4ClassRunner {
|
||||
super(klass);
|
||||
}
|
||||
|
||||
protected Object createTest() throws Exception {
|
||||
Object o = getTestClass().getConstructor().newInstance();
|
||||
o.getClass().getMethod("setSession", Session.class).invoke(o, session);
|
||||
return o;
|
||||
@Override
|
||||
protected List<MethodRule> rules(Object test) {
|
||||
List<MethodRule> rules = super.rules(test);
|
||||
rules.add(new MethodRule(){
|
||||
@Override
|
||||
public Statement apply(final Statement base, FrameworkMethod method, final Object target) {
|
||||
return new Statement(){
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
Session session = sessionFactory.openSession();
|
||||
target.getClass().getMethod("setSession", Session.class).invoke(target, session);
|
||||
session.beginTransaction();
|
||||
try {
|
||||
base.evaluate();
|
||||
} finally {
|
||||
session.getTransaction().rollback();
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
});
|
||||
return rules;
|
||||
}
|
||||
|
||||
protected void invokeTestMethod(Method method, RunNotifier notifier) {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
try {
|
||||
super.invokeTestMethod(method, notifier);
|
||||
} finally {
|
||||
session.getTransaction().rollback();
|
||||
}
|
||||
session.close();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run(final RunNotifier notifier) {
|
||||
try {
|
||||
AnnotationConfiguration cfg = new AnnotationConfiguration();
|
||||
|
||||
@ -5,47 +5,58 @@
|
||||
*/
|
||||
package com.mysema.testutil;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
|
||||
import org.junit.internal.runners.InitializationError;
|
||||
import org.junit.internal.runners.JUnit4ClassRunner;
|
||||
import org.junit.rules.MethodRule;
|
||||
import org.junit.runner.notification.RunNotifier;
|
||||
import org.junit.runners.BlockJUnit4ClassRunner;
|
||||
import org.junit.runners.model.FrameworkMethod;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class JPATestRunner extends JUnit4ClassRunner {
|
||||
|
||||
private EntityManager entityManager;
|
||||
public class JPATestRunner extends BlockJUnit4ClassRunner {
|
||||
|
||||
private EntityManagerFactory entityManagerFactory;
|
||||
|
||||
public JPATestRunner(Class<?> klass) throws InitializationError {
|
||||
public JPATestRunner(Class<?> klass) throws InitializationError{
|
||||
super(klass);
|
||||
}
|
||||
|
||||
protected Object createTest() throws Exception {
|
||||
Object o = getTestClass().getConstructor().newInstance();
|
||||
o.getClass().getMethod("setEntityManager", EntityManager.class).invoke(o, entityManager);
|
||||
return o;
|
||||
@Override
|
||||
protected List<MethodRule> rules(Object test) {
|
||||
List<MethodRule> rules = super.rules(test);
|
||||
rules.add(new MethodRule(){
|
||||
@Override
|
||||
public Statement apply(final Statement base, FrameworkMethod method, final Object target) {
|
||||
return new Statement(){
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
EntityManager entityManager = entityManagerFactory.createEntityManager();
|
||||
target.getClass().getMethod("setEntityManager", EntityManager.class).invoke(target, entityManager);
|
||||
entityManager.getTransaction().begin();
|
||||
try {
|
||||
base.evaluate();
|
||||
} finally {
|
||||
entityManager.getTransaction().rollback();
|
||||
entityManager.close();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
});
|
||||
return rules;
|
||||
}
|
||||
|
||||
protected void invokeTestMethod(Method method, RunNotifier notifier) {
|
||||
entityManager = entityManagerFactory.createEntityManager();
|
||||
entityManager.getTransaction().begin();
|
||||
try {
|
||||
super.invokeTestMethod(method, notifier);
|
||||
} finally {
|
||||
entityManager.getTransaction().rollback();
|
||||
}
|
||||
entityManager.close();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run(final RunNotifier notifier) {
|
||||
try {
|
||||
JPAConfig config = getTestClass().getJavaClass().getAnnotation(JPAConfig.class);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user