package vu.org.ombudsman; import java.util.List; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Restrictions; import org.hibernate.validator.InvalidStateException; import org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException; import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests; import org.trails.persistence.HibernatePersistenceService; import org.trails.validation.HibernateClassValidatorFactory; import org.trails.i18n.SpringMessageSource; import org.trails.i18n.TestLocaleHolder; /** * Class for Complaint tests.

This class extends AbstractTransactionalDataSourceSpringContextTests, one of * the valuable test superclasses provided in the org.springframework.test package. This represents best practice for * integration tests with Spring. The AbstractTransactionalDataSourceSpringContextTests superclass provides the * following services:

  • Injects test dependencies, meaning that we don't need to perform application * context lookups. Injection uses autowiring by type.
  • Executes each test method in its own transaction, * which is automatically rolled back by default. This means that even if tests insert or otherwise change database * state, there is no need for a teardown or cleanup script.
  • Provides useful inherited protected fields, such as * a JdbcTemplate that can be used to verify database state after test operations, or verify the results of queries * performed by application code. An ApplicationContext is also inherited, and can be used for explicit lookup if * necessary.
  • The AbstractTransactionalDataSourceSpringContextTests and related classes are shipped in the * spring-mock.jar.

    *

    * http://www.theserverside.com/tt/articles/article.tss?l=PersistentDomain http://static.springframework.org/spring/docs/2.0.x/reference/testing.html * * @see org.springframework.samples.petclinic.AbstractClinicTests * @see org.springframework.test.AbstractTransactionalDataSourceSpringContextTests */ public class ComplaintTest extends AbstractTransactionalDataSourceSpringContextTests { protected void onSetUp() throws Exception { TestLocaleHolder testLocaleHolder = new TestLocaleHolder(); HibernateClassValidatorFactory hibernateClassValidatorFactory = ((HibernateClassValidatorFactory) applicationContext.getBean("hibernateClassValidatorFactory")); SpringMessageSource springMessageSource = ((SpringMessageSource) applicationContext.getBean("trailsMessageSource")); hibernateClassValidatorFactory.setLocaleHolder(testLocaleHolder); springMessageSource.setLocaleHolder(testLocaleHolder); } @Override protected String[] getConfigLocations() { return new String[]{"applicationContext.xml"}; } HibernatePersistenceService persistenceService; public void setPersistenceService(HibernatePersistenceService persistenceService) { this.persistenceService = persistenceService; } public void testSearchByDetachedCriteria() { Complaint firstComplaint = new Complaint(); firstComplaint.setComplainantName("first one"); firstComplaint = persistenceService.save(firstComplaint); Complaint secondComplaint = new Complaint(); secondComplaint.setComplainantName("second one"); secondComplaint = persistenceService.save(secondComplaint); DetachedCriteria criteria = DetachedCriteria.forClass(Complaint.class); criteria.add(Restrictions.like("id", new Long(1).longValue())); List objectList = (List) persistenceService.getInstances(Complaint.class, criteria); assertFalse(objectList.isEmpty()); int i = objectList.indexOf(firstComplaint); assertTrue(i >= 0); int j = objectList.indexOf(secondComplaint); assertTrue(j == -1); Complaint myReturnedComplaint; myReturnedComplaint = objectList.get(i); assertEquals(firstComplaint, myReturnedComplaint); assertEquals(firstComplaint.getComplainantName(), myReturnedComplaint.getComplainantName()); } }