mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2026-01-14 13:51:12 +00:00
SPR-6447
SPR-6448 + commit the gross of the files + added maven pom
This commit is contained in:
parent
9dd07f05f3
commit
521d01db95
117 changed files with 6945 additions and 10 deletions
|
|
@ -0,0 +1,224 @@
|
|||
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.util.EntityUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Base class for {@link Clinic} 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}.
|
||||
* </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>clinic</code> instance
|
||||
* variable, which uses autowiring <em>by type</em>. As an alternative, we
|
||||
* could annotate <code>clinic</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
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public abstract class AbstractClinicTests extends AbstractTransactionalJUnit4SpringContextTests {
|
||||
|
||||
@Autowired
|
||||
protected Clinic clinic;
|
||||
|
||||
|
||||
@Test
|
||||
public void getVets() {
|
||||
Collection<Vet> vets = this.clinic.getVets();
|
||||
// Use the inherited countRowsInTable() convenience method (from
|
||||
// AbstractTransactionalJUnit4SpringContextTests) to verify the results.
|
||||
assertEquals("JDBC query must show the same number of vets", super.countRowsInTable("VETS"), vets.size());
|
||||
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();
|
||||
assertEquals("JDBC query must show the same number of pet types", super.countRowsInTable("TYPES"),
|
||||
petTypes.size());
|
||||
PetType t1 = EntityUtils.getById(petTypes, PetType.class, 1);
|
||||
assertEquals("cat", t1.getName());
|
||||
PetType t4 = EntityUtils.getById(petTypes, PetType.class, 4);
|
||||
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
|
||||
public void loadOwner() {
|
||||
Owner o1 = this.clinic.loadOwner(1);
|
||||
assertTrue(o1.getLastName().startsWith("Franklin"));
|
||||
Owner o10 = this.clinic.loadOwner(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.loadOwner(1);
|
||||
String old = o1.getLastName();
|
||||
o1.setLastName(old + "X");
|
||||
this.clinic.storeOwner(o1);
|
||||
o1 = this.clinic.loadOwner(1);
|
||||
assertEquals(old + "X", o1.getLastName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadPet() {
|
||||
Collection<PetType> types = this.clinic.getPetTypes();
|
||||
Pet p7 = this.clinic.loadPet(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.loadPet(6);
|
||||
assertEquals("George", p6.getName());
|
||||
assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), p6.getType().getId());
|
||||
assertEquals("Peter", p6.getOwner().getFirstName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertPet() {
|
||||
Owner o6 = this.clinic.loadOwner(6);
|
||||
int found = o6.getPets().size();
|
||||
Pet pet = new Pet();
|
||||
pet.setName("bowser");
|
||||
Collection<PetType> types = this.clinic.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.loadOwner(6);
|
||||
assertEquals(found + 1, o6.getPets().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updatePet() throws Exception {
|
||||
Pet p7 = this.clinic.loadPet(7);
|
||||
String old = p7.getName();
|
||||
p7.setName(old + "X");
|
||||
this.clinic.storePet(p7);
|
||||
p7 = this.clinic.loadPet(7);
|
||||
assertEquals(old + "X", p7.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertVisit() {
|
||||
Pet p7 = this.clinic.loadPet(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.loadPet(7);
|
||||
assertEquals(found + 1, p7.getVisits().size());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package org.springframework.samples.petclinic;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* JUnit test for the {@link Owner} class.
|
||||
*
|
||||
* @author Ken Krebs
|
||||
*/
|
||||
public class OwnerTests {
|
||||
|
||||
@Test
|
||||
public void testHasPet() {
|
||||
Owner owner = new Owner();
|
||||
Pet fido = new Pet();
|
||||
fido.setName("Fido");
|
||||
assertNull(owner.getPet("Fido"));
|
||||
assertNull(owner.getPet("fido"));
|
||||
owner.addPet(fido);
|
||||
assertEquals(fido, owner.getPet("Fido"));
|
||||
assertEquals(fido, owner.getPet("fido"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package org.springframework.samples.petclinic;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.Suite.SuiteClasses;
|
||||
import org.springframework.samples.petclinic.hibernate.HibernateClinicTests;
|
||||
import org.springframework.samples.petclinic.jdbc.SimpleJdbcClinicTests;
|
||||
import org.springframework.samples.petclinic.jpa.EntityManagerClinicTests;
|
||||
import org.springframework.samples.petclinic.jpa.HibernateEntityManagerClinicTests;
|
||||
import org.springframework.samples.petclinic.jpa.OpenJpaEntityManagerClinicTests;
|
||||
import org.springframework.samples.petclinic.web.VisitsAtomViewTest;
|
||||
|
||||
/**
|
||||
* JUnit 4 based test suite for all PetClinic tests.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({
|
||||
OwnerTests.class,
|
||||
SimpleJdbcClinicTests.class,
|
||||
HibernateClinicTests.class,
|
||||
EntityManagerClinicTests.class,
|
||||
HibernateEntityManagerClinicTests.class,
|
||||
OpenJpaEntityManagerClinicTests.class,
|
||||
VisitsAtomViewTest.class
|
||||
})
|
||||
public class PetClinicTestSuite {
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package org.springframework.samples.petclinic.hibernate;
|
||||
|
||||
import org.springframework.samples.petclinic.AbstractClinicTests;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Integration tests for the {@link HibernateClinic} implementation.
|
||||
* </p>
|
||||
* <p>
|
||||
* "HibernateClinicTests-context.xml" determines the actual beans to test.
|
||||
* </p>
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class HibernateClinicTests extends AbstractClinicTests {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package org.springframework.samples.petclinic.jdbc;
|
||||
|
||||
import org.springframework.samples.petclinic.AbstractClinicTests;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Integration tests for the {@link SimpleJdbcClinic} implementation.
|
||||
* </p>
|
||||
* <p>
|
||||
* "SimpleJdbcClinicTests-context.xml" determines the actual beans to test.
|
||||
* </p>
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public class SimpleJdbcClinicTests extends AbstractClinicTests {
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,199 @@
|
|||
|
||||
package org.springframework.samples.petclinic.jpa;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.samples.petclinic.Clinic;
|
||||
import org.springframework.samples.petclinic.Owner;
|
||||
import org.springframework.samples.petclinic.Pet;
|
||||
import org.springframework.samples.petclinic.PetType;
|
||||
import org.springframework.samples.petclinic.Vet;
|
||||
import org.springframework.samples.petclinic.Visit;
|
||||
import org.springframework.samples.petclinic.util.EntityUtils;
|
||||
import org.springframework.test.annotation.ExpectedException;
|
||||
import org.springframework.test.jpa.AbstractJpaTests;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* This class extends {@link AbstractJpaTests}, one of the valuable test
|
||||
* superclasses provided in the <code>org.springframework.test</code> package.
|
||||
* This represents best practice for integration tests with Spring for JPA based
|
||||
* tests which require <em>shadow class loading</em>. For all other types of
|
||||
* integration testing, the <em>Spring TestContext Framework</em> is
|
||||
* preferred.
|
||||
* </p>
|
||||
* <p>
|
||||
* AbstractJpaTests and its superclasses provide the following services:
|
||||
* <ul>
|
||||
* <li>Injects test dependencies, meaning that we don't need to perform
|
||||
* application context lookups. See the setClinic() method. Injection uses
|
||||
* autowiring by type.</li>
|
||||
* <li>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.</li>
|
||||
* <li>Provides useful inherited protected fields, such as a
|
||||
* {@link SimpleJdbcTemplate} that can be used to verify database state after
|
||||
* test operations, or verify the results of queries performed by application
|
||||
* code. Alternatively, you can use protected convenience methods such as
|
||||
* {@link #countRowsInTable(String)}, {@link #deleteFromTables(String[])},
|
||||
* etc. An ApplicationContext is also inherited, and can be used for explicit
|
||||
* lookup if necessary.</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* {@link AbstractJpaTests} and related classes are shipped in
|
||||
* <code>spring-test.jar</code>.
|
||||
* </p>
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Sam Brannen
|
||||
* @see AbstractJpaTests
|
||||
*/
|
||||
public abstract class AbstractJpaClinicTests extends AbstractJpaTests {
|
||||
|
||||
protected Clinic clinic;
|
||||
|
||||
|
||||
/**
|
||||
* This method is provided to set the Clinic instance being tested by the
|
||||
* Dependency Injection injection behaviour of the superclass from the
|
||||
* <code>org.springframework.test</code> package.
|
||||
*
|
||||
* @param clinic clinic to test
|
||||
*/
|
||||
public void setClinic(Clinic clinic) {
|
||||
this.clinic = clinic;
|
||||
}
|
||||
|
||||
@ExpectedException(IllegalArgumentException.class)
|
||||
public void testBogusJpql() {
|
||||
this.sharedEntityManager.createQuery("SELECT RUBBISH FROM RUBBISH HEAP").executeUpdate();
|
||||
}
|
||||
|
||||
public void testApplicationManaged() {
|
||||
EntityManager appManaged = this.entityManagerFactory.createEntityManager();
|
||||
appManaged.joinTransaction();
|
||||
}
|
||||
|
||||
public void testGetVets() {
|
||||
Collection<Vet> vets = this.clinic.getVets();
|
||||
// Use the inherited countRowsInTable() convenience method (from
|
||||
// AbstractTransactionalDataSourceSpringContextTests) to verify the
|
||||
// results.
|
||||
assertEquals("JDBC query must show the same number of vets", super.countRowsInTable("VETS"), vets.size());
|
||||
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());
|
||||
}
|
||||
|
||||
public void testGetPetTypes() {
|
||||
Collection<PetType> petTypes = this.clinic.getPetTypes();
|
||||
assertEquals("JDBC query must show the same number of pet types", super.countRowsInTable("TYPES"),
|
||||
petTypes.size());
|
||||
PetType t1 = EntityUtils.getById(petTypes, PetType.class, 1);
|
||||
assertEquals("cat", t1.getName());
|
||||
PetType t4 = EntityUtils.getById(petTypes, PetType.class, 4);
|
||||
assertEquals("snake", t4.getName());
|
||||
}
|
||||
|
||||
public void testFindOwners() {
|
||||
Collection<Owner> owners = this.clinic.findOwners("Davis");
|
||||
assertEquals(2, owners.size());
|
||||
owners = this.clinic.findOwners("Daviss");
|
||||
assertEquals(0, owners.size());
|
||||
}
|
||||
|
||||
public void testLoadOwner() {
|
||||
Owner o1 = this.clinic.loadOwner(1);
|
||||
assertTrue(o1.getLastName().startsWith("Franklin"));
|
||||
Owner o10 = this.clinic.loadOwner(10);
|
||||
assertEquals("Carlos", o10.getFirstName());
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
public void testInsertOwner() {
|
||||
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(found + 1, owners.size());
|
||||
}
|
||||
|
||||
public void testUpdateOwner() throws Exception {
|
||||
Owner o1 = this.clinic.loadOwner(1);
|
||||
String old = o1.getLastName();
|
||||
o1.setLastName(old + "X");
|
||||
this.clinic.storeOwner(o1);
|
||||
o1 = this.clinic.loadOwner(1);
|
||||
assertEquals(old + "X", o1.getLastName());
|
||||
}
|
||||
|
||||
public void testLoadPet() {
|
||||
Collection<PetType> types = this.clinic.getPetTypes();
|
||||
Pet p7 = this.clinic.loadPet(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.loadPet(6);
|
||||
assertEquals("George", p6.getName());
|
||||
assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), p6.getType().getId());
|
||||
assertEquals("Peter", p6.getOwner().getFirstName());
|
||||
}
|
||||
|
||||
public void testInsertPet() {
|
||||
Owner o6 = this.clinic.loadOwner(6);
|
||||
int found = o6.getPets().size();
|
||||
Pet pet = new Pet();
|
||||
pet.setName("bowser");
|
||||
Collection<PetType> types = this.clinic.getPetTypes();
|
||||
pet.setType(EntityUtils.getById(types, PetType.class, 2));
|
||||
pet.setBirthDate(new Date());
|
||||
o6.addPet(pet);
|
||||
assertEquals(found + 1, o6.getPets().size());
|
||||
this.clinic.storeOwner(o6);
|
||||
// assertTrue(!pet.isNew()); -- NOT TRUE FOR TOPLINK (before commit)
|
||||
o6 = this.clinic.loadOwner(6);
|
||||
assertEquals(found + 1, o6.getPets().size());
|
||||
}
|
||||
|
||||
public void testUpdatePet() throws Exception {
|
||||
Pet p7 = this.clinic.loadPet(7);
|
||||
String old = p7.getName();
|
||||
p7.setName(old + "X");
|
||||
this.clinic.storePet(p7);
|
||||
p7 = this.clinic.loadPet(7);
|
||||
assertEquals(old + "X", p7.getName());
|
||||
}
|
||||
|
||||
public void testInsertVisit() {
|
||||
Pet p7 = this.clinic.loadPet(7);
|
||||
int found = p7.getVisits().size();
|
||||
Visit visit = new Visit();
|
||||
p7.addVisit(visit);
|
||||
visit.setDescription("test");
|
||||
this.clinic.storePet(p7);
|
||||
// assertTrue(!visit.isNew()); -- NOT TRUE FOR TOPLINK (before commit)
|
||||
p7 = this.clinic.loadPet(7);
|
||||
assertEquals(found + 1, p7.getVisits().size());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
package org.springframework.samples.petclinic.jpa;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.samples.petclinic.aspects.UsageLogAspect;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Tests for the DAO variant based on the shared EntityManager approach. Uses
|
||||
* TopLink Essentials (the reference implementation) for testing.
|
||||
* </p>
|
||||
* <p>
|
||||
* Specifically tests usage of an <code>orm.xml</code> file, loaded by the
|
||||
* persistence provider through the Spring-provided persistence unit root URL.
|
||||
* </p>
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class EntityManagerClinicTests extends AbstractJpaClinicTests {
|
||||
|
||||
private UsageLogAspect usageLogAspect;
|
||||
|
||||
public void setUsageLogAspect(UsageLogAspect usageLogAspect) {
|
||||
this.usageLogAspect = usageLogAspect;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getConfigPaths() {
|
||||
return new String[] {
|
||||
"applicationContext-jpaCommon.xml",
|
||||
"applicationContext-toplinkAdapter.xml",
|
||||
"applicationContext-entityManager.xml"
|
||||
};
|
||||
}
|
||||
|
||||
public void testUsageLogAspectIsInvoked() {
|
||||
String name1 = "Schuurman";
|
||||
String name2 = "Greenwood";
|
||||
String name3 = "Leau";
|
||||
|
||||
assertTrue(this.clinic.findOwners(name1).isEmpty());
|
||||
assertTrue(this.clinic.findOwners(name2).isEmpty());
|
||||
|
||||
List<String> namesRequested = this.usageLogAspect.getNamesRequested();
|
||||
assertTrue(namesRequested.contains(name1));
|
||||
assertTrue(namesRequested.contains(name2));
|
||||
assertFalse(namesRequested.contains(name3));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package org.springframework.samples.petclinic.jpa;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Tests for the DAO variant based on the shared EntityManager approach, using
|
||||
* Hibernate EntityManager for testing instead of the reference implementation.
|
||||
* </p>
|
||||
* <p>
|
||||
* Specifically tests usage of an <code>orm.xml</code> file, loaded by the
|
||||
* persistence provider through the Spring-provided persistence unit root URL.
|
||||
* </p>
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class HibernateEntityManagerClinicTests extends EntityManagerClinicTests {
|
||||
|
||||
@Override
|
||||
protected String[] getConfigPaths() {
|
||||
return new String[] {
|
||||
"applicationContext-jpaCommon.xml",
|
||||
"applicationContext-hibernateAdapter.xml",
|
||||
"applicationContext-entityManager.xml"
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
|
||||
package org.springframework.samples.petclinic.jpa;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Tests for the DAO variant based on the shared EntityManager approach, using
|
||||
* Apache OpenJPA for testing instead of the reference implementation.
|
||||
* </p>
|
||||
* <p>
|
||||
* Specifically tests usage of an <code>orm.xml</code> file, loaded by the
|
||||
* persistence provider through the Spring-provided persistence unit root URL.
|
||||
* </p>
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class OpenJpaEntityManagerClinicTests extends EntityManagerClinicTests {
|
||||
|
||||
@Override
|
||||
protected String[] getConfigPaths() {
|
||||
return new String[] {
|
||||
"applicationContext-jpaCommon.xml",
|
||||
"applicationContext-openJpaAdapter.xml",
|
||||
"applicationContext-entityManager.xml"
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.samples.petclinic.web;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.sun.syndication.feed.atom.Entry;
|
||||
import com.sun.syndication.feed.atom.Feed;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.samples.petclinic.Pet;
|
||||
import org.springframework.samples.petclinic.PetType;
|
||||
import org.springframework.samples.petclinic.Visit;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class VisitsAtomViewTest {
|
||||
|
||||
private VisitsAtomView visitView;
|
||||
|
||||
private Map<String, Object> model;
|
||||
|
||||
private Feed feed;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
visitView = new VisitsAtomView();
|
||||
PetType dog = new PetType();
|
||||
dog.setName("dog");
|
||||
Pet bello = new Pet();
|
||||
bello.setName("Bello");
|
||||
bello.setType(dog);
|
||||
Visit belloVisit = new Visit();
|
||||
belloVisit.setPet(bello);
|
||||
belloVisit.setDate(new Date(2009, 0, 1));
|
||||
belloVisit.setDescription("Bello visit");
|
||||
Pet wodan = new Pet();
|
||||
wodan.setName("Wodan");
|
||||
wodan.setType(dog);
|
||||
Visit wodanVisit = new Visit();
|
||||
wodanVisit.setPet(wodan);
|
||||
wodanVisit.setDate(new Date(2009, 0, 2));
|
||||
wodanVisit.setDescription("Wodan visit");
|
||||
List<Visit> visits = new ArrayList<Visit>();
|
||||
visits.add(belloVisit);
|
||||
visits.add(wodanVisit);
|
||||
|
||||
model = new HashMap<String, Object>();
|
||||
model.put("visits", visits);
|
||||
feed = new Feed();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildFeedMetadata() {
|
||||
visitView.buildFeedMetadata(model, feed, null);
|
||||
|
||||
assertNotNull("No id set", feed.getId());
|
||||
assertNotNull("No title set", feed.getTitle());
|
||||
assertEquals("Invalid update set", new Date(2009, 0, 2), feed.getUpdated());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildFeedEntries() throws Exception {
|
||||
List<Entry> entries = visitView.buildFeedEntries(model, null, null);
|
||||
assertEquals("Invalid amount of entries", 2, entries.size());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue