mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2026-01-14 13:51:12 +00:00
fixed lazy loading issues
also renamed some local variables in Unit tests
This commit is contained in:
parent
56c7671939
commit
f44e383462
7 changed files with 52 additions and 54 deletions
|
|
@ -97,22 +97,13 @@ public abstract class AbstractOwnerRepositoryTests {
|
|||
}
|
||||
|
||||
@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());
|
||||
public void findSingleOwner() {
|
||||
Owner owner1 = this.ownerRepository.findById(1);
|
||||
assertTrue(owner1.getLastName().startsWith("Franklin"));
|
||||
Owner owner10 = this.ownerRepository.findById(10);
|
||||
assertEquals("Carlos", owner10.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();
|
||||
assertEquals(owner1.getPets().size(), 1);
|
||||
}
|
||||
|
||||
@Test @Transactional
|
||||
|
|
|
|||
|
|
@ -97,51 +97,51 @@ public abstract class AbstractPetRepositoryTests {
|
|||
public void getPetTypes() {
|
||||
Collection<PetType> petTypes = this.petRepository.findPetTypes();
|
||||
|
||||
PetType t1 = EntityUtils.getById(petTypes, PetType.class, 1);
|
||||
assertEquals("cat", t1.getName());
|
||||
PetType t4 = EntityUtils.getById(petTypes, PetType.class, 4);
|
||||
assertEquals("snake", t4.getName());
|
||||
PetType petType1 = EntityUtils.getById(petTypes, PetType.class, 1);
|
||||
assertEquals("cat", petType1.getName());
|
||||
PetType petType4 = EntityUtils.getById(petTypes, PetType.class, 4);
|
||||
assertEquals("snake", petType4.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findPet() {
|
||||
Collection<PetType> types = this.petRepository.findPetTypes();
|
||||
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.petRepository.findById(6);
|
||||
assertEquals("George", p6.getName());
|
||||
assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), p6.getType().getId());
|
||||
assertEquals("Peter", p6.getOwner().getFirstName());
|
||||
Pet pet7 = this.petRepository.findById(7);
|
||||
assertTrue(pet7.getName().startsWith("Samantha"));
|
||||
assertEquals(EntityUtils.getById(types, PetType.class, 1).getId(), pet7.getType().getId());
|
||||
assertEquals("Jean", pet7.getOwner().getFirstName());
|
||||
Pet pet6 = this.petRepository.findById(6);
|
||||
assertEquals("George", pet6.getName());
|
||||
assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), pet6.getType().getId());
|
||||
assertEquals("Peter", pet6.getOwner().getFirstName());
|
||||
}
|
||||
|
||||
@Test @Transactional
|
||||
public void insertPet() {
|
||||
Owner o6 = this.ownerRepository.findById(6);
|
||||
int found = o6.getPets().size();
|
||||
Owner owner6 = this.ownerRepository.findById(6);
|
||||
int found = owner6.getPets().size();
|
||||
Pet pet = new Pet();
|
||||
pet.setName("bowser");
|
||||
Collection<PetType> types = this.petRepository.findPetTypes();
|
||||
pet.setType(EntityUtils.getById(types, PetType.class, 2));
|
||||
pet.setBirthDate(new DateTime());
|
||||
o6.addPet(pet);
|
||||
assertEquals(found + 1, o6.getPets().size());
|
||||
owner6.addPet(pet);
|
||||
assertEquals(found + 1, owner6.getPets().size());
|
||||
// both storePet and storeOwner are necessary to cover all ORM tools
|
||||
this.petRepository.save(pet);
|
||||
this.ownerRepository.save(o6);
|
||||
o6 = this.ownerRepository.findById(6);
|
||||
assertEquals(found + 1, o6.getPets().size());
|
||||
this.ownerRepository.save(owner6);
|
||||
owner6 = this.ownerRepository.findById(6);
|
||||
assertEquals(found + 1, owner6.getPets().size());
|
||||
}
|
||||
|
||||
@Test @Transactional
|
||||
public void updatePet() throws Exception {
|
||||
Pet p7 = this.petRepository.findById(7);
|
||||
String old = p7.getName();
|
||||
p7.setName(old + "X");
|
||||
this.petRepository.save(p7);
|
||||
p7 = this.petRepository.findById(7);
|
||||
assertEquals(old + "X", p7.getName());
|
||||
Pet pet7 = this.petRepository.findById(7);
|
||||
String old = pet7.getName();
|
||||
pet7.setName(old + "X");
|
||||
this.petRepository.save(pet7);
|
||||
pet7 = this.petRepository.findById(7);
|
||||
assertEquals(old + "X", pet7.getName());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -90,17 +90,16 @@ public abstract class AbstractVisitRepositoryTests {
|
|||
|
||||
@Test @Transactional
|
||||
public void insertVisit() {
|
||||
Pet p7 = this.petRepository.findById(7);
|
||||
int found = p7.getVisits().size();
|
||||
Pet pet7 = this.petRepository.findById(7);
|
||||
int found = pet7.getVisits().size();
|
||||
Visit visit = new Visit();
|
||||
p7.addVisit(visit);
|
||||
pet7.addVisit(visit);
|
||||
visit.setDescription("test");
|
||||
// both storeVisit and storePet are necessary to cover all ORM tools
|
||||
this.visitRepository.save(visit);
|
||||
this.petRepository.save(p7);
|
||||
// assertTrue(!visit.isNew()); -- NOT TRUE FOR TOPLINK (before commit)
|
||||
p7 = this.petRepository.findById(7);
|
||||
assertEquals(found + 1, p7.getVisits().size());
|
||||
this.petRepository.save(pet7);
|
||||
pet7 = this.petRepository.findById(7);
|
||||
assertEquals(found + 1, pet7.getVisits().size());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue