OPENJPA-1013: add more test scenarios for parameter settting in Criteria Query

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@775367 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Fay Wang 2009-05-15 22:08:32 +00:00
parent f6c64474ed
commit 78f0f54dc3
2 changed files with 78 additions and 4 deletions

View File

@ -25,7 +25,7 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
@Entity
@ -39,7 +39,7 @@ public class CreditCard {
private Customer customer;
@OneToMany
@OrderBy
@OrderColumn
private List<TransactionHistory> transactionHistory;
public long getId() {

View File

@ -1,6 +1,7 @@
package org.apache.openjpa.persistence.criteria;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
@ -413,7 +414,7 @@ public class TestTypesafeCriteria extends SQLListenerTestCase {
}
@AllowFailure
public void testParameters() {
public void testParameters1() {
String jpql = "SELECT c FROM Customer c Where c.status = :stat";
CriteriaQuery q = cb.create();
Root<Customer> c = q.from(Customer.class);
@ -423,6 +424,56 @@ public class TestTypesafeCriteria extends SQLListenerTestCase {
assertEquivalence(q, jpql, new String[]{"stat"}, new Object[] {1});
}
@AllowFailure
public void testParameters2() {
String jpql = "SELECT c FROM Customer c Where c.status = :stat AND c.name = :name";
CriteriaQuery q = cb.create();
Root<Customer> c = q.from(Customer.class);
Parameter<Integer> param1 = cb.parameter(Integer.class, "stat");
Parameter<String> param2 = cb.parameter(String.class, "name");
q.select(c).where(cb.equal(c.get(Customer_.status), param1),
cb.equal(c.get(Customer_.name), param2));
assertEquivalence(q, jpql, new String[] { "stat", "name" }, new Object[] { 1, "test" });
}
@AllowFailure
public void testParameters3() {
String jpql = "SELECT c FROM Customer c Where c.status = :1";
CriteriaQuery q = cb.create();
Root<Customer> c = q.from(Customer.class);
Parameter<Integer> param = cb.parameter(Integer.class);
q.select(c).where(cb.equal(c.get(Customer_.status), param));
assertEquivalence(q, jpql, new Object[] { 1 });
}
@AllowFailure
public void testParameters4() {
String jpql = "SELECT c FROM Customer c Where c.status = :1 AND c.name = :2";
CriteriaQuery q = cb.create();
Root<Customer> c = q.from(Customer.class);
Parameter<Integer> param1 = cb.parameter(Integer.class);
Parameter<Integer> param2 = cb.parameter(Integer.class);
q.select(c).where(cb.equal(c.get(Customer_.status), param1),
cb.equal(c.get(Customer_.name), param2));
assertEquivalence(q, jpql, new Object[] { 1, "test" });
}
// collection-valued input parameter
@AllowFailure
public void testParameters5() {
String jpql = "SELECT c FROM Customer c Where c.status IN (:coll)";
CriteriaQuery q = cb.create();
Root<Customer> c = q.from(Customer.class);
Parameter<List> param1 = cb.parameter(List.class);
q.select(c).where(cb.equal(c.get(Customer_.status), param1));
List vals = new ArrayList();
vals.add(1);
vals.add(2);
assertEquivalence(q, jpql, new String[] {"coll"}, new Object[] {vals});
}
@AllowFailure
public void testSelectList1() {
String jpql = "SELECT v.location.street, KEY(i).title, VALUE(i) FROM " +
@ -440,6 +491,7 @@ public class TestTypesafeCriteria extends SQLListenerTestCase {
assertEquivalence(q, jpql);
}
@AllowFailure
public void testSelectList2() {
String jpql = "SELECT NEW CustomerDetails(c.id, c.status, o.quantity) FROM " +
@ -676,6 +728,28 @@ public class TestTypesafeCriteria extends SQLListenerTestCase {
assertEquals(jSQL, cSQL);
}
void assertEquivalence(CriteriaQuery c, String jpql,
Object[] params) {
sql.clear();
Query q = em.createQuery(c);
for (int i = 0; i < params.length; i++) {
q.setParameter(i+1, params[i]);
}
List cList = q.getResultList();
assertEquals(1, sql.size());
String cSQL = sql.get(0);
sql.clear();
q = em.createQuery(jpql);
for (int i = 0; i < params.length; i++) {
q.setParameter(i+1, params[i]);
}
List jList = q.getResultList();
assertEquals(1, sql.size());
String jSQL = sql.get(0);
assertEquals(jSQL, cSQL);
}
void assertEquivalence(CriteriaQuery c, String jpql) {
sql.clear();