mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
196 lines
6.9 KiB
Java
196 lines
6.9 KiB
Java
/*
|
|
* Copyright (c) 2009 Mysema Ltd.
|
|
* All rights reserved.
|
|
*
|
|
*/
|
|
package com.mysema.query;
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
import static org.junit.Assert.assertFalse;
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
|
|
import javax.jdo.PersistenceManager;
|
|
import javax.jdo.Transaction;
|
|
|
|
import org.junit.BeforeClass;
|
|
import org.junit.Ignore;
|
|
import org.junit.Test;
|
|
|
|
import com.mysema.commons.lang.Pair;
|
|
import com.mysema.query.jdo.AbstractJDOTest;
|
|
import com.mysema.query.jdo.test.domain.Product;
|
|
import com.mysema.query.jdo.test.domain.QProduct;
|
|
import com.mysema.query.jdo.test.domain.QStore;
|
|
import com.mysema.query.jdo.test.domain.Store;
|
|
import com.mysema.query.types.Expression;
|
|
import com.mysema.query.types.ParamNotSetException;
|
|
import com.mysema.query.types.expr.ArrayConstructorExpression;
|
|
import com.mysema.query.types.expr.BooleanExpression;
|
|
import com.mysema.query.types.expr.ConstructorExpression;
|
|
import com.mysema.query.types.expr.Param;
|
|
import com.mysema.query.types.expr.QTuple;
|
|
|
|
public class JDOQLQueryStandardTest extends AbstractJDOTest {
|
|
|
|
public static class Projection {
|
|
|
|
public Projection(String str) {
|
|
}
|
|
|
|
}
|
|
|
|
private static final Date publicationDate;
|
|
|
|
private static final java.sql.Date date;
|
|
|
|
private static final java.sql.Time time;
|
|
|
|
static{
|
|
Calendar cal = Calendar.getInstance();
|
|
cal.set(2000, 1, 2, 3, 4);
|
|
cal.set(Calendar.MILLISECOND, 0);
|
|
publicationDate = cal.getTime();
|
|
date = new java.sql.Date(cal.getTimeInMillis());
|
|
time = new java.sql.Time(cal.getTimeInMillis());
|
|
}
|
|
|
|
private static String productName = "ABCD";
|
|
private static String otherName = "ABC0";
|
|
|
|
@BeforeClass
|
|
public static void doPersist() {
|
|
// Persistence of a Product and a Book.
|
|
PersistenceManager pm = pmf.getPersistenceManager();
|
|
Transaction tx = pm.currentTransaction();
|
|
try {
|
|
tx.begin();
|
|
for (int i = 0; i < 10; i++) {
|
|
// Product instances
|
|
pm.makePersistent(new Product("ABC" + i, "F" + i, i * 200.00, 2, publicationDate));
|
|
pm.makePersistent(new Product("DEF" + i, "E" + i, i * 200.00, 4, publicationDate));
|
|
pm.makePersistent(new Product("GHI" + i, "D" + i, i * 200.00, 6, publicationDate));
|
|
|
|
// Product of Store
|
|
Product product = new Product(productName,"A",100.0,1, publicationDate);
|
|
pm.makePersistent(product);
|
|
|
|
// Store instances
|
|
Store store = new Store();
|
|
store.getProducts().add(product);
|
|
store.getProductsByName().put(productName, product);
|
|
pm.makePersistent(store);
|
|
}
|
|
tx.commit();
|
|
} finally {
|
|
if (tx.isActive()) {
|
|
tx.rollback();
|
|
}
|
|
pm.close();
|
|
}
|
|
System.out.println("");
|
|
|
|
}
|
|
|
|
private QueryExecution standardTest = new QueryExecution(Module.JDOQL, Target.HSQLDB){
|
|
@Override
|
|
protected Pair<Projectable, List<Expression<?>>> createQuery() {
|
|
return Pair.of(
|
|
(Projectable)query().from(store, product, otherProduct),
|
|
Arrays.<Expression<?>>asList(store, product, otherProduct));
|
|
}
|
|
@Override
|
|
protected Pair<Projectable, List<Expression<?>>> createQuery(BooleanExpression filter) {
|
|
return Pair.of(
|
|
(Projectable)query().from(store, product, otherProduct).where(filter),
|
|
Arrays.<Expression<?>>asList(store, product, otherProduct));
|
|
}
|
|
};
|
|
|
|
private QProduct product = QProduct.product;
|
|
|
|
private QProduct otherProduct = new QProduct("otherProduct");
|
|
|
|
private QStore store = QStore.store;
|
|
|
|
private QStore otherStore = new QStore("otherStore");
|
|
|
|
@Test
|
|
public void test(){
|
|
Product p = query().from(product).where(product.name.eq(productName)).limit(1).uniqueResult(product);
|
|
Product p2 = query().from(product).where(product.name.startsWith(otherName)).limit(1).uniqueResult(product);
|
|
standardTest.noProjections();
|
|
standardTest.noCounts();
|
|
|
|
standardTest.runBooleanTests(product.name.isNull(), otherProduct.price.lt(10.00));
|
|
standardTest.runCollectionTests(store.products, otherStore.products, p, p2);
|
|
standardTest.runDateTests(product.dateField, otherProduct.dateField, date);
|
|
standardTest.runDateTimeTests(product.publicationDate, otherProduct.publicationDate, publicationDate);
|
|
// NO list support in JDOQL
|
|
// testData.listTests(store.products, otherStore.products, p);
|
|
standardTest.runMapTests(store.productsByName, otherStore.productsByName, productName, p, "X", p2);
|
|
standardTest.runNumericCasts(product.price, otherProduct.price, 200.0);
|
|
standardTest.runNumericTests(product.amount, otherProduct.amount, 2);
|
|
standardTest.runStringTests(product.name, otherProduct.name, productName);
|
|
standardTest.runTimeTests(product.timeField, otherProduct.timeField, time);
|
|
|
|
standardTest.report();
|
|
}
|
|
|
|
@Test
|
|
public void tupleProjection(){
|
|
List<Tuple> tuples = query().from(product).list(new QTuple(product.name, product.price));
|
|
assertFalse(tuples.isEmpty());
|
|
for (Tuple tuple : tuples){
|
|
assertNotNull(tuple);
|
|
assertNotNull(tuple.get(product.name));
|
|
assertNotNull(tuple.get(product.price));
|
|
assertNotNull(tuple.get(0,String.class));
|
|
assertNotNull(tuple.get(1,Double.class));
|
|
}
|
|
}
|
|
|
|
@Test
|
|
@Ignore
|
|
public void arrayProjection(){
|
|
// typed array not supported
|
|
List<String[]> results = query().from(store).list(new ArrayConstructorExpression<String>(String[].class, store.name));
|
|
assertFalse(results.isEmpty());
|
|
for (String[] result : results){
|
|
assertNotNull(result);
|
|
assertNotNull(result[0]);
|
|
}
|
|
}
|
|
|
|
@Test
|
|
public void constructorProjection(){
|
|
List<Projection> projections = query().from(store).list(ConstructorExpression.create(Projection.class, store.name));
|
|
assertFalse(projections.isEmpty());
|
|
for (Projection projection : projections){
|
|
assertNotNull(projection);
|
|
}
|
|
}
|
|
|
|
@Test
|
|
public void testParams(){
|
|
Param<String> name = new Param<String>(String.class,"name");
|
|
assertEquals("ABC0",query().from(product).where(product.name.eq(name)).set(name, "ABC0").uniqueResult(product.name));
|
|
}
|
|
|
|
@Test
|
|
public void testParams_anon(){
|
|
Param<String> name = new Param<String>(String.class);
|
|
assertEquals("ABC0",query().from(product).where(product.name.eq(name)).set(name, "ABC0").uniqueResult(product.name));
|
|
}
|
|
|
|
@Test(expected=ParamNotSetException.class)
|
|
public void testParams_not_set(){
|
|
Param<String> name = new Param<String>(String.class,"name");
|
|
assertEquals("ABC0",query().from(product).where(product.name.eq(name)).uniqueResult(product.name));
|
|
}
|
|
}
|