added tests

This commit is contained in:
Timo Westkämper 2010-05-15 13:26:44 +00:00
parent f8dc0dfd6c
commit 5c1fd2d503
10 changed files with 233 additions and 18 deletions

View File

@ -21,13 +21,19 @@ public final class BooleanBuilder extends EBoolean implements Cloneable{
@Nullable
private EBoolean expr;
public BooleanBuilder() {}
public BooleanBuilder(EBoolean initial){
expr = initial;
}
@Override
public void accept(Visitor v) {
if (expr != null){
expr.accept(v);
}else{
throw new QueryException("CascadingBoolean has no value");
throw new QueryException("BooleanBuilder has no value");
}
}
@ -70,8 +76,8 @@ public final class BooleanBuilder extends EBoolean implements Cloneable{
public boolean equals(Object o) {
if (o == this){
return true;
}else if (o instanceof EBoolean){
return expr != null ? expr.equals(o) : false;
}else if (o instanceof BooleanBuilder){
return ((BooleanBuilder)o).getValue().equals(expr);
}else{
return false;
}
@ -84,7 +90,7 @@ public final class BooleanBuilder extends EBoolean implements Cloneable{
@Override
public int hashCode(){
return Boolean.class.hashCode();
return expr != null ? expr.hashCode() : super.hashCode();
}
/**

View File

@ -153,6 +153,10 @@ class PropertyAccessInvocationHandler implements MethodInterceptor {
} else if (Integer.class.equals(type) || int.class.equals(type)) {
path = new PNumber<Integer>(Integer.class, pm);
rv = Integer.valueOf(RETURN_VALUE);
} else if (Byte.class.equals(type) || byte.class.equals(type)) {
path = new PNumber<Byte>(Byte.class, pm);
rv = Byte.valueOf((byte)RETURN_VALUE);
} else if (java.util.Date.class.equals(type)) {
path = new PDateTime<Date>(Date.class, pm);

View File

@ -6,6 +6,7 @@
package com.mysema.query;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import org.junit.Test;
@ -75,4 +76,16 @@ public class BooleanBuilderTest {
builder.and(first).not();
assertEquals(first.not(), builder.getValue());
}
@Test
public void equals(){
assertEquals(new BooleanBuilder(first), new BooleanBuilder(first));
assertFalse(first.equals(new BooleanBuilder(first)));
assertFalse(new BooleanBuilder(first).equals(first));
}
@Test
public void testHashCode(){
assertEquals(new BooleanBuilder(first).hashCode(), new BooleanBuilder(first).hashCode());
}
}

View File

@ -41,6 +41,7 @@ public class DefaultQueryMetadataTest {
assertEquals(modifiers, md.getModifiers());
}
@SuppressWarnings("unchecked")
@Test
public void testGetOrderBy() {
md.addOrderBy(str.asc());

View File

@ -0,0 +1,39 @@
package com.mysema.query;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.mysema.query.types.expr.EBooleanConst;
import com.mysema.query.types.path.PString;
public class JoinExpressionTest {
private JoinExpression je = new JoinExpression(JoinType.DEFAULT, new PString("str"));
@Test
public void testToString(){
assertEquals("DEFAULT str", je.toString());
}
@Test
public void testSetCondition() {
je.setCondition(EBooleanConst.TRUE);
assertEquals(EBooleanConst.TRUE, je.getCondition());
}
@Test
public void testSetFlag() {
assertFalse(je.hasFlag("X"));
je.setFlag("X");
assertTrue(je.hasFlag("X"));
assertFalse(je.hasFlag("Y"));
je.removeFlag("X");
assertFalse(je.hasFlag("X"));
}
}

View File

@ -38,4 +38,16 @@ public class QueryModifiersTest {
assertNull(modifiers.getOffset());
assertFalse(modifiers.isRestricting());
}
@Test
public void testHashCode(){
QueryModifiers modifiers1 = new QueryModifiers(null, null);
QueryModifiers modifiers2 = new QueryModifiers(1l, null);
QueryModifiers modifiers3 = new QueryModifiers(null, 1l);
assertEquals(modifiers1.hashCode(), new QueryModifiers().hashCode());
assertEquals(modifiers2.hashCode(), QueryModifiers.limit(1l).hashCode());
assertEquals(modifiers3.hashCode(), QueryModifiers.offset(1l).hashCode());
}
}

View File

@ -0,0 +1,41 @@
package com.mysema.query;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
public class SearchResultsTest {
private List<Integer> list = Arrays.<Integer>asList(0,1,2,3,4,5,6,7,8,9);
private SearchResults<Integer> results = new SearchResults<Integer>(list,10l,0l,20);
@Test
public void getResults(){
assertEquals(list, results.getResults());
}
@Test
public void getTotal(){
assertEquals(20l , results.getTotal());
}
@Test
public void isEmpty(){
assertFalse(results.isEmpty());
}
@Test
public void getLimit(){
assertEquals(10l, results.getLimit());
}
@Test
public void getOffset(){
assertEquals(0l, results.getOffset());
}
}

View File

@ -8,14 +8,20 @@ package com.mysema.query.alias;
import static com.mysema.query.alias.Alias.$;
import static org.junit.Assert.assertEquals;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
public class AliasTest {
public interface Person{
public interface DomainType{
String getFirstName();
@ -23,18 +29,61 @@ public class AliasTest {
int getAge();
List<Person> getList();
List<DomainType> getList();
Map<String,DomainType> getMap();
BigDecimal getBigDecimal();
BigInteger getBigInteger();
Byte getByte();
Collection<DomainType> getCollection();
Double getDouble();
Float getFloat();
java.sql.Date getDate();
java.util.Date getDate2();
Set<DomainType> getSet();
Short getShort();
Time getTime();
Timestamp getTimestamp();
Map<String,Person> getMap();
}
@Test
public void basicUsage(){
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());
DomainType domainType = Alias.alias(DomainType.class);
assertEquals("lower(domainType.firstName)", $(domainType.getFirstName()).lower().toString());
assertEquals("domainType.age", $(domainType.getAge()).toString());
assertEquals("domainType.map.get(a)", $(domainType.getMap().get("a")).toString());
assertEquals("domainType.list.get(0)", $(domainType.getList().get(0)).toString());
assertEquals("domainType.bigDecimal", $(domainType.getBigDecimal()).toString());
assertEquals("domainType.bigInteger", $(domainType.getBigInteger()).toString());
assertEquals("domainType.byte", $(domainType.getByte()).toString());
assertEquals("domainType.collection", $(domainType.getCollection()).toString());
assertEquals("domainType.double", $(domainType.getDouble()).toString());
assertEquals("domainType.float", $(domainType.getFloat()).toString());
assertEquals("domainType.date", $(domainType.getDate()).toString());
assertEquals("domainType.date2", $(domainType.getDate2()).toString());
assertEquals("domainType.set", $(domainType.getSet()).toString());
assertEquals("domainType.short", $(domainType.getShort()).toString());
assertEquals("domainType.time", $(domainType.getTime()).toString());
assertEquals("domainType.timestamp", $(domainType.getTimestamp()).toString());
}
@Test
public void otherMethods(){
DomainType domainType = Alias.alias(DomainType.class);
assertEquals("domainType", domainType.toString());
}
}

View File

@ -0,0 +1,34 @@
package com.mysema.query.codegen;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import java.util.Map;
import org.junit.Test;
public class ClassTypeTest {
private ClassType stringType = new ClassType(TypeCategory.STRING, String.class);
@Test
public void asArrayType(){
assertEquals(stringType, stringType.asArrayType().getParameter(0));
}
@Test
public void as(){
assertEquals(TypeCategory.COMPARABLE, stringType.as(TypeCategory.COMPARABLE).getCategory());
}
@Test
public void getParameters(){
ClassType mapType = new ClassType(TypeCategory.MAP, Map.class, stringType, stringType);
assertEquals(2, mapType.getParameterCount());
assertEquals(stringType, mapType.getParameter(0));
assertEquals(stringType, mapType.getParameter(1));
assertEquals(stringType, mapType.getSelfOrValueType());
assertFalse(mapType.isPrimitive());
}
}

View File

@ -8,8 +8,11 @@ package com.mysema.query.codegen;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collections;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.junit.Before;
import org.junit.Test;
@ -29,6 +32,7 @@ public class SerializerTest {
private TypeMappings typeMappings = new TypeMappings();
@SuppressWarnings("unchecked")
@Before
public void setUp(){
TypeFactory typeFactory = new TypeFactory();
@ -38,12 +42,19 @@ public class SerializerTest {
type = new EntityType("Q", typeModel);
// property
Property property = new Property(type, "field", typeFactory.create(String.class), new String[0]);
type.addProperty(property);
type.addProperty(new Property(type, "entityField", type, new String[0]));
type.addProperty(new Property(type, "listField", new ClassType(TypeCategory.LIST, List.class, typeModel), new String[0]));
type.addProperty(new Property(type, "mapField", new ClassType(TypeCategory.MAP, List.class, typeModel, typeModel), new String[0]));
for (Class<?> cl : Arrays.asList(Boolean.class, Comparable.class, Integer.class, Date.class, java.sql.Date.class, java.sql.Time.class)){
Type classType = new ClassType(TypeCategory.get(cl.getName()), cl);
type.addProperty(new Property(type, StringUtils.uncapitalize(cl.getSimpleName()), classType, new String[0]));
}
// constructor
Parameter param = new Parameter("name", new ClassType(TypeCategory.STRING, String.class));
type.addConstructor(new Constructor(Collections.singleton(param)));
Parameter firstName = new Parameter("firstName", new ClassType(TypeCategory.STRING, String.class));
Parameter lastName = new Parameter("lastName", new ClassType(TypeCategory.STRING, String.class));
type.addConstructor(new Constructor(Arrays.asList(firstName, lastName)));
// method
Method method = new Method(typeFactory.create(String.class), "method", "abc", typeFactory.create(String.class));
@ -55,6 +66,11 @@ public class SerializerTest {
new EntitySerializer(typeMappings).serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
}
@Test
public void EntitySerializer2() throws Exception {
new EntitySerializer(typeMappings).serialize(type, new SimpleSerializerConfig(true,true,true,true), new JavaWriter(writer));
}
@Test
public void EmbeddableSerializer() throws Exception {
new EmbeddableSerializer(typeMappings).serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));