mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2026-01-14 13:51:12 +00:00
moved the ClinicRepository into some separate repos
There is still some polish up to be done.
This commit is contained in:
parent
16b1476c40
commit
c9c8c4e085
47 changed files with 1666 additions and 728 deletions
|
|
@ -0,0 +1,147 @@
|
|||
package org.springframework.samples.petclinic;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||
import org.springframework.samples.petclinic.util.EntityUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Base class for {@link OwnerRepository} integration tests.
|
||||
* </p>
|
||||
* <p>
|
||||
* "AbstractClinicTests-context.xml" declares a common
|
||||
* {@link javax.sql.DataSource DataSource}. Subclasses should specify
|
||||
* additional context locations which declare a
|
||||
* {@link org.springframework.transaction.PlatformTransactionManager PlatformTransactionManager}
|
||||
* and a concrete implementation of {@link OwnerRepository}.
|
||||
* </p>
|
||||
* <p>
|
||||
* This class extends {@link AbstractTransactionalJUnit4SpringContextTests},
|
||||
* one of the valuable testing support classes provided by the
|
||||
* <em>Spring TestContext Framework</em> found in the
|
||||
* <code>org.springframework.test.context</code> package. The
|
||||
* annotation-driven configuration used here represents best practice for
|
||||
* integration tests with Spring. Note, however, that
|
||||
* AbstractTransactionalJUnit4SpringContextTests serves only as a convenience
|
||||
* for extension. For example, if you do not wish for your test classes to be
|
||||
* tied to a Spring-specific class hierarchy, you may configure your tests with
|
||||
* annotations such as {@link ContextConfiguration @ContextConfiguration},
|
||||
* {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners},
|
||||
* {@link org.springframework.transaction.annotation.Transactional @Transactional},
|
||||
* etc.
|
||||
* </p>
|
||||
* <p>
|
||||
* AbstractClinicTests and its subclasses benefit from the following services
|
||||
* provided by the Spring TestContext Framework:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li><strong>Spring IoC container caching</strong> which spares us
|
||||
* unnecessary set up time between test execution.</li>
|
||||
* <li><strong>Dependency Injection</strong> of test fixture instances,
|
||||
* meaning that we don't need to perform application context lookups. See the
|
||||
* use of {@link Autowired @Autowired} on the <code>ownerRepository</code> instance
|
||||
* variable, which uses autowiring <em>by type</em>. As an alternative, we
|
||||
* could annotate <code>ownerRepository</code> with
|
||||
* {@link javax.annotation.Resource @Resource} to achieve dependency injection
|
||||
* <em>by name</em>.
|
||||
* <em>(see: {@link ContextConfiguration @ContextConfiguration},
|
||||
* {@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener DependencyInjectionTestExecutionListener})</em></li>
|
||||
* <li><strong>Transaction management</strong>, meaning each test method is
|
||||
* executed in its own transaction, which is automatically rolled back by
|
||||
* default. Thus, even if tests insert or otherwise change database state, there
|
||||
* is no need for a teardown or cleanup script.
|
||||
* <em>(see: {@link org.springframework.test.context.transaction.TransactionConfiguration @TransactionConfiguration},
|
||||
* {@link org.springframework.transaction.annotation.Transactional @Transactional},
|
||||
* {@link org.springframework.test.context.transaction.TransactionalTestExecutionListener TransactionalTestExecutionListener})</em></li>
|
||||
* <li><strong>Useful inherited protected fields</strong>, such as a
|
||||
* {@link org.springframework.jdbc.core.simple.SimpleJdbcTemplate SimpleJdbcTemplate}
|
||||
* that can be used to verify database state after test operations or to verify
|
||||
* the results of queries performed by application code. An
|
||||
* {@link org.springframework.context.ApplicationContext ApplicationContext} is
|
||||
* also inherited and can be used for explicit bean lookup if necessary.
|
||||
* <em>(see: {@link org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests AbstractJUnit4SpringContextTests},
|
||||
* {@link AbstractTransactionalJUnit4SpringContextTests})</em></li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* The Spring TestContext Framework and related unit and integration testing
|
||||
* support classes are shipped in <code>spring-test.jar</code>.
|
||||
* </p>
|
||||
*
|
||||
* @author Ken Krebs
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public abstract class AbstractOwnerRepositoryTests {
|
||||
|
||||
@Autowired
|
||||
protected OwnerRepository ownerRepository;
|
||||
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void findOwners() {
|
||||
Collection<Owner> owners = this.ownerRepository.findByLastName("Davis");
|
||||
assertEquals(2, owners.size());
|
||||
owners = this.ownerRepository.findByLastName("Daviss");
|
||||
assertEquals(0, owners.size());
|
||||
}
|
||||
|
||||
@Test @Transactional
|
||||
public void findOwner() {
|
||||
Owner o1 = this.ownerRepository.findById(1);
|
||||
assertTrue(o1.getLastName().startsWith("Franklin"));
|
||||
Owner o10 = this.ownerRepository.findById(10);
|
||||
assertEquals("Carlos", o10.getFirstName());
|
||||
|
||||
// XXX: Add programmatic support for ending transactions with the
|
||||
// TestContext Framework.
|
||||
|
||||
// Check lazy loading, by ending the transaction:
|
||||
// endTransaction();
|
||||
|
||||
// Now Owners are "disconnected" from the data store.
|
||||
// We might need to touch this collection if we switched to lazy loading
|
||||
// in mapping files, but this test would pick this up.
|
||||
o1.getPets();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertOwner() {
|
||||
Collection<Owner> owners = this.ownerRepository.findByLastName("Schultz");
|
||||
int found = owners.size();
|
||||
Owner owner = new Owner();
|
||||
owner.setFirstName("Sam");
|
||||
owner.setLastName("Schultz");
|
||||
owner.setAddress("4, Evans Street");
|
||||
owner.setCity("Wollongong");
|
||||
owner.setTelephone("4444444444");
|
||||
this.ownerRepository.save(owner);
|
||||
owners = this.ownerRepository.findByLastName("Schultz");
|
||||
assertEquals("Verifying number of owners after inserting a new one.", found + 1, owners.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateOwner() throws Exception {
|
||||
Owner o1 = this.ownerRepository.findById(1);
|
||||
String old = o1.getLastName();
|
||||
o1.setLastName(old + "X");
|
||||
this.ownerRepository.save(o1);
|
||||
o1 = this.ownerRepository.findById(1);
|
||||
assertEquals(old + "X", o1.getLastName());
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -8,6 +8,9 @@ import static org.junit.Assert.assertTrue;
|
|||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
import org.springframework.samples.petclinic.service.ClinicService;
|
||||
import org.springframework.samples.petclinic.util.EntityUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
|
||||
|
|
@ -15,14 +18,14 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
|
||||
/**
|
||||
* <p>
|
||||
* Base class for {@link Clinic} integration tests.
|
||||
* Base class for {@link ClinicService} integration tests.
|
||||
* </p>
|
||||
* <p>
|
||||
* "AbstractClinicTests-context.xml" declares a common
|
||||
* {@link javax.sql.DataSource DataSource}. Subclasses should specify
|
||||
* additional context locations which declare a
|
||||
* {@link org.springframework.transaction.PlatformTransactionManager PlatformTransactionManager}
|
||||
* and a concrete implementation of {@link Clinic}.
|
||||
* and a concrete implementation of {@link ClinicService}.
|
||||
* </p>
|
||||
* <p>
|
||||
* This class extends {@link AbstractTransactionalJUnit4SpringContextTests},
|
||||
|
|
@ -48,9 +51,9 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
* unnecessary set up time between test execution.</li>
|
||||
* <li><strong>Dependency Injection</strong> of test fixture instances,
|
||||
* meaning that we don't need to perform application context lookups. See the
|
||||
* use of {@link Autowired @Autowired} on the <code>clinic</code> instance
|
||||
* use of {@link Autowired @Autowired} on the <code>petRepository</code> instance
|
||||
* variable, which uses autowiring <em>by type</em>. As an alternative, we
|
||||
* could annotate <code>clinic</code> with
|
||||
* could annotate <code>petRepository</code> with
|
||||
* {@link javax.annotation.Resource @Resource} to achieve dependency injection
|
||||
* <em>by name</em>.
|
||||
* <em>(see: {@link ContextConfiguration @ContextConfiguration},
|
||||
|
|
@ -81,30 +84,18 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public abstract class AbstractClinicTests {
|
||||
public abstract class AbstractPetRepositoryTests {
|
||||
|
||||
@Autowired
|
||||
protected Clinic clinic;
|
||||
protected PetRepository petRepository;
|
||||
|
||||
@Autowired
|
||||
protected OwnerRepository ownerRepository;
|
||||
|
||||
|
||||
@Test @Transactional
|
||||
public void getVets() {
|
||||
Collection<Vet> vets = this.clinic.getVets();
|
||||
|
||||
Vet v1 = EntityUtils.getById(vets, Vet.class, 2);
|
||||
assertEquals("Leary", v1.getLastName());
|
||||
assertEquals(1, v1.getNrOfSpecialties());
|
||||
assertEquals("radiology", (v1.getSpecialties().get(0)).getName());
|
||||
Vet v2 = EntityUtils.getById(vets, Vet.class, 3);
|
||||
assertEquals("Douglas", v2.getLastName());
|
||||
assertEquals(2, v2.getNrOfSpecialties());
|
||||
assertEquals("dentistry", (v2.getSpecialties().get(0)).getName());
|
||||
assertEquals("surgery", (v2.getSpecialties().get(1)).getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPetTypes() {
|
||||
Collection<PetType> petTypes = this.clinic.getPetTypes();
|
||||
Collection<PetType> petTypes = this.petRepository.getPetTypes();
|
||||
|
||||
PetType t1 = EntityUtils.getById(petTypes, PetType.class, 1);
|
||||
assertEquals("cat", t1.getName());
|
||||
|
|
@ -112,63 +103,14 @@ public abstract class AbstractClinicTests {
|
|||
assertEquals("snake", t4.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findOwners() {
|
||||
Collection<Owner> owners = this.clinic.findOwners("Davis");
|
||||
assertEquals(2, owners.size());
|
||||
owners = this.clinic.findOwners("Daviss");
|
||||
assertEquals(0, owners.size());
|
||||
}
|
||||
|
||||
@Test @Transactional
|
||||
public void findOwner() {
|
||||
Owner o1 = this.clinic.findOwner(1);
|
||||
assertTrue(o1.getLastName().startsWith("Franklin"));
|
||||
Owner o10 = this.clinic.findOwner(10);
|
||||
assertEquals("Carlos", o10.getFirstName());
|
||||
|
||||
// XXX: Add programmatic support for ending transactions with the
|
||||
// TestContext Framework.
|
||||
|
||||
// Check lazy loading, by ending the transaction:
|
||||
// endTransaction();
|
||||
|
||||
// Now Owners are "disconnected" from the data store.
|
||||
// We might need to touch this collection if we switched to lazy loading
|
||||
// in mapping files, but this test would pick this up.
|
||||
o1.getPets();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertOwner() {
|
||||
Collection<Owner> owners = this.clinic.findOwners("Schultz");
|
||||
int found = owners.size();
|
||||
Owner owner = new Owner();
|
||||
owner.setLastName("Schultz");
|
||||
this.clinic.storeOwner(owner);
|
||||
// assertTrue(!owner.isNew()); -- NOT TRUE FOR TOPLINK (before commit)
|
||||
owners = this.clinic.findOwners("Schultz");
|
||||
assertEquals("Verifying number of owners after inserting a new one.", found + 1, owners.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateOwner() throws Exception {
|
||||
Owner o1 = this.clinic.findOwner(1);
|
||||
String old = o1.getLastName();
|
||||
o1.setLastName(old + "X");
|
||||
this.clinic.storeOwner(o1);
|
||||
o1 = this.clinic.findOwner(1);
|
||||
assertEquals(old + "X", o1.getLastName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findPet() {
|
||||
Collection<PetType> types = this.clinic.getPetTypes();
|
||||
Pet p7 = this.clinic.findPet(7);
|
||||
Collection<PetType> types = this.petRepository.getPetTypes();
|
||||
Pet p7 = this.petRepository.findById(7);
|
||||
assertTrue(p7.getName().startsWith("Samantha"));
|
||||
assertEquals(EntityUtils.getById(types, PetType.class, 1).getId(), p7.getType().getId());
|
||||
assertEquals("Jean", p7.getOwner().getFirstName());
|
||||
Pet p6 = this.clinic.findPet(6);
|
||||
Pet p6 = this.petRepository.findById(6);
|
||||
assertEquals("George", p6.getName());
|
||||
assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), p6.getType().getId());
|
||||
assertEquals("Peter", p6.getOwner().getFirstName());
|
||||
|
|
@ -176,46 +118,30 @@ public abstract class AbstractClinicTests {
|
|||
|
||||
@Test @Transactional
|
||||
public void insertPet() {
|
||||
Owner o6 = this.clinic.findOwner(6);
|
||||
Owner o6 = this.ownerRepository.findById(6);
|
||||
int found = o6.getPets().size();
|
||||
Pet pet = new Pet();
|
||||
pet.setName("bowser");
|
||||
Collection<PetType> types = this.clinic.getPetTypes();
|
||||
Collection<PetType> types = this.petRepository.getPetTypes();
|
||||
pet.setType(EntityUtils.getById(types, PetType.class, 2));
|
||||
pet.setBirthDate(new Date());
|
||||
o6.addPet(pet);
|
||||
assertEquals(found + 1, o6.getPets().size());
|
||||
// both storePet and storeOwner are necessary to cover all ORM tools
|
||||
this.clinic.storePet(pet);
|
||||
this.clinic.storeOwner(o6);
|
||||
// assertTrue(!pet.isNew()); -- NOT TRUE FOR TOPLINK (before commit)
|
||||
o6 = this.clinic.findOwner(6);
|
||||
this.petRepository.storePet(pet);
|
||||
this.ownerRepository.save(o6);
|
||||
o6 = this.ownerRepository.findById(6);
|
||||
assertEquals(found + 1, o6.getPets().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updatePet() throws Exception {
|
||||
Pet p7 = this.clinic.findPet(7);
|
||||
Pet p7 = this.petRepository.findById(7);
|
||||
String old = p7.getName();
|
||||
p7.setName(old + "X");
|
||||
this.clinic.storePet(p7);
|
||||
p7 = this.clinic.findPet(7);
|
||||
this.petRepository.storePet(p7);
|
||||
p7 = this.petRepository.findById(7);
|
||||
assertEquals(old + "X", p7.getName());
|
||||
}
|
||||
|
||||
@Test @Transactional
|
||||
public void insertVisit() {
|
||||
Pet p7 = this.clinic.findPet(7);
|
||||
int found = p7.getVisits().size();
|
||||
Visit visit = new Visit();
|
||||
p7.addVisit(visit);
|
||||
visit.setDescription("test");
|
||||
// both storeVisit and storePet are necessary to cover all ORM tools
|
||||
this.clinic.storeVisit(visit);
|
||||
this.clinic.storePet(p7);
|
||||
// assertTrue(!visit.isNew()); -- NOT TRUE FOR TOPLINK (before commit)
|
||||
p7 = this.clinic.findPet(7);
|
||||
assertEquals(found + 1, p7.getVisits().size());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
package org.springframework.samples.petclinic;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.samples.petclinic.repository.VetRepository;
|
||||
import org.springframework.samples.petclinic.service.ClinicService;
|
||||
import org.springframework.samples.petclinic.util.EntityUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Base class for {@link ClinicService} integration tests.
|
||||
* </p>
|
||||
* <p>
|
||||
* "AbstractClinicTests-context.xml" declares a common
|
||||
* {@link javax.sql.DataSource DataSource}. Subclasses should specify
|
||||
* additional context locations which declare a
|
||||
* {@link org.springframework.transaction.PlatformTransactionManager PlatformTransactionManager}
|
||||
* and a concrete implementation of {@link ClinicService}.
|
||||
* </p>
|
||||
* <p>
|
||||
* This class extends {@link AbstractTransactionalJUnit4SpringContextTests},
|
||||
* one of the valuable testing support classes provided by the
|
||||
* <em>Spring TestContext Framework</em> found in the
|
||||
* <code>org.springframework.test.context</code> package. The
|
||||
* annotation-driven configuration used here represents best practice for
|
||||
* integration tests with Spring. Note, however, that
|
||||
* AbstractTransactionalJUnit4SpringContextTests serves only as a convenience
|
||||
* for extension. For example, if you do not wish for your test classes to be
|
||||
* tied to a Spring-specific class hierarchy, you may configure your tests with
|
||||
* annotations such as {@link ContextConfiguration @ContextConfiguration},
|
||||
* {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners},
|
||||
* {@link org.springframework.transaction.annotation.Transactional @Transactional},
|
||||
* etc.
|
||||
* </p>
|
||||
* <p>
|
||||
* AbstractClinicTests and its subclasses benefit from the following services
|
||||
* provided by the Spring TestContext Framework:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li><strong>Spring IoC container caching</strong> which spares us
|
||||
* unnecessary set up time between test execution.</li>
|
||||
* <li><strong>Dependency Injection</strong> of test fixture instances,
|
||||
* meaning that we don't need to perform application context lookups. See the
|
||||
* use of {@link Autowired @Autowired} on the <code>petRepository</code> instance
|
||||
* variable, which uses autowiring <em>by type</em>. As an alternative, we
|
||||
* could annotate <code>petRepository</code> with
|
||||
* {@link javax.annotation.Resource @Resource} to achieve dependency injection
|
||||
* <em>by name</em>.
|
||||
* <em>(see: {@link ContextConfiguration @ContextConfiguration},
|
||||
* {@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener DependencyInjectionTestExecutionListener})</em></li>
|
||||
* <li><strong>Transaction management</strong>, meaning each test method is
|
||||
* executed in its own transaction, which is automatically rolled back by
|
||||
* default. Thus, even if tests insert or otherwise change database state, there
|
||||
* is no need for a teardown or cleanup script.
|
||||
* <em>(see: {@link org.springframework.test.context.transaction.TransactionConfiguration @TransactionConfiguration},
|
||||
* {@link org.springframework.transaction.annotation.Transactional @Transactional},
|
||||
* {@link org.springframework.test.context.transaction.TransactionalTestExecutionListener TransactionalTestExecutionListener})</em></li>
|
||||
* <li><strong>Useful inherited protected fields</strong>, such as a
|
||||
* {@link org.springframework.jdbc.core.simple.SimpleJdbcTemplate SimpleJdbcTemplate}
|
||||
* that can be used to verify database state after test operations or to verify
|
||||
* the results of queries performed by application code. An
|
||||
* {@link org.springframework.context.ApplicationContext ApplicationContext} is
|
||||
* also inherited and can be used for explicit bean lookup if necessary.
|
||||
* <em>(see: {@link org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests AbstractJUnit4SpringContextTests},
|
||||
* {@link AbstractTransactionalJUnit4SpringContextTests})</em></li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* The Spring TestContext Framework and related unit and integration testing
|
||||
* support classes are shipped in <code>spring-test.jar</code>.
|
||||
* </p>
|
||||
*
|
||||
* @author Ken Krebs
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public abstract class AbstractVetRepositoryTests {
|
||||
|
||||
@Autowired
|
||||
protected VetRepository vetRepository;
|
||||
|
||||
|
||||
@Test @Transactional
|
||||
public void getVets() {
|
||||
Collection<Vet> vets = this.vetRepository.getVets();
|
||||
|
||||
Vet v1 = EntityUtils.getById(vets, Vet.class, 2);
|
||||
assertEquals("Leary", v1.getLastName());
|
||||
assertEquals(1, v1.getNrOfSpecialties());
|
||||
assertEquals("radiology", (v1.getSpecialties().get(0)).getName());
|
||||
Vet v2 = EntityUtils.getById(vets, Vet.class, 3);
|
||||
assertEquals("Douglas", v2.getLastName());
|
||||
assertEquals(2, v2.getNrOfSpecialties());
|
||||
assertEquals("dentistry", (v2.getSpecialties().get(0)).getName());
|
||||
assertEquals("surgery", (v2.getSpecialties().get(1)).getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,111 @@
|
|||
package org.springframework.samples.petclinic;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.samples.petclinic.repository.PetRepository;
|
||||
import org.springframework.samples.petclinic.repository.VisitRepository;
|
||||
import org.springframework.samples.petclinic.service.ClinicService;
|
||||
import org.springframework.samples.petclinic.util.EntityUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Base class for {@link ClinicService} integration tests.
|
||||
* </p>
|
||||
* <p>
|
||||
* "AbstractClinicTests-context.xml" declares a common
|
||||
* {@link javax.sql.DataSource DataSource}. Subclasses should specify
|
||||
* additional context locations which declare a
|
||||
* {@link org.springframework.transaction.PlatformTransactionManager PlatformTransactionManager}
|
||||
* and a concrete implementation of {@link ClinicService}.
|
||||
* </p>
|
||||
* <p>
|
||||
* This class extends {@link AbstractTransactionalJUnit4SpringContextTests},
|
||||
* one of the valuable testing support classes provided by the
|
||||
* <em>Spring TestContext Framework</em> found in the
|
||||
* <code>org.springframework.test.context</code> package. The
|
||||
* annotation-driven configuration used here represents best practice for
|
||||
* integration tests with Spring. Note, however, that
|
||||
* AbstractTransactionalJUnit4SpringContextTests serves only as a convenience
|
||||
* for extension. For example, if you do not wish for your test classes to be
|
||||
* tied to a Spring-specific class hierarchy, you may configure your tests with
|
||||
* annotations such as {@link ContextConfiguration @ContextConfiguration},
|
||||
* {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners},
|
||||
* {@link org.springframework.transaction.annotation.Transactional @Transactional},
|
||||
* etc.
|
||||
* </p>
|
||||
* <p>
|
||||
* AbstractClinicTests and its subclasses benefit from the following services
|
||||
* provided by the Spring TestContext Framework:
|
||||
* </p>
|
||||
* <ul>
|
||||
* <li><strong>Spring IoC container caching</strong> which spares us
|
||||
* unnecessary set up time between test execution.</li>
|
||||
* <li><strong>Dependency Injection</strong> of test fixture instances,
|
||||
* meaning that we don't need to perform application context lookups. See the
|
||||
* use of {@link Autowired @Autowired} on the <code>petRepository</code> instance
|
||||
* variable, which uses autowiring <em>by type</em>. As an alternative, we
|
||||
* could annotate <code>petRepository</code> with
|
||||
* {@link javax.annotation.Resource @Resource} to achieve dependency injection
|
||||
* <em>by name</em>.
|
||||
* <em>(see: {@link ContextConfiguration @ContextConfiguration},
|
||||
* {@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener DependencyInjectionTestExecutionListener})</em></li>
|
||||
* <li><strong>Transaction management</strong>, meaning each test method is
|
||||
* executed in its own transaction, which is automatically rolled back by
|
||||
* default. Thus, even if tests insert or otherwise change database state, there
|
||||
* is no need for a teardown or cleanup script.
|
||||
* <em>(see: {@link org.springframework.test.context.transaction.TransactionConfiguration @TransactionConfiguration},
|
||||
* {@link org.springframework.transaction.annotation.Transactional @Transactional},
|
||||
* {@link org.springframework.test.context.transaction.TransactionalTestExecutionListener TransactionalTestExecutionListener})</em></li>
|
||||
* <li><strong>Useful inherited protected fields</strong>, such as a
|
||||
* {@link org.springframework.jdbc.core.simple.SimpleJdbcTemplate SimpleJdbcTemplate}
|
||||
* that can be used to verify database state after test operations or to verify
|
||||
* the results of queries performed by application code. An
|
||||
* {@link org.springframework.context.ApplicationContext ApplicationContext} is
|
||||
* also inherited and can be used for explicit bean lookup if necessary.
|
||||
* <em>(see: {@link org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests AbstractJUnit4SpringContextTests},
|
||||
* {@link AbstractTransactionalJUnit4SpringContextTests})</em></li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* The Spring TestContext Framework and related unit and integration testing
|
||||
* support classes are shipped in <code>spring-test.jar</code>.
|
||||
* </p>
|
||||
*
|
||||
* @author Ken Krebs
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public abstract class AbstractVisitRepositoryTests {
|
||||
|
||||
@Autowired
|
||||
protected VisitRepository visitRepository;
|
||||
|
||||
@Autowired
|
||||
protected PetRepository petRepository;
|
||||
|
||||
|
||||
@Test @Transactional
|
||||
public void insertVisit() {
|
||||
Pet p7 = this.petRepository.findById(7);
|
||||
int found = p7.getVisits().size();
|
||||
Visit visit = new Visit();
|
||||
p7.addVisit(visit);
|
||||
visit.setDescription("test");
|
||||
// both storeVisit and storePet are necessary to cover all ORM tools
|
||||
this.visitRepository.storeVisit(visit);
|
||||
this.petRepository.storePet(p7);
|
||||
// assertTrue(!visit.isNew()); -- NOT TRUE FOR TOPLINK (before commit)
|
||||
p7 = this.petRepository.findById(7);
|
||||
assertEquals(found + 1, p7.getVisits().size());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,18 +1,17 @@
|
|||
package org.springframework.samples.petclinic.aspects;
|
||||
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.samples.petclinic.Clinic;
|
||||
import org.springframework.samples.petclinic.aspects.UsageLogAspect;
|
||||
import org.springframework.samples.petclinic.jpa.JpaClinicImplTests;
|
||||
import org.springframework.samples.petclinic.repository.OwnerRepository;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
|
@ -36,7 +35,7 @@ public class UsageLogAspectTests {
|
|||
private UsageLogAspect usageLogAspect;
|
||||
|
||||
@Autowired
|
||||
private Clinic clinic;
|
||||
private OwnerRepository ownerRepository;
|
||||
|
||||
|
||||
@Test
|
||||
|
|
@ -45,8 +44,8 @@ public class UsageLogAspectTests {
|
|||
String lastName2 = "Davis";
|
||||
String lastName3 = "foo";
|
||||
|
||||
assertFalse(this.clinic.findOwners(lastName1).isEmpty());
|
||||
assertFalse(this.clinic.findOwners(lastName2).isEmpty());
|
||||
assertFalse(this.ownerRepository.findByLastName(lastName1).isEmpty());
|
||||
assertFalse(this.ownerRepository.findByLastName(lastName2).isEmpty());
|
||||
|
||||
List<String> namesRequested = this.usageLogAspect.getNamesRequested();
|
||||
assertTrue(namesRequested.contains(lastName1));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
package org.springframework.samples.petclinic.jdbc;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.samples.petclinic.AbstractOwnerRepositoryTests;
|
||||
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Integration tests for the {@link JdbcClinicImpl} implementation.
|
||||
* </p>
|
||||
* <p>
|
||||
* </p>
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@DirtiesContext
|
||||
@ActiveProfiles("jdbc")
|
||||
public class JdbcOwnerRepositoryImplTests extends AbstractOwnerRepositoryTests {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,7 +1,8 @@
|
|||
package org.springframework.samples.petclinic.jdbc;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.samples.petclinic.AbstractClinicTests;
|
||||
import org.springframework.samples.petclinic.AbstractPetRepositoryTests;
|
||||
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
|
@ -21,7 +22,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@DirtiesContext
|
||||
@ActiveProfiles("jdbc")
|
||||
public class JdbcClinicImplTests extends AbstractClinicTests {
|
||||
public class JdbcPetRepositoryImplTests extends AbstractPetRepositoryTests {
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package org.springframework.samples.petclinic.jdbc;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.samples.petclinic.AbstractVetRepositoryTests;
|
||||
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Integration tests for the {@link JdbcClinicImpl} implementation.
|
||||
* </p>
|
||||
* <p>
|
||||
* </p>
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@DirtiesContext
|
||||
@ActiveProfiles("jdbc")
|
||||
public class JdbcVetRepositoryImplTests extends AbstractVetRepositoryTests {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package org.springframework.samples.petclinic.jdbc;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.samples.petclinic.AbstractVisitRepositoryTests;
|
||||
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Integration tests for the {@link JdbcClinicImpl} implementation.
|
||||
* </p>
|
||||
* <p>
|
||||
* </p>
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@DirtiesContext
|
||||
@ActiveProfiles("jdbc")
|
||||
public class JdbcVisitRepositoryImplTests extends AbstractVisitRepositoryTests {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,16 +1,8 @@
|
|||
|
||||
package org.springframework.samples.petclinic.jpa;
|
||||
|
||||
import static junit.framework.Assert.fail;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.samples.petclinic.AbstractClinicTests;
|
||||
import org.springframework.samples.petclinic.Clinic;
|
||||
import org.springframework.samples.petclinic.AbstractOwnerRepositoryTests;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
|
@ -37,23 +29,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ActiveProfiles({"jpa","plain-jpa"})
|
||||
public class JpaClinicImplTests extends AbstractClinicTests {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Autowired
|
||||
private Clinic clinic;
|
||||
|
||||
|
||||
@Test
|
||||
public void testBogusJpql() {
|
||||
try {
|
||||
this.entityManager.createQuery("SELECT RUBBISH FROM RUBBISH HEAP").executeUpdate();
|
||||
fail("exception was expected because of incorrect SQL statement");
|
||||
} catch (Exception e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
public class JpaOwnerRepositoryImplTests extends AbstractOwnerRepositoryTests {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package org.springframework.samples.petclinic.jpa;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.samples.petclinic.AbstractPetRepositoryTests;
|
||||
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Integration tests for the {@link JdbcClinicImpl} implementation.
|
||||
* </p>
|
||||
* <p>
|
||||
* </p>
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@DirtiesContext
|
||||
@ActiveProfiles({"jpa","plain-jpa"})
|
||||
public class JpaPetRepositoryImplTests extends AbstractPetRepositoryTests {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package org.springframework.samples.petclinic.jpa;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.samples.petclinic.AbstractVetRepositoryTests;
|
||||
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Integration tests for the {@link JdbcClinicImpl} implementation.
|
||||
* </p>
|
||||
* <p>
|
||||
* </p>
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@DirtiesContext
|
||||
@ActiveProfiles({"jpa","plain-jpa"})
|
||||
public class JpaVetRepositoryImplTests extends AbstractVetRepositoryTests {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package org.springframework.samples.petclinic.jpa;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.samples.petclinic.AbstractVisitRepositoryTests;
|
||||
import org.springframework.samples.petclinic.repository.jdbc.JdbcClinicImpl;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Integration tests for the {@link JdbcClinicImpl} implementation.
|
||||
* </p>
|
||||
* <p>
|
||||
* </p>
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
* @author Michael Isvy
|
||||
*/
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@DirtiesContext
|
||||
@ActiveProfiles({"jpa","plain-jpa"})
|
||||
public class JpaVisitRepositoryImplTests extends AbstractVisitRepositoryTests {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
package org.springframework.samples.petclinic.jpa;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.samples.petclinic.AbstractClinicTests;
|
||||
import org.springframework.samples.petclinic.AbstractOwnerRepositoryTests;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
|
@ -14,6 +14,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||
@ContextConfiguration(locations={"classpath:spring/applicationContext-dao.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ActiveProfiles({"jpa","spring-data-jpa"})
|
||||
public class SpringDataClinicTests extends AbstractClinicTests {
|
||||
public class SpringDataOwnerRepositoryTests extends AbstractOwnerRepositoryTests {
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue