use of org.eclipse.persistence.testing.models.jpa.relationships.Item in project eclipselink by eclipse-ee4j.
the class RelationshipModelJUnitTestSuite method testGetSingleResultTest.
/*
* Tests using the 'getSingleResult' api on a Query object obtained from the
* EntityManager Tests fixes for bugs 4202835 and 4301674
*
* modified for changes in bug:4628215 (EntityNotFoundException)
* EntityNotFoundException changed to NoResultException as per new spec
*/
public void testGetSingleResultTest() {
// used for verification
Customer returnedCustomer1, returnedCustomer2 = null;
NonUniqueResultException expectedException1 = null;
NoResultException expectedException2 = null;
String searchString = "notAnItemName";
Integer[] cusIDs = new Integer[3];
Customer cusClone1 = RelationshipsExamples.customerExample1();
Customer cusClone2 = RelationshipsExamples.customerExample2();
EntityManager em = createEntityManager();
try {
beginTransaction(em);
em.persist(cusClone1);
em.persist(cusClone2);
commitTransaction(em);
clearCache();
cusIDs[0] = cusClone1.getCustomerId();
cusIDs[1] = cusClone2.getCustomerId();
beginTransaction(em);
try {
returnedCustomer1 = (Customer) em.createNamedQuery("findAllCustomers").getSingleResult();
} catch (NonUniqueResultException exceptionExpected1) {
expectedException1 = exceptionExpected1;
}
try {
// should be no Items to find, which should cause an
// NoResultException
Query query1 = em.createNamedQuery("findAllItemsByName");
Item item = (Item) query1.setParameter(1, searchString).getSingleResult();
item.toString();
} catch (NoResultException exceptionExpected2) {
expectedException2 = exceptionExpected2;
}
// bug 4301674 test
EJBQueryImpl query2 = (EJBQueryImpl) em.createNamedQuery("findAllCustomers");
ReadAllQuery readAllQuery = new ReadAllQuery(Customer.class);
MapContainerPolicy mapContainerPolicy = new MapContainerPolicy();
mapContainerPolicy.setContainerClass(HashMap.class);
mapContainerPolicy.setKeyName("hashCode");
readAllQuery.setContainerPolicy(mapContainerPolicy);
query2.setDatabaseQuery(readAllQuery);
Map result = (Map) query2.getSingleResult();
result.toString();
// check for single result found.
Query query3 = em.createQuery("SELECT OBJECT(thecust) FROM Customer thecust WHERE thecust.customerId = :id");
returnedCustomer1 = (Customer) query3.setParameter("id", cusIDs[0]).getSingleResult();
// check for single result using a ReadObjectQuery (tests previous
// fix for 4202835)
EJBQueryImpl query4 = (EJBQueryImpl) em.createQuery("SELECT OBJECT(thecust) FROM Customer thecust WHERE thecust.customerId = :id");
query4.setParameter("id", cusIDs[0]);
ReadObjectQuery readObjectQuery = new ReadObjectQuery(Customer.class);
readObjectQuery.setEJBQLString("SELECT OBJECT(thecust) FROM Customer thecust WHERE thecust.customerId = :id");
query4.setDatabaseQuery(readObjectQuery);
returnedCustomer2 = (Customer) query4.getSingleResult();
commitTransaction(em);
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);
if (expectedException1 == null) {
fail("getSingelResult on query returning multiple values did not throw a NonUniqueResultException");
}
if (expectedException2 == null) {
fail("getSingelResult on query returning multiple values did not throw an NoResultException");
}
if (returnedCustomer1 == null || (!returnedCustomer1.getCustomerId().equals(cusIDs[0]))) {
fail("Incorrect Single Customer returned, found: " + returnedCustomer1);
}
if (returnedCustomer2 == null || (!returnedCustomer2.getCustomerId().equals(cusIDs[0]))) {
fail("Incorrect Single Customer returned, found: " + returnedCustomer2);
}
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
}
}
use of org.eclipse.persistence.testing.models.jpa.relationships.Item in project eclipselink by eclipse-ee4j.
the class RelationshipModelJUnitTestSuite method testNamedQueryWithArgumentsTest.
// Bug#4646580 Query arguments are added in EJBQL
public void testNamedQueryWithArgumentsTest() {
Integer[] cusIDs = new Integer[3];
Integer[] orderIDs = new Integer[3];
Integer[] itemIDs = new Integer[3];
Exception exception = null;
List list = null;
Customer cusClone1 = RelationshipsExamples.customerExample1();
Item item1 = RelationshipsExamples.itemExample1();
Order order1 = RelationshipsExamples.orderExample1();
order1.setCustomer(cusClone1);
order1.setItem(item1);
EntityManager em = createEntityManager();
try {
beginTransaction(em);
em.persist(cusClone1);
em.persist(order1);
commitTransaction(em);
cusIDs[0] = cusClone1.getCustomerId();
orderIDs[0] = order1.getOrderId();
itemIDs[0] = item1.getItemId();
clearCache();
try {
ServerSession ss = getServerSession();
Vector vec = new Vector();
vec.add(itemIDs[0]);
list = (List) ss.executeQuery("findAllOrdersByItem", vec);
} catch (Exception ex) {
exception = ex;
}
beginTransaction(em);
Customer cus1 = em.find(Customer.class, cusIDs[0]);
em.remove(cus1);
Order ord1 = em.find(Order.class, orderIDs[0]);
em.remove(ord1);
Item it1 = em.find(Item.class, itemIDs[0]);
em.remove(it1);
commitTransaction(em);
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
}
if (exception != null) {
fail("An exception is thrown: " + exception);
}
if (list.size() != 1) {
fail("One order is expected but " + list.size() + " was returned");
}
}
use of org.eclipse.persistence.testing.models.jpa.relationships.Item in project eclipselink by eclipse-ee4j.
the class RelationshipModelJUnitTestSuite method testModifyItem.
/**
* Read an item, verify it contents, modify it and commit.
*/
public void testModifyItem() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
Item item = em.find(Item.class, itemId);
item.setName("Willy Waller");
item.setDescription("For adults only!");
assertTrue("The manufacturer was not persisted", item.getManufacturer() != null);
assertTrue("The manufacturer of the item was incorrect", item.getManufacturer().getName().equals("Mattel Inc."));
Lego lego = new Lego();
lego.setName("The LEGO Group");
item.setManufacturer(lego);
assertTrue("The distributor was not persisted", item.getDistributor() != null);
assertTrue("The distributor of the item was incorrect", item.getDistributor().getName().equals("Namco Games"));
MegaBrands megaBrands = new MegaBrands();
megaBrands.setName("MegaBrands Inc.");
item.setDistributor(megaBrands);
em.merge(item);
em.persist(lego);
em.persist(megaBrands);
commitTransaction(em);
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw e;
}
closeEntityManager(em);
}
use of org.eclipse.persistence.testing.models.jpa.relationships.Item in project eclipselink by eclipse-ee4j.
the class RelationshipModelJUnitTestSuite method testCreateItem.
/**
* Create a new item that has a variable one to one to a manufacturer.
*/
public void testCreateItem() {
EntityManager em = createEntityManager();
beginTransaction(em);
try {
Item item = new Item();
item.setName("Synergizer2000");
item.setDescription("Every kid must have one ... ");
// Manufacturer does not cascade persist
Mattel mattel = new Mattel();
mattel.setName("Mattel Inc.");
em.persist(mattel);
item.setManufacturer(mattel);
// Distributor will cascade persist
Namco namco = new Namco();
namco.setName("Namco Games");
item.setDistributor(namco);
em.persist(item);
itemId = item.getItemId();
commitTransaction(em);
} catch (RuntimeException e) {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
throw e;
}
closeEntityManager(em);
}
use of org.eclipse.persistence.testing.models.jpa.relationships.Item in project eclipselink by eclipse-ee4j.
the class RelationshipModelJUnitTestSuite method testVerifyItem.
/**
* Verify the final contents of item.
*/
public void testVerifyItem() {
EntityManager em = createEntityManager();
Item item = em.find(Item.class, itemId);
assertTrue("The manufacturer was not persisted", item.getManufacturer() != null);
assertTrue("The manufacturer of the item was incorrect", item.getManufacturer().getName().equals("The LEGO Group"));
assertTrue("The distributor was not persisted", item.getDistributor() != null);
assertTrue("The distributor of the item was incorrect", item.getDistributor().getName().equals("MegaBrands Inc."));
closeEntityManager(em);
}
Aggregations