Author: epbernard
Date: 2009-12-21 08:58:50 -0500 (Mon, 21 Dec 2009)
New Revision: 18300
Added:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/tuple/
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/tuple/Customer.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/tuple/TupleCriteriaTest.java
Log:
HHH-4724 add tests on Tuple
Added:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/tuple/Customer.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/tuple/Customer.java
(rev 0)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/tuple/Customer.java 2009-12-21
13:58:50 UTC (rev 18300)
@@ -0,0 +1,43 @@
+package org.hibernate.ejb.criteria.tuple;
+
+import javax.persistence.Table;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+@Table(name="tup_cust")
+public class Customer {
+ private Long id;
+ private String name;
+ private Integer age;
+
+ @Id
+ @GeneratedValue
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public Integer getAge() {
+ return age;
+ }
+
+ public void setAge(Integer age) {
+ this.age = age;
+ }
+}
Added:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/tuple/TupleCriteriaTest.java
===================================================================
---
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/tuple/TupleCriteriaTest.java
(rev 0)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/tuple/TupleCriteriaTest.java 2009-12-21
13:58:50 UTC (rev 18300)
@@ -0,0 +1,74 @@
+package org.hibernate.ejb.criteria.tuple;
+
+import java.util.List;
+import javax.persistence.EntityManager;
+import javax.persistence.Tuple;
+import javax.persistence.TupleElement;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.Root;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Path;
+
+import org.hibernate.ejb.test.TestCase;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class TupleCriteriaTest extends TestCase {
+ public void testArray() {
+ EntityManager em = factory.createEntityManager();
+ Customer c1 = new Customer();
+ c1.setAge( 18 );
+ c1.setName( "Bob" );
+ em.getTransaction().begin();
+ em.persist( c1 );
+ em.flush();
+
+ final CriteriaBuilder cb = em.getCriteriaBuilder();
+ CriteriaQuery<Object[]> q = cb.createQuery(Object[].class);
+ Root<Customer> c = q.from(Customer.class);
+ q.select( cb.array( c.get(Customer_.name), c.get(Customer_.age) ) );
+ List<Object[]> result = em.createQuery(q).getResultList();
+
+ assertEquals( 1, result.size() );
+ assertEquals( c1.getName(), result.get( 0 )[0] );
+ assertEquals( c1.getAge(), result.get( 0 )[1] );
+
+ em.getTransaction().rollback();
+ em.close();
+ }
+
+
+ public void testTuple() {
+ EntityManager em = factory.createEntityManager();
+ Customer c1 = new Customer();
+ c1.setAge( 18 );
+ c1.setName( "Bob" );
+ em.getTransaction().begin();
+ em.persist( c1 );
+ em.flush();
+
+ final CriteriaBuilder cb = em.getCriteriaBuilder();
+
+ CriteriaQuery<Tuple> q = cb.createTupleQuery();
+ Root<Customer> c = q.from(Customer.class);
+ Path<String> tname = c.get(Customer_.name);
+ q.multiselect( tname, c.get(Customer_.age).alias("age") );
+ List<Tuple> result = em.createQuery(q).getResultList();
+
+ assertEquals( 1, result.size() );
+ //FIXME uncomment when HHH-4724 is fixed
+// assertEquals( c1.getName(), result.get(0).get(tname) );
+// assertEquals( c1.getAge(), result.get(0).get("age") );
+
+
+ em.getTransaction().rollback();
+ em.close();
+ }
+
+ public Class[] getAnnotatedClasses() {
+ return new Class[] {
+ Customer.class
+ };
+ }
+}