package petstore.test; import java.sql.Time; import java.util.HashSet; import java.util.List; import java.util.Set; import airliftj.app.ComponentException; import airliftj.core.DemarcationBlock; import airliftj.core.DemarcationSettings; import airliftj.persistence.PersistenceException; import airliftj.persistence.TransactionManager; import airliftj.response.ResponseSummary; import airliftj.test.app.BaseAppLayerComponentUnitTest; import petstore.entity.*; import petstore.entity.cmd.*; /** * Tests Queries introduced for use in the . *
* Notes: * * * Based on pre-existing Query tester. * * @author milan zimmermann */ public class TestPetstorePerson extends BaseAppLayerComponentUnitTest { private String smith = "Smith"; private String miller = "Miller"; private String carpenter = "Carpenter"; public void testPersonQuery_LoadTestData() { ResponseSummary response = this.getConversation().executeDemarcationBlock(new DemarcationBlock() { /* (non-Javadoc) * @see airliftj.shared.DemarcationBlock#executeWithDemarcation() */ public void executeWithDemarcation() { // Time constructor is one less, so 7-3 is Time(6,0,0) length=7 insertPerson(smith, "smith@airliftj.org", "Tennesee", "Nashville"); insertPerson(miller, "miller@airliftj.org", "Alabama", "Birmingham"); insertPerson(carpenter, "carpenter@airliftj.org", "Ontario", "Oakville"); } // NOTE: automatic commit happens at the end of this block.. // or rollback if an exception was thrown inside this block // this assumes of course, that there was no transaction //already running when we got here. },DemarcationSettings.SETTINGS_TRANSACTION_REQUIRED); if (response.isErrorOrFatalLevel()) { fail("Failure while testing ServiceCaseActivity Query." + response.toString()); } } public void testPersonQuery() { try { // build the component SelectPersons query = (SelectPersons) this.getTransactionManager().getCommandByClass(SelectPersons.class); // 1) ########## These should yield 1 row query.clearParameters(); query.setFilterByLastName(this.smith); List resultList = (List)query.execute(); if (resultList.size() != 1) { fail("Filter by Person name = " + this.smith + " failed."); } // 2) ########## These should yield 0 rows System.out.println(" ##### " + SelectPersons.class.getName()+ " query test finished."); } catch (Exception e) { e.printStackTrace(); fail("Exception while testing ServiceCaseActivity Query." + e.getMessage()); } } // returns true if Person existed by name. private boolean deletePersonIfExists(String lastName) throws ComponentException, PersistenceException { TransactionManager transMgr = this.getTransactionManager(); // Lookup the person using a Person query that filters on CaseNumber. // We're going to need to build a query for SelectPersons which takes the CaseNumber. SelectPersons query = (SelectPersons)transMgr.getCommandByClass(SelectPersons.class); query.setFilterByLastName(lastName); Person foundCase = (Person) query.fetchSingleExpectedResult(); if (foundCase != null) { transMgr.delete(Person.class, foundCase.getId()); transMgr.flushChangesToStorage(); } return (foundCase != null); } private void insertPerson(String lastName, String email, String state, String city) { // delete any Person in database by name while(deletePersonIfExists(lastName)); Person person = (Person)getEntityManager().createEntityAndAssignIdentity(Person.class); person.setLastName(lastName); person.setEmailAddress(email); getTransactionManager().managePersistentEntity(person); // Create a single Address for this Person. Address address = (Address)getEntityManager().createEntityAndAssignIdentity(Address.class); address.setState(state); address.setCity(city); Set addresses = new HashSet(); addresses.add(address); person.setAddress(addresses); getTransactionManager().managePersistentEntity(address); } }