added tests

This commit is contained in:
Timo Westkämper 2010-05-11 20:33:39 +00:00
parent aa59950c18
commit b595fc191a
26 changed files with 219 additions and 59 deletions

View File

@ -15,6 +15,7 @@ import org.apache.commons.collections15.IteratorUtils;
import com.mysema.commons.lang.CloseableIterator;
import com.mysema.commons.lang.IteratorAdapter;
import com.mysema.query.JoinType;
import com.mysema.query.QueryException;
import com.mysema.query.QueryMetadata;
import com.mysema.query.QueryModifiers;
@ -94,7 +95,7 @@ public abstract class AbstractColQuery<Q extends AbstractColQuery<Q>> extends P
@SuppressWarnings("unchecked")
public <A> Q from(Path<A> entity, Iterable<? extends A> col) {
iterables.put(entity.asExpr(), col);
queryMixin.getMetadata().addFrom(entity.asExpr());
queryMixin.getMetadata().addJoin(JoinType.DEFAULT, entity.asExpr());
return (Q)this;
}

View File

@ -50,22 +50,7 @@ public class DefaultQueryMetadata implements QueryMetadata, Cloneable {
private boolean unique;
private BooleanBuilder where = new BooleanBuilder();
@Override
@SuppressWarnings("GC_UNCHECKED_TYPE_IN_GENERIC_CALL")
public void addFrom(Expr<?>... args) {
for (Expr<?> arg : args) {
if (arg instanceof Path<?>){
ensureRoot((Path<?>) arg);
}
// NOTE : contains takes Object argument
if (!exprInJoins.contains(arg)) {
joins.add(new JoinExpression(JoinType.DEFAULT, arg));
exprInJoins.add(arg);
}
}
}
@Override
public void addGroupBy(Expr<?>... o) {
groupBy.addAll(Arrays.<Expr<?>> asList(o));

View File

@ -21,13 +21,6 @@ import com.mysema.query.types.expr.EBoolean;
* @author tiwe
*/
public interface QueryMetadata extends Serializable {
/**
* Add the given query sources
*
* @param o
*/
void addFrom(Expr<?>... o);
/**
* Add the given group by expressions

View File

@ -139,7 +139,7 @@ class PropertyAccessInvocationHandler implements MethodInterceptor {
return rv;
}
@SuppressWarnings({ "unchecked", "serial" })
@SuppressWarnings({ "unchecked"})
@Nullable
private <T> T newInstance(Class<T> type, Type genericType, Object parent, Object propKey, PathMetadata<?> pm) {
Expr<?> path;

View File

@ -46,7 +46,7 @@ public class DetachableMixin implements Detachable{
@Override
public ObjectSubQuery<Long> count() {
queryMixin.addToProjection(COUNT_ALL_AGG_EXPR);
return new ObjectSubQuery<Long>(queryMixin.getMetadata(), Long.class);
return new ObjectSubQuery<Long>(Long.class, queryMixin.getMetadata());
}
@Override
@ -61,13 +61,13 @@ public class DetachableMixin implements Detachable{
public ListSubQuery<Object[]> list(Expr<?> first, Expr<?> second, Expr<?>... rest) {
queryMixin.addToProjection(first, second);
queryMixin.addToProjection(rest);
return new ListSubQuery<Object[]>(queryMixin.getMetadata(), Object[].class);
return new ListSubQuery<Object[]>(Object[].class, queryMixin.getMetadata());
}
@Override
public ListSubQuery<Object[]> list(Expr<?>[] args) {
queryMixin.addToProjection(args);
return new ListSubQuery<Object[]>(queryMixin.getMetadata(), Object[].class);
return new ListSubQuery<Object[]>(Object[].class, queryMixin.getMetadata());
}
@ -75,7 +75,7 @@ public class DetachableMixin implements Detachable{
@Override
public <RT> ListSubQuery<RT> list(Expr<RT> projection) {
queryMixin.addToProjection(projection);
return new ListSubQuery<RT>(queryMixin.getMetadata(), (Class)projection.getType());
return new ListSubQuery<RT>((Class)projection.getType(), queryMixin.getMetadata());
}
@Override
@ -98,28 +98,28 @@ public class DetachableMixin implements Detachable{
@Override
public <RT extends Comparable<?>> ComparableSubQuery<RT> unique(EComparable<RT> projection) {
setUniqueProjection(projection);
return new ComparableSubQuery<RT>(queryMixin.getMetadata(), (Class)projection.getType());
return new ComparableSubQuery<RT>((Class)projection.getType(), queryMixin.getMetadata());
}
@SuppressWarnings("unchecked")
@Override
public <RT extends Comparable<?>> DateSubQuery<RT> unique(EDate<RT> projection) {
setUniqueProjection(projection);
return new DateSubQuery<RT>(queryMixin.getMetadata(), (Class)projection.getType());
return new DateSubQuery<RT>((Class)projection.getType(), queryMixin.getMetadata());
}
@SuppressWarnings("unchecked")
@Override
public <RT extends Comparable<?>> DateTimeSubQuery<RT> unique(EDateTime<RT> projection) {
setUniqueProjection(projection);
return new DateTimeSubQuery<RT>(queryMixin.getMetadata(), (Class)projection.getType());
return new DateTimeSubQuery<RT>((Class)projection.getType(), queryMixin.getMetadata());
}
@SuppressWarnings("unchecked")
@Override
public <RT extends Number & Comparable<?>> NumberSubQuery<RT> unique(ENumber<RT> projection) {
setUniqueProjection(projection);
return new NumberSubQuery<RT>(queryMixin.getMetadata(), (Class)projection.getType());
return new NumberSubQuery<RT>((Class)projection.getType(), queryMixin.getMetadata());
}
@Override
@ -132,7 +132,7 @@ public class DetachableMixin implements Detachable{
@Override
public <RT extends Comparable<?>> TimeSubQuery<RT> unique(ETime<RT> projection) {
setUniqueProjection(projection);
return new TimeSubQuery<RT>(queryMixin.getMetadata(), (Class)projection.getType());
return new TimeSubQuery<RT>((Class)projection.getType(), queryMixin.getMetadata());
}
@Override
@ -140,20 +140,20 @@ public class DetachableMixin implements Detachable{
queryMixin.addToProjection(first, second);
queryMixin.addToProjection(rest);
queryMixin.setUnique(true);
return new ObjectSubQuery<Object[]>(queryMixin.getMetadata(), Object[].class);
return new ObjectSubQuery<Object[]>(Object[].class, queryMixin.getMetadata());
}
@Override
public ObjectSubQuery<Object[]> unique(Expr<?>[] args) {
queryMixin.addToProjection(args);
queryMixin.setUnique(true);
return new ObjectSubQuery<Object[]>(queryMixin.getMetadata(), Object[].class);
return new ObjectSubQuery<Object[]>(Object[].class, queryMixin.getMetadata());
}
@SuppressWarnings("unchecked")
@Override
public <RT> ObjectSubQuery<RT> unique(Expr<RT> projection) {
setUniqueProjection(projection);
return new ObjectSubQuery<RT>(queryMixin.getMetadata(), (Class)projection.getType());
return new ObjectSubQuery<RT>((Class)projection.getType(), queryMixin.getMetadata());
}
}

View File

@ -54,8 +54,10 @@ public class QueryMixin<T>{
return metadata;
}
public T from(Expr<?>... args) {
metadata.addFrom(args);
public T from(Expr<?>... args) {
for (Expr<?> arg : args){
metadata.addJoin(JoinType.DEFAULT, arg);
}
return self;
}

View File

@ -26,7 +26,6 @@ public abstract class EComparable<D extends Comparable> extends EComparableBase<
super(type);
}
@SuppressWarnings("unchecked")
@Override
public EComparable<D> as(Path<D> alias) {
return OComparable.create(getType(),(Operator)Ops.ALIAS, this, alias.asExpr());

View File

@ -24,7 +24,7 @@ public final class ComparableSubQuery<A extends Comparable<?>> extends EComparab
private final SubQueryMixin<A> subQueryMixin;
public ComparableSubQuery(QueryMetadata md, Class<A> type) {
public ComparableSubQuery(Class<A> type, QueryMetadata md) {
super(type);
subQueryMixin = new SubQueryMixin<A>(this,md);
}

View File

@ -24,7 +24,7 @@ public final class DateSubQuery<A extends Comparable<?>> extends EDate<A> implem
private final SubQueryMixin<A> subQueryMixin;
public DateSubQuery(QueryMetadata md, Class<A> type) {
public DateSubQuery(Class<A> type, QueryMetadata md) {
super(type);
subQueryMixin = new SubQueryMixin<A>(this,md);
}

View File

@ -24,7 +24,7 @@ public final class DateTimeSubQuery<A extends Comparable<?>> extends EDateTime<A
private final SubQueryMixin<A> subQueryMixin;
public DateTimeSubQuery(QueryMetadata md, Class<A> type) {
public DateTimeSubQuery(Class<A> type, QueryMetadata md) {
super(type);
subQueryMixin = new SubQueryMixin<A>(this,md);
}

View File

@ -34,7 +34,7 @@ public final class ListSubQuery<A> extends ECollectionBase<List<A>,A> implements
private final SubQueryMixin<List<A>> subQueryMixin;
@SuppressWarnings("unchecked")
public ListSubQuery(QueryMetadata md, Class<A> elementType) {
public ListSubQuery(Class<A> elementType, QueryMetadata md) {
super((Class)List.class);
this.elementType = elementType;
this.subQueryMixin = new SubQueryMixin<List<A>>(this,md);

View File

@ -25,7 +25,7 @@ public final class NumberSubQuery<A extends Number & Comparable<?>> extends ENum
private final SubQueryMixin<A> subQueryMixin;
public NumberSubQuery(QueryMetadata md, Class<A> type) {
public NumberSubQuery(Class<A> type, QueryMetadata md) {
super(type);
subQueryMixin = new SubQueryMixin<A>(this,md);
}

View File

@ -24,7 +24,7 @@ public final class ObjectSubQuery<A> extends ESimple<A> implements SubQuery<A>{
private final SubQueryMixin<A> subQueryMixin;
public ObjectSubQuery(QueryMetadata md, Class<A> type) {
public ObjectSubQuery(Class<A> type, QueryMetadata md) {
super(type);
subQueryMixin = new SubQueryMixin<A>(this,md);
}

View File

@ -28,7 +28,7 @@ public final class TimeSubQuery<A extends Comparable<?>> extends ETime<A> implem
private final SubQueryMixin<A> subQueryMixin;
public TimeSubQuery(QueryMetadata md, Class<A> type) {
public TimeSubQuery(Class<A> type, QueryMetadata md) {
super(type);
subQueryMixin = new SubQueryMixin<A>(this,md);
}

View File

@ -54,4 +54,25 @@ public class BooleanBuilderTest {
assertEquals(first, first.and(null));
assertEquals(first, first.or(null));
}
@Test
public void andNot(){
BooleanBuilder builder = new BooleanBuilder();
builder.and(first).andNot(second);
assertEquals(first.and(second.not()), builder.getValue());
}
@Test
public void orNot(){
BooleanBuilder builder = new BooleanBuilder();
builder.and(first).orNot(second);
assertEquals(first.or(second.not()), builder.getValue());
}
@Test
public void not(){
BooleanBuilder builder = new BooleanBuilder();
builder.and(first).not();
assertEquals(first.not(), builder.getValue());
}
}

View File

@ -0,0 +1,77 @@
package com.mysema.query;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import org.junit.Test;
import com.mysema.query.types.path.PString;
public class DefaultQueryMetadataTest {
private QueryMetadata md = new DefaultQueryMetadata();
private PString str = new PString("str");
@Test
public void testGetGroupBy() {
md.addGroupBy(str);
assertEquals(Arrays.asList(str), md.getGroupBy());
}
@Test
public void testGetHaving() {
md.addHaving(str.isNotNull());
assertEquals(str.isNotNull(), md.getHaving());
}
@Test
public void testGetJoins() {
md.addJoin(JoinType.DEFAULT, str);
assertEquals(Arrays.asList(new JoinExpression(JoinType.DEFAULT, str)),md.getJoins());
}
@Test
public void testGetModifiers() {
QueryModifiers modifiers = new QueryModifiers(1l,2l);
md.setModifiers(modifiers);
assertEquals(modifiers, md.getModifiers());
}
@Test
public void testGetOrderBy() {
md.addOrderBy(str.asc());
md.addOrderBy(str.desc());
assertEquals(Arrays.asList(str.asc(),str.desc()), md.getOrderBy());
}
@Test
public void testGetProjection() {
md.addProjection(str, str.append("abc"));
assertEquals(Arrays.asList(str, str.append("abc")), md.getProjection());
}
@Test
public void testGetWhere() {
md.addWhere(str.eq("b"), str.isNotEmpty());
assertEquals(str.eq("b").and(str.isNotEmpty()), md.getWhere());
}
@Test
public void testIsDistinct() {
assertFalse(md.isDistinct());
md.setDistinct(true);
assertTrue(md.isDistinct());
}
@Test
public void testIsUnique() {
assertFalse(md.isUnique());
md.setUnique(true);
assertTrue(md.isUnique());
}
}

View File

@ -8,6 +8,9 @@ package com.mysema.query.alias;
import static com.mysema.query.alias.Alias.$;
import static org.junit.Assert.assertEquals;
import java.util.List;
import java.util.Map;
import org.junit.Test;
public class AliasTest {
@ -19,6 +22,10 @@ public class AliasTest {
String getLastName();
int getAge();
List<Person> getList();
Map<String,Person> getMap();
}
@Test
@ -26,6 +33,8 @@ public class AliasTest {
Person person = Alias.alias(Person.class);
assertEquals("lower(person.firstName)", $(person.getFirstName()).lower().toString());
assertEquals("person.age", $(person.getAge()).toString());
assertEquals("person.map.get(a)", $(person.getMap().get("a")).toString());
assertEquals("person.list.get(0)", $(person.getList().get(0)).toString());
}
}

View File

@ -5,10 +5,12 @@
*/
package com.mysema.query.codegen;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import com.mysema.codegen.JavaWriter;
@ -25,22 +27,46 @@ public class SerializerTest {
private Writer writer = new StringWriter();
public SerializerTest() {
private TypeMappings typeMappings = new TypeMappings();
@Before
public void setUp(){
TypeFactory typeFactory = new TypeFactory();
// type
Type typeModel = new SimpleType(TypeCategory.ENTITY, "com.mysema.query.DomainClass", "com.mysema.query", "DomainClass", false);
type = new EntityType("Q", typeModel);
// property
Property property = new Property(type, "field", typeFactory.create(String.class), new String[0]);
type.addProperty(property);
Property field = new Property(type, "field", typeFactory.create(String.class), new String[0]);
type.addProperty(field);
// constructor
Parameter param = new Parameter("name", new ClassType(TypeCategory.STRING, String.class));
type.addConstructor(new Constructor(Collections.singleton(param)));
// method
Method method = new Method(typeFactory.create(String.class), "method", "abc", typeFactory.create(String.class));
type.addMethod(method);
}
@Test
public void testDomainTypesAsOuterClasses() throws Exception {
TypeMappings typeMappings = new TypeMappings();
public void EntitySerializer() throws Exception {
new EntitySerializer(typeMappings).serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
// System.out.println(writer);
}
@Test
public void EmbeddableSerializer() throws Exception {
new EmbeddableSerializer(typeMappings).serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
}
@Test
public void SupertypeSerializer() throws IOException{
new SupertypeSerializer(typeMappings).serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
}
@Test
public void DTOSerializer() throws IOException{
new DTOSerializer(typeMappings).serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
}
}

View File

@ -1,3 +1,4 @@
/*
* Copyright (c) 2010 Mysema Ltd.
* All rights reserved.

View File

@ -0,0 +1,40 @@
package com.mysema.query.types.query;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import com.mysema.query.DefaultQueryMetadata;
import com.mysema.query.QueryMetadata;
import com.mysema.query.types.SubQuery;
public class SubQueryTest {
@SuppressWarnings("unchecked")
@Test
public void test(){
QueryMetadata metadata = new DefaultQueryMetadata();
List<SubQuery> subQueries = Arrays.<SubQuery>asList(
new BooleanSubQuery(metadata),
new ComparableSubQuery(Date.class,metadata),
new DateSubQuery(Date.class,metadata),
new DateTimeSubQuery(Date.class,metadata),
new ListSubQuery(Date.class,metadata),
new NumberSubQuery(Integer.class,metadata),
new ObjectSubQuery(String.class,metadata),
new StringSubQuery(metadata),
new TimeSubQuery(Date.class,metadata)
);
for (SubQuery sq : subQueries){
assertNotNull(sq.asExpr());
assertNotNull(sq.exists());
assertNotNull(sq.getMetadata());
assertNotNull(sq.notExists());
}
}
}

View File

@ -12,6 +12,7 @@ import org.hibernate.Session;
import org.hibernate.StatelessSession;
import com.mysema.query.DefaultQueryMetadata;
import com.mysema.query.JoinType;
import com.mysema.query.QueryMetadata;
import com.mysema.query.dml.DeleteClause;
import com.mysema.query.hql.HQLSerializer;
@ -44,7 +45,7 @@ public class HibernateDeleteClause implements DeleteClause<HibernateDeleteClause
public HibernateDeleteClause(SessionHolder session, PEntity<?> entity, HQLTemplates templates){
this.session = session;
this.templates = templates;
md.addFrom(entity);
md.addJoin(JoinType.DEFAULT, entity);
}
@Override

View File

@ -12,6 +12,7 @@ import org.hibernate.Session;
import org.hibernate.StatelessSession;
import com.mysema.query.DefaultQueryMetadata;
import com.mysema.query.JoinType;
import com.mysema.query.QueryMetadata;
import com.mysema.query.dml.UpdateClause;
import com.mysema.query.hql.HQLSerializer;
@ -45,7 +46,7 @@ public class HibernateUpdateClause implements UpdateClause<HibernateUpdateClause
public HibernateUpdateClause(SessionHolder session, PEntity<?> entity, HQLTemplates templates){
this.session = session;
this.templates = templates;
metadata.addFrom(entity);
metadata.addJoin(JoinType.DEFAULT, entity);
}
@Override

View File

@ -11,6 +11,7 @@ import javax.persistence.EntityManager;
import javax.persistence.Query;
import com.mysema.query.DefaultQueryMetadata;
import com.mysema.query.JoinType;
import com.mysema.query.QueryMetadata;
import com.mysema.query.dml.DeleteClause;
import com.mysema.query.hql.HQLSerializer;
@ -39,7 +40,7 @@ public class JPADeleteClause implements DeleteClause<JPADeleteClause>{
public JPADeleteClause(EntityManager entityManager, PEntity<?> entity, HQLTemplates templates){
this.entityManager = entityManager;
this.templates = templates;
metadata.addFrom(entity);
metadata.addJoin(JoinType.DEFAULT, entity);
}
@Override

View File

@ -11,6 +11,7 @@ import javax.persistence.EntityManager;
import javax.persistence.Query;
import com.mysema.query.DefaultQueryMetadata;
import com.mysema.query.JoinType;
import com.mysema.query.QueryMetadata;
import com.mysema.query.dml.UpdateClause;
import com.mysema.query.hql.HQLSerializer;
@ -40,7 +41,7 @@ public class JPAUpdateClause implements UpdateClause<JPAUpdateClause>{
public JPAUpdateClause(EntityManager em, PEntity<?> entity, HQLTemplates templates){
this.entityManager = em;
this.templates = templates;
metadata.addFrom(entity);
metadata.addJoin(JoinType.DEFAULT, entity);
}
@Override

View File

@ -8,6 +8,7 @@ package com.mysema.query.jdoql;
import java.util.Collection;
import com.mysema.query.DefaultQueryMetadata;
import com.mysema.query.JoinType;
import com.mysema.query.QueryMetadata;
import com.mysema.query.support.DetachableQuery;
import com.mysema.query.types.Expr;
@ -44,7 +45,7 @@ public class JDOQLSubQuery extends DetachableQuery<JDOQLSubQuery>{
}
public <P> JDOQLSubQuery from(Path<? extends Collection<P>> target, PEntity<P> alias){
queryMixin.getMetadata().addFrom(OSimple.create(alias.getType(), Ops.ALIAS, target.asExpr(), alias));
queryMixin.getMetadata().addJoin(JoinType.DEFAULT, OSimple.create(alias.getType(), Ops.ALIAS, target.asExpr(), alias));
return this;
}

View File

@ -13,6 +13,7 @@ import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import com.mysema.query.DefaultQueryMetadata;
import com.mysema.query.JoinType;
import com.mysema.query.QueryMetadata;
import com.mysema.query.dml.DeleteClause;
import com.mysema.query.jdoql.JDOQLSerializer;
@ -44,7 +45,7 @@ public class JDOQLDeleteClause implements DeleteClause<JDOQLDeleteClause>{
this.entity = entity;
this.persistenceManager = persistenceManager;
this.templates = templates;
metadata.addFrom(entity);
metadata.addJoin(JoinType.DEFAULT, entity);
}
@Override