use of org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer in project eclipselink by eclipse-ee4j.
the class RelationshipModelJUnitTestSuite method testGetResultListTest.
/*
* Tests using the 'getSingleResult' api on a Query object obtained from the
* EntityManager Also tests bugs 4300879 - check non Collection container
* policy error and 4297903 - check ReadObjectQuery fails
*/
public void testGetResultListTest() {
Collection returnedCustomers1, returnedCustomers2;
QueryException expectedException1 = null;
String ejbql1 = "SELECT OBJECT(thecust) FROM FieldAccessCustomer thecust WHERE thecust.customerId = :id";
Integer[] cusIDs = new Integer[3];
Customer cusClone1 = RelationshipsExamples.customerExample1();
Customer cusClone2 = RelationshipsExamples.customerExample2();
EntityManager em = createEntityManager("fieldaccess");
beginTransaction(em);
em.persist(cusClone1);
em.persist(cusClone2);
commitTransaction(em);
em.clear();
clearCache("fieldaccess");
cusIDs[0] = cusClone1.getCustomerId();
cusIDs[1] = cusClone2.getCustomerId();
try {
beginTransaction(em);
EntityManagerImpl entityManagerImpl = (EntityManagerImpl) em.getDelegate();
Query query1 = em.createNamedQuery("findAllCustomersFieldAccess");
returnedCustomers1 = query1.getResultList();
Query query2 = em.createQuery(ejbql1);
query2.setParameter("id", -10);
returnedCustomers2 = query2.getResultList();
// bug:4297903, check container policy failure
EJBQueryImpl query3 = (EJBQueryImpl) entityManagerImpl.createQuery(ejbql1);
ReadAllQuery readAllQuery = new ReadAllQuery(Customer.class);
MapContainerPolicy mapContainerPolicy = new MapContainerPolicy();
mapContainerPolicy.setContainerClass(HashMap.class);
mapContainerPolicy.setKeyName("hashCode");
readAllQuery.setContainerPolicy(mapContainerPolicy);
query3.setDatabaseQuery(readAllQuery);
try {
query3.getResultList();
} catch (PersistenceException exc) {
// QueryException.INVALID_CONTAINER_CLASS
expectedException1 = (QueryException) exc.getCause();
rollbackTransaction(em);
beginTransaction(em);
}
entityManagerImpl = (EntityManagerImpl) em.getDelegate();
// bug:4300879, check ReadObjectQuery fails
EJBQueryImpl query4 = (EJBQueryImpl) entityManagerImpl.createQuery(ejbql1);
query4.setParameter("id", -10);
ReadObjectQuery readObjectQuery2 = new ReadObjectQuery(Customer.class);
readObjectQuery2.setEJBQLString(ejbql1);
query4.setDatabaseQuery(readObjectQuery2);
query4.getResultList();
commitTransaction(em);
if (returnedCustomers1 == null || (returnedCustomers1.size() < 2)) {
fail("Not all customers were returned from findAllCustomers query ");
}
if (returnedCustomers2 == null || (returnedCustomers2.size() != 0)) {
fail("Customer from ReadObjectQuery was not returned using getResultCollection");
}
if (expectedException1 == null || (expectedException1.getErrorCode() != QueryException.INVALID_CONTAINER_CLASS)) {
fail("getResultCollection on query returning a hashtable did not throw expected INVALID_CONTAINER_CLASS QueryException");
}
beginTransaction(em);
Customer cus1 = em.find(Customer.class, cusIDs[0]);
em.remove(cus1);
Customer cus2 = em.find(Customer.class, cusIDs[1]);
em.remove(cus2);
commitTransaction(em);
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer in project eclipselink by eclipse-ee4j.
the class RelationshipModelJUnitTestSuite method testUpdateCustomer.
// Test that updating a customer works correctly.
public void testUpdateCustomer() {
Customer customer = RelationshipsExamples.customerExample4();
EntityManager em = createEntityManager("fieldaccess");
try {
beginTransaction(em);
em.persist(customer);
commitTransaction(em);
closeEntityManager(em);
clearCache("fieldaccess");
em = createEntityManager("fieldaccess");
beginTransaction(em);
customer = em.find(Customer.class, customer.getCustomerId());
Item item1 = RelationshipsExamples.itemExample1();
Order order1 = RelationshipsExamples.orderExample1();
customer.addOrder(order1);
order1.setItem(item1);
em.flush();
Item item2 = RelationshipsExamples.itemExample2();
Order order2 = RelationshipsExamples.orderExample2();
customer.addOrder(order2);
order2.setItem(item2);
Item item3 = RelationshipsExamples.itemExample3();
Order order3 = RelationshipsExamples.orderExample3();
customer.addOrder(order3);
order3.setItem(item3);
// Force LAZY otherwise compare will fail on was.
getServerSession("fieldaccess").copy(customer);
commitTransaction(em);
beginTransaction(em);
verifyObjectInEntityManager(customer, "fieldaccess");
verifyObjectInEntityManager(order1, "fieldaccess");
verifyObjectInEntityManager(item1, "fieldaccess");
verifyObjectInEntityManager(order2, "fieldaccess");
verifyObjectInEntityManager(item2, "fieldaccess");
verifyObjectInEntityManager(order3, "fieldaccess");
verifyObjectInEntityManager(item3, "fieldaccess");
commitTransaction(em);
clearCache("fieldaccess");
beginTransaction(em);
verifyObjectInEntityManager(customer, "fieldaccess");
verifyObjectInEntityManager(order1, "fieldaccess");
verifyObjectInEntityManager(item1, "fieldaccess");
verifyObjectInEntityManager(order2, "fieldaccess");
verifyObjectInEntityManager(item2, "fieldaccess");
verifyObjectInEntityManager(order3, "fieldaccess");
verifyObjectInEntityManager(item3, "fieldaccess");
commitTransaction(em);
clearCache("fieldaccess");
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer in project eclipselink by eclipse-ee4j.
the class RelationshipModelJUnitTestSuite method testExecuteUpdateTest.
/*
* Tests for the executeUpdate method on the EJBQueryImpl class.
* Also tests bugs 4288845 and 4293920, that params are passed in and used correctly.
*/
public void testExecuteUpdateTest() {
EntityManager em = createEntityManager("fieldaccess");
Integer[] cusIDs = new Integer[2];
String nameChange1 = "New Name1";
String nameChange2 = "New Name2";
String nameChange3 = "New Name3";
String returnedName1, returnedName2, returnedName3;
Exception expectedException = null;
Exception expectedException2 = null;
try {
Customer cusClone1 = RelationshipsExamples.customerExample1();
beginTransaction(em);
em.persist(cusClone1);
commitTransaction(em);
em.clear();
clearCache("fieldaccess");
cusIDs[0] = cusClone1.getCustomerId();
beginTransaction(em);
Customer cus = em.find(Customer.class, cusIDs[0]);
Query query = em.createQuery("UPDATE FieldAccessCustomer customer SET customer.name = '" + nameChange1 + "' WHERE customer.customerId = " + cusIDs[0]);
query.executeUpdate();
em.clear();
clearCache("fieldaccess");
// getEntityManager().refresh(cus);
cus = em.find(Customer.class, cusIDs[0]);
returnedName1 = cus.getName();
// tests bug 4288845
Query query2 = em.createQuery("UPDATE FieldAccessCustomer customer SET customer.name = :name WHERE customer.customerId = " + cusIDs[0]);
query2.setParameter("name", nameChange2);
query2.executeUpdate();
em.clear();
clearCache("fieldaccess");
// getEntityManager().refresh(cus);
cus = em.find(Customer.class, cusIDs[0]);
returnedName2 = cus.getName();
// tests bug 4293920
Query query3 = em.createQuery("UPDATE FieldAccessCustomer customer SET customer.name = :name WHERE customer.customerId = :id");
query3.setParameter("name", nameChange3);
query3.setParameter("id", cusIDs[0]);
query3.executeUpdate();
em.clear();
clearCache("fieldaccess");
// getEntityManager().refresh(cus);
cus = em.find(Customer.class, cusIDs[0]);
returnedName3 = cus.getName();
// tests bug 4294241
try {
Query query4 = em.createNamedQuery("findAllCustomersFieldAccess");
query4.executeUpdate();
} catch (IllegalStateException expected) {
expectedException = expected;
}
expectedException2 = null;
try {
commitTransaction(em);
expectedException2 = null;
} catch (Exception ex) {
expectedException2 = ex;
}
if ((returnedName1 == null || !returnedName1.equals(nameChange1))) {
fail("Customer name did not get updated correctly should be:" + nameChange1 + " is :" + returnedName1);
}
if ((returnedName2 == null || !returnedName2.equals(nameChange2))) {
fail("Customer name did not get updated correctly should be:" + nameChange2 + " is :" + returnedName2);
}
if ((returnedName3 == null || !returnedName3.equals(nameChange3))) {
fail("Customer name did not get updated correctly should be:" + nameChange3 + " is :" + returnedName3);
}
if (expectedException == null) {
fail("excuteUpdate did not result in an exception on findAllCustomersFieldAccess named ReadAllQuery");
}
if (expectedException2 == null) {
fail("commit did not throw expected RollbackException");
}
beginTransaction(em);
Customer cus1 = em.find(Customer.class, cusIDs[0]);
em.remove(cus1);
commitTransaction(em);
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer in project eclipselink by eclipse-ee4j.
the class RelationshipModelJUnitTestSuite method testPersistCustomer.
// Test that persisting a customer works correctly.
public void testPersistCustomer() {
Customer customer = RelationshipsExamples.customerExample4();
Item item1 = RelationshipsExamples.itemExample1();
Order order1 = RelationshipsExamples.orderExample1();
customer.addOrder(order1);
order1.setItem(item1);
Item item2 = RelationshipsExamples.itemExample2();
Order order2 = RelationshipsExamples.orderExample2();
customer.addOrder(order2);
order2.setItem(item2);
Item item3 = RelationshipsExamples.itemExample3();
Order order3 = RelationshipsExamples.orderExample3();
customer.addOrder(order3);
order3.setItem(item3);
EntityManager em = createEntityManager("fieldaccess");
try {
beginTransaction(em);
em.persist(customer);
commitTransaction(em);
beginTransaction(em);
verifyObjectInEntityManager(customer, "fieldaccess");
verifyObjectInEntityManager(order1, "fieldaccess");
verifyObjectInEntityManager(order2, "fieldaccess");
verifyObjectInEntityManager(item1, "fieldaccess");
verifyObjectInEntityManager(item2, "fieldaccess");
commitTransaction(em);
clearCache("fieldaccess");
beginTransaction(em);
verifyObjectInEntityManager(customer, "fieldaccess");
verifyObjectInEntityManager(order1, "fieldaccess");
verifyObjectInEntityManager(order2, "fieldaccess");
verifyObjectInEntityManager(item1, "fieldaccess");
verifyObjectInEntityManager(item2, "fieldaccess");
commitTransaction(em);
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
use of org.eclipse.persistence.testing.models.jpa.fieldaccess.relationships.Customer in project eclipselink by eclipse-ee4j.
the class RelationshipModelJUnitTestSuite method testGetResultCollectionTest.
/*
* Tests using the 'getResultCollection' api on a Query object obtained from the
* EntityManager Also tests bugs 4300879 - check non Collection container
* policy error and 4297903 - check ReadObjectQuery fails
*/
public void testGetResultCollectionTest() {
Collection returnedCustomers1, returnedCustomers2;
QueryException expectedException1 = null;
String ejbql1 = "SELECT OBJECT(thecust) FROM FieldAccessCustomer thecust WHERE thecust.customerId = :id";
Integer[] cusIDs = new Integer[3];
Customer cusClone1 = RelationshipsExamples.customerExample1();
Customer cusClone2 = RelationshipsExamples.customerExample2();
EntityManager em = createEntityManager("fieldaccess");
beginTransaction(em);
em.persist(cusClone1);
em.persist(cusClone2);
commitTransaction(em);
em.clear();
clearCache("fieldaccess");
cusIDs[0] = cusClone1.getCustomerId();
cusIDs[1] = cusClone2.getCustomerId();
try {
beginTransaction(em);
EntityManagerImpl entityManagerImpl = (EntityManagerImpl) em.getDelegate();
EJBQueryImpl query1 = (EJBQueryImpl) entityManagerImpl.createNamedQuery("findAllCustomersFieldAccess");
returnedCustomers1 = query1.getResultCollection();
EJBQueryImpl query2 = (EJBQueryImpl) entityManagerImpl.createQuery(ejbql1);
query2.setParameter("id", -10);
returnedCustomers2 = query2.getResultCollection();
// bug:4297903, check container policy failure
EJBQueryImpl query3 = (EJBQueryImpl) entityManagerImpl.createQuery(ejbql1);
ReadAllQuery readAllQuery = new ReadAllQuery(Customer.class);
MapContainerPolicy mapContainerPolicy = new MapContainerPolicy();
mapContainerPolicy.setContainerClass(HashMap.class);
mapContainerPolicy.setKeyName("hashCode");
readAllQuery.setContainerPolicy(mapContainerPolicy);
query3.setDatabaseQuery(readAllQuery);
try {
query3.getResultCollection();
} catch (PersistenceException exc) {
// QueryException.INVALID_CONTAINER_CLASS
expectedException1 = (QueryException) exc.getCause();
rollbackTransaction(em);
beginTransaction(em);
}
entityManagerImpl = (EntityManagerImpl) em.getDelegate();
// bug:4300879, check ReadObjectQuery fails
EJBQueryImpl query4 = (EJBQueryImpl) entityManagerImpl.createQuery(ejbql1);
query4.setParameter("id", -10);
ReadObjectQuery readObjectQuery2 = new ReadObjectQuery(Customer.class);
readObjectQuery2.setEJBQLString(ejbql1);
query4.setDatabaseQuery(readObjectQuery2);
query4.getResultCollection();
commitTransaction(em);
if (returnedCustomers1 == null || (returnedCustomers1.size() < 2)) {
fail("Not all customers were returned from findAllCustomers query ");
}
if (returnedCustomers2 == null || (returnedCustomers2.size() != 0)) {
fail("Customer from ReadObjectQuery was not returned using getResultCollection");
}
if (expectedException1 == null || (expectedException1.getErrorCode() != QueryException.INVALID_CONTAINER_CLASS)) {
fail("getResultCollection on query returning a hashtable did not throw expected INVALID_CONTAINER_CLASS QueryException");
}
beginTransaction(em);
Customer cus1 = em.find(Customer.class, cusIDs[0]);
em.remove(cus1);
Customer cus2 = em.find(Customer.class, cusIDs[1]);
em.remove(cus2);
commitTransaction(em);
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
}
}
Aggregations