mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2026-01-13 21:31:11 +00:00
migrated from SimpleJdbcTemplate to JdbcTemplate
This commit is contained in:
parent
4e35adb6b0
commit
608c42f74b
15 changed files with 108 additions and 207 deletions
|
|
@ -81,7 +81,6 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@ContextConfiguration
|
||||
public abstract class AbstractClinicTests {
|
||||
|
||||
@Autowired
|
||||
|
|
@ -121,8 +120,8 @@ public abstract class AbstractClinicTests {
|
|||
assertEquals(0, owners.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadOwner() {
|
||||
@Test @Transactional
|
||||
public void findOwner() {
|
||||
Owner o1 = this.clinic.findOwner(1);
|
||||
assertTrue(o1.getLastName().startsWith("Franklin"));
|
||||
Owner o10 = this.clinic.findOwner(10);
|
||||
|
|
@ -163,7 +162,7 @@ public abstract class AbstractClinicTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void loadPet() {
|
||||
public void findPet() {
|
||||
Collection<PetType> types = this.clinic.getPetTypes();
|
||||
Pet p7 = this.clinic.findPet(7);
|
||||
assertTrue(p7.getName().startsWith("Samantha"));
|
||||
|
|
@ -175,7 +174,7 @@ public abstract class AbstractClinicTests {
|
|||
assertEquals("Peter", p6.getOwner().getFirstName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test @Transactional
|
||||
public void insertPet() {
|
||||
Owner o6 = this.clinic.findOwner(6);
|
||||
int found = o6.getPets().size();
|
||||
|
|
@ -204,7 +203,7 @@ public abstract class AbstractClinicTests {
|
|||
assertEquals(old + "X", p7.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test @Transactional
|
||||
public void insertVisit() {
|
||||
Pet p7 = this.clinic.findPet(7);
|
||||
int found = p7.getVisits().size();
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ 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.JpaClinicTests;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
|
@ -27,10 +28,13 @@ import static junit.framework.Assert.assertFalse;
|
|||
*/
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-jpa.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class UsageLogAspectTests extends JpaClinicTests {
|
||||
public class UsageLogAspectTests {
|
||||
|
||||
@Autowired
|
||||
private UsageLogAspect usageLogAspect;
|
||||
|
||||
@Autowired
|
||||
private Clinic clinic;
|
||||
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
package org.springframework.samples.petclinic.jdbc;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.samples.petclinic.AbstractClinicTests;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Integration tests for the {@link JdbcClinic} implementation.
|
||||
* </p>
|
||||
* <p>
|
||||
* "JdbcClinicTests-context.xml" determines the actual beans to test.
|
||||
* </p>
|
||||
*
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-jdbc.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@DirtiesContext
|
||||
public class JdbcClinicTests extends AbstractClinicTests {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
package org.springframework.samples.petclinic.jdbc;
|
||||
|
||||
import org.springframework.samples.petclinic.AbstractClinicTests;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
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
|
||||
@DirtiesContext
|
||||
public class SimpleJdbcClinicTests extends AbstractClinicTests {
|
||||
|
||||
}
|
||||
|
|
@ -14,6 +14,7 @@ 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.Owner;
|
||||
import org.springframework.samples.petclinic.Pet;
|
||||
|
|
@ -46,13 +47,13 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
|
||||
@ContextConfiguration(locations={"classpath:spring/applicationContext-jpa.xml"})
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class JpaClinicTests {
|
||||
public class JpaClinicTests extends AbstractClinicTests {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Autowired
|
||||
protected Clinic clinic;
|
||||
private Clinic clinic;
|
||||
|
||||
|
||||
@Test
|
||||
|
|
@ -64,121 +65,5 @@ public class JpaClinicTests {
|
|||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test @Transactional
|
||||
public void testGetVets() {
|
||||
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 testGetPetTypes() {
|
||||
Collection<PetType> petTypes = this.clinic.getPetTypes();
|
||||
|
||||
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 testFindOwners() {
|
||||
Collection<Owner> owners = this.clinic.findOwners("Davis");
|
||||
assertEquals(2, owners.size());
|
||||
owners = this.clinic.findOwners("Daviss");
|
||||
assertEquals(0, owners.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tesFindOwner() {
|
||||
Owner o1 = this.clinic.findOwner(1);
|
||||
assertTrue(o1.getLastName().startsWith("Franklin"));
|
||||
Owner o10 = this.clinic.findOwner(10);
|
||||
assertEquals("Carlos", o10.getFirstName());
|
||||
}
|
||||
|
||||
@Test
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateOwner() 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 testFindPet() {
|
||||
Collection<PetType> types = this.clinic.getPetTypes();
|
||||
Pet p7 = this.clinic.findPet(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);
|
||||
assertEquals("George", p6.getName());
|
||||
assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), p6.getType().getId());
|
||||
assertEquals("Peter", p6.getOwner().getFirstName());
|
||||
}
|
||||
|
||||
@Test @Transactional
|
||||
public void testInsertPet() {
|
||||
Owner o6 = this.clinic.findOwner(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.findOwner(6);
|
||||
assertEquals(found + 1, o6.getPets().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatePet() throws Exception {
|
||||
Pet p7 = this.clinic.findPet(7);
|
||||
String old = p7.getName();
|
||||
p7.setName(old + "X");
|
||||
this.clinic.storePet(p7);
|
||||
p7 = this.clinic.findPet(7);
|
||||
assertEquals(old + "X", p7.getName());
|
||||
}
|
||||
|
||||
@Test @Transactional
|
||||
public void testInsertVisit() {
|
||||
Pet p7 = this.clinic.findPet(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.findPet(7);
|
||||
assertEquals(found + 1, p7.getVisits().size());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue