added tests

This commit is contained in:
Timo Westkämper 2010-11-01 16:01:08 +00:00
parent 3711857300
commit 8ed481dcbd
17 changed files with 269 additions and 114 deletions

View File

@ -45,6 +45,10 @@ public class ArrayPath<E> extends SimpleExpression<E[]> implements Path<E[]>, Ar
this(type, PathMetadataFactory.forVariable(variable));
}
public ArrayPath(Class<? super E[]> type, Path<?> parent, String property) {
this(type, PathMetadataFactory.forProperty(parent, property));
}
@SuppressWarnings("unchecked")
public ArrayPath(Class<? super E[]> type, PathMetadata<?> metadata) {
super((Class)type);

View File

@ -46,6 +46,10 @@ public class BeanPath<D> extends SimpleExpression<D> implements Path<D> {
public BeanPath(Class<? extends D> type, String variable) {
this(type, PathMetadataFactory.forVariable(variable), null);
}
public BeanPath(Class<? extends D> type, Path<?> parent, String property) {
this(type, PathMetadataFactory.forProperty(parent, property), null);
}
public BeanPath(Class<? extends D> type, PathMetadata<?> metadata) {
this(type, metadata, null);

View File

@ -6,13 +6,11 @@
package com.mysema.query.types.path;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
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.PathImpl;
import com.mysema.query.types.PathMetadata;
@ -44,6 +42,10 @@ public class CollectionPath<E, Q extends SimpleExpression<E>> extends Collection
this(type, queryType, PathMetadataFactory.forVariable(variable));
}
public CollectionPath(Class<? super E> type, Class<Q> queryType, Path<?> parent, String property) {
this(type, queryType, PathMetadataFactory.forProperty(parent, property));
}
@SuppressWarnings("unchecked")
public CollectionPath(Class<? super E> type, Class<Q> queryType, PathMetadata<?> metadata) {
super((Class)Collection.class);
@ -60,17 +62,7 @@ public class CollectionPath<E, Q extends SimpleExpression<E>> extends Collection
@Override
public Q any(){
if (any == null){
try {
any = newInstance(queryType, PathMetadataFactory.forCollectionAny(this));
} catch (NoSuchMethodException e) {
throw new ExpressionException(e);
} catch (InstantiationException e) {
throw new ExpressionException(e);
} catch (IllegalAccessException e) {
throw new ExpressionException(e);
} catch (InvocationTargetException e) {
throw new ExpressionException(e);
}
any = newInstance(queryType, PathMetadataFactory.forCollectionAny(this));
}
return any;
}

View File

@ -6,6 +6,7 @@ import java.util.Collection;
import javax.annotation.Nullable;
import com.mysema.query.types.ExpressionException;
import com.mysema.query.types.Path;
import com.mysema.query.types.PathMetadata;
import com.mysema.query.types.expr.CollectionExpressionBase;
@ -33,21 +34,30 @@ public abstract class CollectionPathBase<C extends Collection<E>, E> extends Col
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);
protected <Q extends SimpleExpression<E>> Q newInstance(Class<Q> queryType, PathMetadata<?> pm){
try{
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);
}
} catch (NoSuchMethodException e) {
throw new ExpressionException(e);
} catch (InstantiationException e) {
throw new ExpressionException(e);
} catch (IllegalAccessException e) {
throw new ExpressionException(e);
} catch (InvocationTargetException e) {
throw new ExpressionException(e);
}
if (Constants.isTyped(queryType)){
return (Q)constructor.newInstance(getElementType(), pm);
}else{
return (Q)constructor.newInstance(pm);
}
}
}

View File

@ -42,10 +42,6 @@ public class ComparablePath<D extends Comparable> extends ComparableExpression<D
this(type, PathMetadataFactory.forVariable(var));
}
public ComparablePath(Path<?> parent, String property) {
this((Class<? extends D>) Comparable.class, PathMetadataFactory.forProperty(parent, property));
}
@Override
public <R,C> R accept(Visitor<R,C> v, C context) {
return v.visit(this, context);

View File

@ -6,7 +6,6 @@
package com.mysema.query.types.path;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -15,7 +14,6 @@ import javax.annotation.Nullable;
import com.mysema.commons.lang.Assert;
import com.mysema.query.types.Expression;
import com.mysema.query.types.ExpressionException;
import com.mysema.query.types.Path;
import com.mysema.query.types.PathImpl;
import com.mysema.query.types.PathMetadata;
@ -50,6 +48,10 @@ public class ListPath<E, Q extends SimpleExpression<E>> extends CollectionPathBa
this(elementType, queryType, PathMetadataFactory.forVariable(variable));
}
public ListPath(Class<? super E> elementType, Class<Q> queryType, Path<?> parent, String property) {
this(elementType, queryType, PathMetadataFactory.forProperty(parent, property));
}
@SuppressWarnings("unchecked")
public ListPath(Class<? super E> elementType, Class<Q> queryType, PathMetadata<?> metadata) {
super((Class)List.class);
@ -66,17 +68,7 @@ public class ListPath<E, Q extends SimpleExpression<E>> extends CollectionPathBa
@Override
public Q any(){
if (any == null){
try {
any = newInstance(queryType, PathMetadataFactory.forCollectionAny(this));
} catch (NoSuchMethodException e) {
throw new ExpressionException(e);
} catch (InstantiationException e) {
throw new ExpressionException(e);
} catch (IllegalAccessException e) {
throw new ExpressionException(e);
} catch (InvocationTargetException e) {
throw new ExpressionException(e);
}
any = newInstance(queryType, PathMetadataFactory.forCollectionAny(this));
}
return any;
}
@ -90,18 +82,8 @@ public class ListPath<E, Q extends SimpleExpression<E>> extends CollectionPathBa
}
private Q create(int index){
try {
PathMetadata<Integer> md = forListAccess(index);
return newInstance(queryType, md);
} catch (NoSuchMethodException e) {
throw new ExpressionException(e);
} catch (InstantiationException e) {
throw new ExpressionException(e);
} catch (IllegalAccessException e) {
throw new ExpressionException(e);
} catch (InvocationTargetException e) {
throw new ExpressionException(e);
}
PathMetadata<Integer> md = forListAccess(index);
return newInstance(queryType, md);
}
@Override
@ -111,18 +93,8 @@ public class ListPath<E, Q extends SimpleExpression<E>> extends CollectionPathBa
@Override
public Q get(Expression<Integer> index) {
try {
PathMetadata<Integer> md = forListAccess(index);
return newInstance(queryType, md);
} catch (NoSuchMethodException e) {
throw new ExpressionException(e);
} catch (InstantiationException e) {
throw new ExpressionException(e);
} catch (IllegalAccessException e) {
throw new ExpressionException(e);
} catch (InvocationTargetException e) {
throw new ExpressionException(e);
}
PathMetadata<Integer> md = forListAccess(index);
return newInstance(queryType, md);
}

View File

@ -15,9 +15,9 @@ import javax.annotation.Nullable;
import com.mysema.query.types.Expression;
import com.mysema.query.types.ExpressionException;
import com.mysema.query.types.Path;
import com.mysema.query.types.PathImpl;
import com.mysema.query.types.PathMetadata;
import com.mysema.query.types.PathMetadataFactory;
import com.mysema.query.types.PathImpl;
import com.mysema.query.types.Visitor;
import com.mysema.query.types.expr.MapExpressionBase;
import com.mysema.query.types.expr.SimpleExpression;
@ -49,6 +49,10 @@ public class MapPath<K, V, E extends SimpleExpression<V>> extends MapExpressionB
this(keyType, valueType, queryType, PathMetadataFactory.forVariable(variable));
}
public MapPath(Class<? super K> keyType, Class<? super V> valueType, Class<E> queryType, Path<?> parent, String property) {
this(keyType, valueType, queryType, PathMetadataFactory.forProperty(parent, property));
}
@SuppressWarnings("unchecked")
public MapPath(Class<? super K> keyType, Class<? super V> valueType, Class<E> queryType, PathMetadata<?> metadata) {
super((Class)Map.class);

View File

@ -6,13 +6,11 @@
package com.mysema.query.types.path;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.InvocationTargetException;
import java.util.Set;
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.PathImpl;
import com.mysema.query.types.PathMetadata;
@ -44,6 +42,10 @@ public class SetPath<E, Q extends SimpleExpression<E>> extends CollectionPathBas
this(type, queryType, PathMetadataFactory.forVariable(variable));
}
public SetPath(Class<? super E> type, Class<Q> queryType, Path<?> parent, String property) {
this(type, queryType, PathMetadataFactory.forProperty(parent, property));
}
@SuppressWarnings("unchecked")
public SetPath(Class<? super E> type, Class<Q> queryType, PathMetadata<?> metadata) {
super((Class)Set.class);
@ -60,17 +62,7 @@ public class SetPath<E, Q extends SimpleExpression<E>> extends CollectionPathBas
@Override
public Q any(){
if (any == null){
try {
any = newInstance(queryType, PathMetadataFactory.forCollectionAny(this));
} catch (NoSuchMethodException e) {
throw new ExpressionException(e);
} catch (InstantiationException e) {
throw new ExpressionException(e);
} catch (IllegalAccessException e) {
throw new ExpressionException(e);
} catch (InvocationTargetException e) {
throw new ExpressionException(e);
}
any = newInstance(queryType, PathMetadataFactory.forCollectionAny(this));
}
return any;
}

View File

@ -0,0 +1,29 @@
package com.mysema.query.types.expr;
import static org.junit.Assert.*;
import org.junit.Test;
import com.mysema.query.types.path.BooleanPath;
public class BooleanExpressionTest {
private BooleanExpression a = new BooleanPath("a");
private BooleanExpression b = new BooleanPath("b");
private BooleanExpression c = new BooleanPath("c");
@Test
public void AnyOf(){
assertEquals(a.or(b).or(c), BooleanExpression.anyOf(a, b, c));
}
@Test
public void AllOf(){
assertEquals(a.and(b).and(c), BooleanExpression.allOf(a, b, c));
}
@Test
public void Not(){
assertEquals(a, a.not().not());
}
}

View File

@ -8,16 +8,24 @@ package com.mysema.query.types.expr;
import static org.junit.Assert.assertEquals;
import java.lang.reflect.Method;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import com.mysema.query.types.OperationImpl;
import com.mysema.query.types.Ops;
import com.mysema.query.types.Path;
import com.mysema.query.types.path.StringPath;
import com.mysema.query.types.PathImpl;
import com.mysema.query.types.path.*;
public class SimpleExpressionTest {
enum ExampleEnum {A,B}
@Test
public void As_usage(){
SimpleExpression<String> str = new StringPath("str");
@ -46,5 +54,32 @@ public class SimpleExpressionTest {
assertEquals(cl, asString.getReturnType());
}
}
@SuppressWarnings("unchecked")
@Test
public void Various(){
List<SimpleExpression<?>> paths = new ArrayList<SimpleExpression<?>>();
paths.add(new ArrayPath(String[].class, "p"));
paths.add(new BeanPath(Object.class, "p"));
paths.add(new BooleanPath("p"));
paths.add(new CollectionPath(String.class, StringPath.class, "p"));
paths.add(new ComparablePath(String.class,"p"));
paths.add(new DatePath(Date.class,"p"));
paths.add(new DateTimePath(Date.class,"p"));
paths.add(new EnumPath(ExampleEnum.class,"p"));
paths.add(new ListPath(String.class, StringPath.class, "p"));
paths.add(new MapPath(String.class, String.class, StringPath.class, "p"));
paths.add(new NumberPath(Integer.class,"p"));
paths.add(new SetPath(String.class, StringPath.class, "p"));
paths.add(new SimplePath(String.class,"p"));
paths.add(new StringPath("p"));
paths.add(new TimePath(Time.class,"p"));
for (SimpleExpression<?> expr : paths){
Path o = new PathImpl(expr.getType(), "o");
assertEquals(OperationImpl.create(expr.getType(), Ops.ALIAS, expr, o), expr.as("o"));
Path p = new PathImpl(expr.getType(), "p");
assertEquals(OperationImpl.create(expr.getType(), Ops.ALIAS, expr, p), expr.as(p));
}
}
}

View File

@ -1,18 +1,29 @@
package com.mysema.query.types.path;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import javax.annotation.Nullable;
import org.junit.Test;
import com.mysema.query.annotations.PropertyType;
import com.mysema.query.types.PathMetadata;
import com.mysema.query.types.PathMetadataFactory;
public class BeanPathTest {
public static class MyBeanPath extends BeanPath<Object>{
public static class MyBeanPath extends BeanPath<BeanPathTest>{
private static final long serialVersionUID = 6225684967115368814L;
public MyBeanPath(PathMetadata<?> metadata) {
super(Object.class, metadata);
super(BeanPathTest.class, metadata);
}
public MyBeanPath(PathMetadata<?> metadata, @Nullable PathInits inits) {
super(BeanPathTest.class, metadata);
}
}
@ -25,11 +36,33 @@ public class BeanPathTest {
assertNotNull(beanPath.as(simplePath));
}
// @Test
// public void As_Class(){
// MyBeanPath otherPath = beanPath.as(MyBeanPath.class);
// assertEquals(beanPath, otherPath);
// }
@Test
public void As_Class(){
MyBeanPath otherPath = beanPath.as(MyBeanPath.class);
assertEquals(beanPath, otherPath);
}
@Test
public void As_Class_Cached(){
MyBeanPath otherPath = beanPath.as(MyBeanPath.class);
assertEquals(beanPath, otherPath);
assertTrue(otherPath == beanPath.as(MyBeanPath.class));
}
@Test
public void As_Class_with_Inits(){
beanPath = new BeanPath<BeanPathTest>(BeanPathTest.class, PathMetadataFactory.forVariable("p"), PathInits.DEFAULT);
MyBeanPath otherPath = beanPath.as(MyBeanPath.class);
assertEquals(beanPath, otherPath);
}
@Test
public void As_Class_with_Inits_Cached(){
beanPath = new BeanPath<BeanPathTest>(BeanPathTest.class, PathMetadataFactory.forVariable("p"), PathInits.DEFAULT);
MyBeanPath otherPath = beanPath.as(MyBeanPath.class);
assertEquals(beanPath, otherPath);
assertTrue(otherPath == beanPath.as(MyBeanPath.class));
}
@Test
public void CreateEnum(){

View File

@ -8,14 +8,25 @@ import com.mysema.query.types.PathMetadataFactory;
public class CollectionPathTest {
private CollectionPath<String,StringPath> stringPath = new CollectionPath<String,StringPath>(
String.class, StringPath.class,
PathMetadataFactory.forVariable("stringPath"));
@Test
public void test(){
CollectionPath<String,StringPath> stringPath = new CollectionPath<String,StringPath>(
String.class, StringPath.class,
PathMetadataFactory.forVariable("stringPath"));
public void ToString(){
assertEquals("stringPath", stringPath.toString());
assertEquals("any(stringPath)", stringPath.any().toString());
assertEquals("eqIc(any(stringPath),X)", stringPath.any().equalsIgnoreCase("X").toString());
}
@Test
public void GetElementType(){
assertEquals(String.class, stringPath.getElementType());
}
@Test
public void GetParameter(){
assertEquals(String.class, stringPath.getParameter(0));
}
}

View File

@ -9,17 +9,27 @@ import com.mysema.query.types.PathMetadataFactory;
public class ListPathTest {
private ListPath<String,StringPath> stringPath = new ListPath<String,StringPath>(
String.class, StringPath.class,
PathMetadataFactory.forVariable("stringPath"));
@Test
public void test(){
ListPath<String,StringPath> stringPath = new ListPath<String,StringPath>(
String.class, StringPath.class,
PathMetadataFactory.forVariable("stringPath"));
public void ToString(){
assertEquals("stringPath", stringPath.toString());
assertEquals("any(stringPath)", stringPath.any().toString());
assertEquals("eqIc(stringPath.get(0),X)", stringPath.get(0).equalsIgnoreCase("X").toString());
assertEquals("eqIc(any(stringPath),X)", stringPath.any().equalsIgnoreCase("X").toString());
assertEquals("stringPath.get(0)", stringPath.get(ConstantImpl.create(0)).toString());
assertEquals("stringPath.get(0)", stringPath.get(ConstantImpl.create(0)).toString());
}
@Test
public void GetElementType(){
assertEquals(String.class, stringPath.getElementType());
}
@Test
public void GetParameter(){
assertEquals(String.class, stringPath.getParameter(0));
}
}

View File

@ -8,11 +8,28 @@ import com.mysema.query.types.ConstantImpl;
public class MapPathTest {
private MapPath<String,String,StringPath> mapPath = new MapPath<String,String,StringPath>(String.class, String.class, StringPath.class, "p");
@Test
public void Get() {
MapPath<String,String,StringPath> mapPath = new MapPath<String,String,StringPath>(String.class, String.class, StringPath.class, "p");
assertNotNull(mapPath.get("X"));
assertNotNull(mapPath.get(ConstantImpl.create("X")));
}
@Test
public void GetKeytType(){
assertEquals(String.class, mapPath.getKeyType());
}
@Test
public void GetValueType(){
assertEquals(String.class, mapPath.getValueType());
}
@Test
public void GetParameter(){
assertEquals(String.class, mapPath.getParameter(0));
assertEquals(String.class, mapPath.getParameter(1));
}
}

View File

@ -16,14 +16,14 @@ public class PathInitsTest {
public void Default(){
assertFalse(PathInits.DEFAULT.isInitialized(""));
}
@Test
public void test(){
PathInits Inits = new PathInits(".2").get("");
assertFalse(Inits.isInitialized("1"));
assertTrue(Inits.isInitialized("2"));
public void IsInitialized(){
PathInits inits = new PathInits(".2").get("");
assertFalse(inits.isInitialized("1"));
assertTrue(inits.isInitialized("2"));
}
@Test
public void Wildcard(){
assertTrue(new PathInits("*").isInitialized(""));
@ -31,8 +31,8 @@ public class PathInitsTest {
@Test
public void Wildcard2(){
PathInits Inits = new PathInits(".*").get("");
assertTrue(Inits.isInitialized("1"));
assertTrue(Inits.isInitialized("2"));
PathInits inits = new PathInits(".*").get("");
assertTrue(inits.isInitialized("1"));
assertTrue(inits.isInitialized("2"));
}
}

View File

@ -30,6 +30,8 @@ import com.mysema.query.annotations.QueryEntity;
import com.mysema.query.annotations.QueryTransient;
import com.mysema.query.types.Path;
import com.mysema.query.types.PathImpl;
import com.mysema.query.types.PathMetadataFactory;
import com.mysema.query.types.Templates;
import com.mysema.query.types.ToStringVisitor;
import com.mysema.util.AnnotatedElementAdapter;
@ -137,6 +139,38 @@ public class PathTest {
assertEquals(new TimePath(Time.class,"p"), new PathImpl(Time.class, "p"));
}
@SuppressWarnings("unchecked")
@Test
public void Various_Properties(){
Path<?> parent = new PathImpl(Object.class, "parent");
List<Path<?>> paths = new ArrayList<Path<?>>();
paths.add(new ArrayPath(String[].class, parent, "p"));
paths.add(new BeanPath(Object.class, parent, "p"));
paths.add(new BooleanPath(parent, "p"));
paths.add(new CollectionPath(String.class, StringPath.class, parent, "p"));
paths.add(new ComparablePath(String.class, parent, "p"));
paths.add(new DatePath(Date.class, parent, "p"));
paths.add(new DateTimePath(Date.class, parent, "p"));
paths.add(new EnumPath(ExampleEnum.class, parent, "p"));
paths.add(new ListPath(String.class, StringPath.class, parent, "p"));
paths.add(new MapPath(String.class, String.class, StringPath.class, parent, "p"));
paths.add(new NumberPath(Integer.class, parent, "p"));
paths.add(new SetPath(String.class, StringPath.class, parent, "p"));
paths.add(new SimplePath(String.class, parent, "p"));
paths.add(new StringPath(parent, "p"));
paths.add(new TimePath(Time.class, parent, "p"));
for (Path<?> path : paths){
Path other = new PathImpl(path.getType(), PathMetadataFactory.forProperty(parent, "p"));
assertEquals(path.toString(), path.accept(ToStringVisitor.DEFAULT, Templates.DEFAULT));
assertEquals(path.hashCode(), other.hashCode());
assertEquals(path, other);
assertNotNull(path.getMetadata());
assertNotNull(path.getType());
assertEquals(parent, path.getRoot());
}
}
@SuppressWarnings("unchecked")
@Test
public void Various(){
@ -152,6 +186,7 @@ public class PathTest {
paths.add(new ListPath(String.class, StringPath.class, "p"));
paths.add(new MapPath(String.class, String.class, StringPath.class, "p"));
paths.add(new NumberPath(Integer.class,"p"));
paths.add(new SetPath(String.class, StringPath.class, "p"));
paths.add(new SimplePath(String.class,"p"));
paths.add(new StringPath("p"));
paths.add(new TimePath(Time.class,"p"));

View File

@ -8,14 +8,25 @@ import com.mysema.query.types.PathMetadataFactory;
public class SetPathTest {
private SetPath<String,StringPath> stringPath = new SetPath<String,StringPath>(
String.class, StringPath.class,
PathMetadataFactory.forVariable("stringPath"));
@Test
public void test(){
SetPath<String,StringPath> stringPath = new SetPath<String,StringPath>(
String.class, StringPath.class,
PathMetadataFactory.forVariable("stringPath"));
public void ToString(){
assertEquals("stringPath", stringPath.toString());
assertEquals("any(stringPath)", stringPath.any().toString());
assertEquals("eqIc(any(stringPath),X)", stringPath.any().equalsIgnoreCase("X").toString());
}
@Test
public void GetElementType(){
assertEquals(String.class, stringPath.getElementType());
}
@Test
public void GetParameter(){
assertEquals(String.class, stringPath.getParameter(0));
}
}