HHH-5736 Adding actual asserts to the tests and adding some new tests

This commit is contained in:
Hardy Ferentschik 2010-12-17 15:33:39 +01:00
parent a4fdb870f2
commit 18ed85a0cd
1 changed files with 85 additions and 62 deletions

View File

@ -23,11 +23,12 @@
*/ */
package org.hibernate.ejb.criteria.basic; package org.hibernate.ejb.criteria.basic;
import java.util.List;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.hibernate.ejb.metamodel.AbstractMetamodelSpecificTest; import org.hibernate.ejb.metamodel.AbstractMetamodelSpecificTest;
import org.hibernate.ejb.metamodel.Order; import org.hibernate.ejb.metamodel.Order;
@ -36,99 +37,121 @@ import org.hibernate.ejb.metamodel.Order;
* Test the various predicates. * Test the various predicates.
* *
* @author Steve Ebersole * @author Steve Ebersole
* @author Hardy Ferentschik
*/ */
public class PredicateTest extends AbstractMetamodelSpecificTest { public class PredicateTest extends AbstractMetamodelSpecificTest {
public void testEmptyConjunction() {
// yes this is a retarded case, but explicitly allowed in the JPA spec private EntityManager em;
CriteriaBuilder builder = factory.getCriteriaBuilder(); private CriteriaBuilder builder;
EntityManager em = getOrCreateEntityManager();
public void setUp() throws Exception {
super.setUp();
builder = factory.getCriteriaBuilder();
em = getOrCreateEntityManager();
createTestOrders();
em.getTransaction().begin(); em.getTransaction().begin();
CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class); }
Root<Order> orderRoot = orderCriteria.from(Order.class);
orderCriteria.select(orderRoot); public void tearDown() throws Exception {
orderCriteria.where( builder.isTrue( builder.conjunction() ) );
em.createQuery( orderCriteria ).getResultList();
em.getTransaction().commit(); em.getTransaction().commit();
em.close(); em.close();
super.tearDown();
}
public void testEmptyConjunction() {
// yes this is a retarded case, but explicitly allowed in the JPA spec
CriteriaQuery<Order> orderCriteria = builder.createQuery( Order.class );
Root<Order> orderRoot = orderCriteria.from( Order.class );
orderCriteria.select( orderRoot );
orderCriteria.where( builder.isTrue( builder.conjunction() ) );
em.createQuery( orderCriteria ).getResultList();
List<Order> orders = em.createQuery( orderCriteria ).getResultList();
assertTrue( orders.size() == 3 );
} }
public void testEmptyDisjunction() { public void testEmptyDisjunction() {
// yes this is a retarded case, but explicitly allowed in the JPA spec // yes this is a retarded case, but explicitly allowed in the JPA spec
CriteriaBuilder builder = factory.getCriteriaBuilder(); CriteriaQuery<Order> orderCriteria = builder.createQuery( Order.class );
EntityManager em = getOrCreateEntityManager(); Root<Order> orderRoot = orderCriteria.from( Order.class );
em.getTransaction().begin(); orderCriteria.select( orderRoot );
CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class);
Root<Order> orderRoot = orderCriteria.from(Order.class);
orderCriteria.select(orderRoot);
orderCriteria.where( builder.isFalse( builder.disjunction() ) ); orderCriteria.where( builder.isFalse( builder.disjunction() ) );
em.createQuery( orderCriteria ).getResultList(); em.createQuery( orderCriteria ).getResultList();
em.getTransaction().commit();
em.close(); List<Order> orders = em.createQuery( orderCriteria ).getResultList();
assertTrue( orders.size() == 3 );
} }
/** /**
* Check simple not. * Check simple not.
*/ */
public void testSimpleNot() { public void testSimpleNot() {
// yes this is a retarded case, but explicitly allowed in the JPA spec CriteriaQuery<Order> orderCriteria = builder.createQuery( Order.class );
CriteriaBuilder builder = factory.getCriteriaBuilder(); Root<Order> orderRoot = orderCriteria.from( Order.class );
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class); orderCriteria.select( orderRoot );
Root<Order> orderRoot = orderCriteria.from(Order.class); orderCriteria.where( builder.not( builder.equal( orderRoot.get( "id" ), "order-1" ) ) );
orderCriteria.select(orderRoot); List<Order> orders = em.createQuery( orderCriteria ).getResultList();
orderCriteria.where( builder.not( builder.equal(orderRoot.get("id"), 1) ) ); assertTrue( orders.size() == 2 );
em.createQuery( orderCriteria ).getResultList();
em.getTransaction().commit();
em.close();
} }
/** /**
* Check complicated not. * Check complicated not.
*/ */
public void testComplicatedNotOr() { public void testComplicatedNotOr() {
// yes this is a retarded case, but explicitly allowed in the JPA spec CriteriaQuery<Order> orderCriteria = builder.createQuery( Order.class );
CriteriaBuilder builder = factory.getCriteriaBuilder(); Root<Order> orderRoot = orderCriteria.from( Order.class );
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class); orderCriteria.select( orderRoot );
Root<Order> orderRoot = orderCriteria.from(Order.class); Predicate p1 = builder.equal( orderRoot.get( "id" ), "order-1" );
Predicate p2 = builder.equal( orderRoot.get( "id" ), "order-2" );
orderCriteria.where( builder.not( builder.or( p1, p2 ) ) );
orderCriteria.select(orderRoot); List<Order> orders = em.createQuery( orderCriteria ).getResultList();
Predicate p1 = builder.equal(orderRoot.get("id"), 1); assertTrue( orders.size() == 1 );
Predicate p2 = builder.equal(orderRoot.get("id"), 2); Order order = orders.get( 0 );
orderCriteria.where( builder.not( builder.or (p1, p2) ) ); assertEquals( "order-3", order.getId() );
em.createQuery( orderCriteria ).getResultList();
em.getTransaction().commit();
em.close();
} }
/** /**
* Check complicated not. * Check complicated not.
*/ */
public void testComplicatedNotAND() { public void testNotMultipleOr() {
// yes this is a retarded case, but explicitly allowed in the JPA spec CriteriaQuery<Order> orderCriteria = builder.createQuery( Order.class );
CriteriaBuilder builder = factory.getCriteriaBuilder(); Root<Order> orderRoot = orderCriteria.from( Order.class );
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaQuery<Order> orderCriteria = builder.createQuery(Order.class); orderCriteria.select( orderRoot );
Root<Order> orderRoot = orderCriteria.from(Order.class); Predicate p1 = builder.equal( orderRoot.get( "id" ), "order-1" );
Predicate p2 = builder.equal( orderRoot.get( "id" ), "order-2" );
Predicate p3 = builder.equal( orderRoot.get( "id" ), "order-3" );
orderCriteria.where( builder.not( builder.or( p1, p2, p3 ) ) );
orderCriteria.select(orderRoot); List<Order> orders = em.createQuery( orderCriteria ).getResultList();
Predicate p1 = builder.equal(orderRoot.get("id"), 1); assertTrue( orders.size() == 0 );
Predicate p2 = builder.equal(orderRoot.get("id"), 2);
orderCriteria.where( builder.not( builder.and (p1, p2) ) );
em.createQuery( orderCriteria ).getResultList();
em.getTransaction().commit();
em.close();
} }
/**
* Check complicated not.
*/
public void testComplicatedNotAnd() {
CriteriaQuery<Order> orderCriteria = builder.createQuery( Order.class );
Root<Order> orderRoot = orderCriteria.from( Order.class );
orderCriteria.select( orderRoot );
Predicate p1 = builder.equal( orderRoot.get( "id" ), "order-1" );
Predicate p2 = builder.equal( orderRoot.get( "id" ), "order-2" );
orderCriteria.where( builder.not( builder.and( p1, p2 ) ) );
List<Order> orders = em.createQuery( orderCriteria ).getResultList();
assertTrue( orders.size() == 3 );
}
private void createTestOrders() {
em.getTransaction().begin();
em.persist( new Order( "order-1", 1.0d ) );
em.persist( new Order( "order-2", 10.0d ) );
em.persist( new Order( "order-3", 100.0d ) );
em.getTransaction().commit();
}
} }