/* * 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 static org.junit.Assert.assertTrue; 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.ArrayConstructorExpression; import com.mysema.query.types.ConstructorExpression; import com.mysema.query.types.Expression; import com.mysema.query.types.ParamNotSetException; import com.mysema.query.types.QTuple; import com.mysema.query.types.expr.BooleanExpression; import com.mysema.query.types.expr.Param; 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 final QueryExecution standardTest = new QueryExecution(Module.JDOQL, Target.H2){ @Override protected Pair>> createQuery() { return Pair.of( (Projectable)query().from(store, product, otherProduct), Arrays.>asList(store, product, otherProduct)); } @Override protected Pair>> createQuery(BooleanExpression filter) { return Pair.of( (Projectable)query().from(store, product, otherProduct).where(filter), Arrays.>asList(store, product, otherProduct)); } }; private final QProduct product = QProduct.product; private final QProduct otherProduct = new QProduct("otherProduct"); private final QStore store = QStore.store; private final QStore otherStore = new QStore("otherStore"); @Test public void StandardTest(){ 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 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)); } } @SuppressWarnings("unchecked") @Test @Ignore public void ArrayProjection(){ // typed array not supported List results = query().from(store).list(new ArrayConstructorExpression(String[].class, store.name)); assertFalse(results.isEmpty()); for (String[] result : results){ assertNotNull(result); assertNotNull(result[0]); } } @Test public void ConstructorProjection(){ List projections = query().from(store).list(ConstructorExpression.create(Projection.class, store.name)); assertFalse(projections.isEmpty()); for (Projection projection : projections){ assertNotNull(projection); } } @Test public void Params(){ Param name = new Param(String.class,"name"); assertEquals("ABC0",query().from(product).where(product.name.eq(name)).set(name, "ABC0").uniqueResult(product.name)); } @Test public void Params_anon(){ Param name = new Param(String.class); assertEquals("ABC0",query().from(product).where(product.name.eq(name)).set(name, "ABC0").uniqueResult(product.name)); } @Test(expected=ParamNotSetException.class) public void Params_not_set(){ Param name = new Param(String.class,"name"); assertEquals("ABC0",query().from(product).where(product.name.eq(name)).uniqueResult(product.name)); } @Test public void Exists(){ assertTrue(query().from(product).where(product.name.eq("ABC0")).exists()); } @Test public void NotExists(){ assertTrue(query().from(product).where(product.name.eq("XXX")).notExists()); } }