mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-30 21:08:30 +08:00
added tests
This commit is contained in:
parent
dec6565b47
commit
9536a0d4c5
@ -5,8 +5,6 @@
|
||||
*/
|
||||
package com.mysema.query.types.expr;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
@ -15,7 +13,6 @@ import com.mysema.query.types.CollectionExpression;
|
||||
import com.mysema.query.types.ConstantImpl;
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.Ops;
|
||||
import com.mysema.query.types.PathMetadata;
|
||||
|
||||
/**
|
||||
* CollectionExpressionBase is an abstract base class for CollectionExpression implementations
|
||||
@ -34,14 +31,9 @@ public abstract class CollectionExpressionBase<C extends Collection<E>, E> exten
|
||||
@Nullable
|
||||
private volatile NumberExpression<Integer> size;
|
||||
|
||||
@Nullable
|
||||
private transient volatile Constructor<?> constructor;
|
||||
|
||||
public CollectionExpressionBase(Class<? extends C> type) {
|
||||
super(type);
|
||||
}
|
||||
|
||||
public abstract SimpleExpression<E> any();
|
||||
|
||||
public final BooleanExpression contains(E child) {
|
||||
return contains(new ConstantImpl<E>(child));
|
||||
@ -71,22 +63,6 @@ public abstract class CollectionExpressionBase<C extends Collection<E>, E> exten
|
||||
return size;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <Q extends SimpleExpression<E>> Q newInstance(Class<Q> queryType, boolean typed, PathMetadata<?> pm) throws NoSuchMethodException,
|
||||
InstantiationException, IllegalAccessException,
|
||||
InvocationTargetException {
|
||||
if (constructor == null) {
|
||||
if (typed){
|
||||
constructor = queryType.getConstructor(Class.class, PathMetadata.class);
|
||||
}else{
|
||||
constructor = queryType.getConstructor(PathMetadata.class);
|
||||
}
|
||||
}
|
||||
if (typed){
|
||||
return (Q)constructor.newInstance(getElementType(), pm);
|
||||
}else{
|
||||
return (Q)constructor.newInstance(pm);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -17,7 +17,6 @@ import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.PathImpl;
|
||||
import com.mysema.query.types.PathMetadata;
|
||||
import com.mysema.query.types.Visitor;
|
||||
import com.mysema.query.types.expr.CollectionExpressionBase;
|
||||
import com.mysema.query.types.expr.SimpleExpression;
|
||||
|
||||
/**
|
||||
@ -27,7 +26,7 @@ import com.mysema.query.types.expr.SimpleExpression;
|
||||
*
|
||||
* @param <E> component type
|
||||
*/
|
||||
public class CollectionPath<E, Q extends SimpleExpression<E>> extends CollectionExpressionBase<Collection<E>,E> implements Path<Collection<E>>{
|
||||
public class CollectionPath<E, Q extends SimpleExpression<E>> extends CollectionPathBase<Collection<E>,E>{
|
||||
|
||||
private static final long serialVersionUID = -4982311799113762600L;
|
||||
|
||||
@ -57,7 +56,7 @@ public class CollectionPath<E, Q extends SimpleExpression<E>> extends Collection
|
||||
public Q any(){
|
||||
if (any == null){
|
||||
try {
|
||||
any = newInstance(queryType, Constants.isTyped(queryType), pathMixin.getMetadata());
|
||||
any = newInstance(queryType, pathMixin.getMetadata());
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new ExpressionException(e);
|
||||
} catch (InstantiationException e) {
|
||||
|
||||
@ -0,0 +1,53 @@
|
||||
package com.mysema.query.types.path;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.PathMetadata;
|
||||
import com.mysema.query.types.expr.CollectionExpressionBase;
|
||||
import com.mysema.query.types.expr.SimpleExpression;
|
||||
|
||||
/**
|
||||
* CollectionPath is a base class for collection typed paths
|
||||
*
|
||||
* @author tiwe
|
||||
*
|
||||
* @param <C>
|
||||
* @param <E>
|
||||
*/
|
||||
public abstract class CollectionPathBase<C extends Collection<E>, E> extends CollectionExpressionBase<C,E> implements Path<C>{
|
||||
|
||||
private static final long serialVersionUID = -9004995667633601298L;
|
||||
|
||||
public CollectionPathBase(Class<? extends C> type) {
|
||||
super(type);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private transient volatile Constructor<?> constructor;
|
||||
|
||||
public abstract SimpleExpression<E> any();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected <Q extends SimpleExpression<E>> Q newInstance(Class<Q> queryType, PathMetadata<?> pm) throws NoSuchMethodException,
|
||||
InstantiationException, IllegalAccessException,
|
||||
InvocationTargetException {
|
||||
if (constructor == null) {
|
||||
if (Constants.isTyped(queryType)){
|
||||
constructor = queryType.getConstructor(Class.class, PathMetadata.class);
|
||||
}else{
|
||||
constructor = queryType.getConstructor(PathMetadata.class);
|
||||
}
|
||||
}
|
||||
if (Constants.isTyped(queryType)){
|
||||
return (Q)constructor.newInstance(getElementType(), pm);
|
||||
}else{
|
||||
return (Q)constructor.newInstance(pm);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -21,7 +21,6 @@ import com.mysema.query.types.PathImpl;
|
||||
import com.mysema.query.types.PathMetadata;
|
||||
import com.mysema.query.types.PathMetadataFactory;
|
||||
import com.mysema.query.types.Visitor;
|
||||
import com.mysema.query.types.expr.CollectionExpressionBase;
|
||||
import com.mysema.query.types.expr.ListExpression;
|
||||
import com.mysema.query.types.expr.SimpleExpression;
|
||||
|
||||
@ -32,7 +31,7 @@ import com.mysema.query.types.expr.SimpleExpression;
|
||||
*
|
||||
* @param <E> component type
|
||||
*/
|
||||
public class ListPath<E, Q extends SimpleExpression<E>> extends CollectionExpressionBase<List<E>,E> implements ListExpression<E>, Path<List<E>>{
|
||||
public class ListPath<E, Q extends SimpleExpression<E>> extends CollectionPathBase<List<E>,E> implements ListExpression<E> {
|
||||
|
||||
private static final long serialVersionUID = 3302301599074388860L;
|
||||
|
||||
@ -64,7 +63,7 @@ public class ListPath<E, Q extends SimpleExpression<E>> extends CollectionExpres
|
||||
public Q any(){
|
||||
if (any == null){
|
||||
try {
|
||||
any = newInstance(pathMixin.getMetadata());
|
||||
any = newInstance(queryType, pathMixin.getMetadata());
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new ExpressionException(e);
|
||||
} catch (InstantiationException e) {
|
||||
@ -89,7 +88,7 @@ public class ListPath<E, Q extends SimpleExpression<E>> extends CollectionExpres
|
||||
private Q create(int index){
|
||||
try {
|
||||
PathMetadata<Integer> md = forListAccess(index);
|
||||
return newInstance(md);
|
||||
return newInstance(queryType, md);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new ExpressionException(e);
|
||||
} catch (InstantiationException e) {
|
||||
@ -110,7 +109,7 @@ public class ListPath<E, Q extends SimpleExpression<E>> extends CollectionExpres
|
||||
public Q get(Expression<Integer> index) {
|
||||
try {
|
||||
PathMetadata<Integer> md = forListAccess(index);
|
||||
return newInstance(md);
|
||||
return newInstance(queryType, md);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new ExpressionException(e);
|
||||
} catch (InstantiationException e) {
|
||||
@ -122,10 +121,6 @@ public class ListPath<E, Q extends SimpleExpression<E>> extends CollectionExpres
|
||||
}
|
||||
}
|
||||
|
||||
private Q newInstance(PathMetadata<?> md) throws NoSuchMethodException, InstantiationException,
|
||||
IllegalAccessException, InvocationTargetException {
|
||||
return newInstance(queryType, Constants.isTyped(queryType), md);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Q get(int index) {
|
||||
|
||||
@ -14,10 +14,9 @@ import javax.annotation.Nullable;
|
||||
import com.mysema.commons.lang.Assert;
|
||||
import com.mysema.query.types.ExpressionException;
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.PathMetadata;
|
||||
import com.mysema.query.types.PathImpl;
|
||||
import com.mysema.query.types.PathMetadata;
|
||||
import com.mysema.query.types.Visitor;
|
||||
import com.mysema.query.types.expr.CollectionExpressionBase;
|
||||
import com.mysema.query.types.expr.SimpleExpression;
|
||||
|
||||
/**
|
||||
@ -27,7 +26,7 @@ import com.mysema.query.types.expr.SimpleExpression;
|
||||
*
|
||||
* @param <E> component type
|
||||
*/
|
||||
public class SetPath<E, Q extends SimpleExpression<E>> extends CollectionExpressionBase<Set<E>,E> implements Path<Set<E>> {
|
||||
public class SetPath<E, Q extends SimpleExpression<E>> extends CollectionPathBase<Set<E>,E> {
|
||||
|
||||
private static final long serialVersionUID = 4145848445507037373L;
|
||||
|
||||
@ -57,7 +56,7 @@ public class SetPath<E, Q extends SimpleExpression<E>> extends CollectionExpress
|
||||
public Q any(){
|
||||
if (any == null){
|
||||
try {
|
||||
any = newInstance(queryType, Constants.isTyped(queryType), pathMixin.getMetadata());
|
||||
any = newInstance(queryType, pathMixin.getMetadata());
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new ExpressionException(e);
|
||||
} catch (InstantiationException e) {
|
||||
|
||||
@ -51,11 +51,6 @@ public final class ListSubQuery<A> extends CollectionExpressionBase<List<A>,A> i
|
||||
return v.visit(this, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SimpleExpression<A> any(){
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return subQueryMixin.equals(o);
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
package com.mysema.query.types;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConstantImplTest {
|
||||
|
||||
@Test
|
||||
public void Create(){
|
||||
assertNotNull(ConstantImpl.create(true));
|
||||
assertNotNull(ConstantImpl.create((byte)1));
|
||||
assertNotNull(ConstantImpl.create(ConstantImplTest.class));
|
||||
assertNotNull(ConstantImpl.create(1));
|
||||
assertNotNull(ConstantImpl.create(1l));
|
||||
assertNotNull(ConstantImpl.create((short)1));
|
||||
assertNotNull(ConstantImpl.create("x"));
|
||||
assertNotNull(ConstantImpl.create("x",true));
|
||||
}
|
||||
|
||||
}
|
||||
@ -150,7 +150,11 @@ public class PathTest {
|
||||
paths.add(new StringPath("p"));
|
||||
paths.add(new TimePath(Time.class,"p"));
|
||||
|
||||
// TODO : assertions
|
||||
for (Path<?> path : paths){
|
||||
assertNotNull(path.getMetadata());
|
||||
assertNotNull(path.getType());
|
||||
assertEquals(path, path.getRoot());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user