1 package org.springframework.samples.petclinic; 2 3 import java.util.Collection ; 4 import java.util.Date ; 5 6 import org.springframework.samples.petclinic.util.EntityUtils; 7 import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests; 8 9 36 public abstract class AbstractClinicTests extends AbstractTransactionalDataSourceSpringContextTests { 37 38 protected Clinic clinic; 39 40 45 public void setClinic(Clinic clinic) { 46 this.clinic = clinic; 47 } 48 49 public void testGetVets() { 50 Collection vets = this.clinic.getVets(); 51 52 assertEquals("JDBC query must show the same number of vets", 55 jdbcTemplate.queryForInt("SELECT COUNT(0) FROM VETS"), 56 vets.size()); 57 Vet v1 = (Vet) EntityUtils.getById(vets, Vet.class, 2); 58 assertEquals("Leary", v1.getLastName()); 59 assertEquals(1, v1.getNrOfSpecialties()); 60 assertEquals("radiology", ((Specialty) v1.getSpecialties().get(0)).getName()); 61 Vet v2 = (Vet) EntityUtils.getById(vets, Vet.class, 3); 62 assertEquals("Douglas", v2.getLastName()); 63 assertEquals(2, v2.getNrOfSpecialties()); 64 assertEquals("dentistry", ((Specialty) v2.getSpecialties().get(0)).getName()); 65 assertEquals("surgery", ((Specialty) v2.getSpecialties().get(1)).getName()); 66 } 67 68 public void testGetPetTypes() { 69 Collection petTypes = this.clinic.getPetTypes(); 70 assertEquals("JDBC query must show the same number of pet typess", 71 jdbcTemplate.queryForInt("SELECT COUNT(0) FROM TYPES"), 72 petTypes.size()); 73 PetType t1 = (PetType) EntityUtils.getById(petTypes, PetType.class, 1); 74 assertEquals("cat", t1.getName()); 75 PetType t4 = (PetType) EntityUtils.getById(petTypes, PetType.class, 4); 76 assertEquals("snake", t4.getName()); 77 } 78 79 public void testFindOwners() { 80 Collection owners = this.clinic.findOwners("Davis"); 81 assertEquals(2, owners.size()); 82 owners = this.clinic.findOwners("Daviss"); 83 assertEquals(0, owners.size()); 84 } 85 86 public void testLoadOwner() { 87 Owner o1 = this.clinic.loadOwner(1); 88 assertTrue(o1.getLastName().startsWith("Franklin")); 89 Owner o10 = this.clinic.loadOwner(10); 90 assertEquals("Carlos", o10.getFirstName()); 91 92 endTransaction(); 94 o1.getPets(); 98 } 99 100 public void testInsertOwner() { 101 Collection owners = this.clinic.findOwners("Schultz"); 102 int found = owners.size(); 103 Owner owner = new Owner(); 104 owner.setLastName("Schultz"); 105 this.clinic.storeOwner(owner); 106 owners = this.clinic.findOwners("Schultz"); 108 assertEquals(found + 1, owners.size()); 109 } 110 111 public void testUpdateOwner() throws Exception { 112 Owner o1 = this.clinic.loadOwner(1); 113 String old = o1.getLastName(); 114 o1.setLastName(old + "X"); 115 this.clinic.storeOwner(o1); 116 o1 = this.clinic.loadOwner(1); 117 assertEquals(old + "X", o1.getLastName()); 118 } 119 120 public void testLoadPet() { 121 Collection types = this.clinic.getPetTypes(); 122 Pet p7 = this.clinic.loadPet(7); 123 assertTrue(p7.getName().startsWith("Samantha")); 124 assertEquals(EntityUtils.getById(types, PetType.class, 1).getId(), p7.getType().getId()); 125 assertEquals("Jean", p7.getOwner().getFirstName()); 126 Pet p6 = this.clinic.loadPet(6); 127 assertEquals("George", p6.getName()); 128 assertEquals(EntityUtils.getById(types, PetType.class, 4).getId(), p6.getType().getId()); 129 assertEquals("Peter", p6.getOwner().getFirstName()); 130 } 131 132 public void testInsertPet() { 133 Owner o6 = this.clinic.loadOwner(6); 134 int found = o6.getPets().size(); 135 Pet pet = new Pet(); 136 pet.setName("bowser"); 137 o6.addPet(pet); 138 Collection types = this.clinic.getPetTypes(); 139 pet.setType((PetType) EntityUtils.getById(types, PetType.class, 2)); 140 pet.setBirthDate(new Date ()); 141 assertEquals(found + 1, o6.getPets().size()); 142 this.clinic.storePet(pet); 144 this.clinic.storeOwner(o6); 145 o6 = this.clinic.loadOwner(6); 147 assertEquals(found + 1, o6.getPets().size()); 148 } 149 150 public void testUpdatePet() throws Exception { 151 Pet p7 = this.clinic.loadPet(7); 152 String old = p7.getName(); 153 p7.setName(old + "X"); 154 this.clinic.storePet(p7); 155 p7 = this.clinic.loadPet(7); 156 assertEquals(old + "X", p7.getName()); 157 } 158 159 public void testInsertVisit() { 160 Pet p7 = this.clinic.loadPet(7); 161 int found = p7.getVisits().size(); 162 Visit visit = new Visit(); 163 p7.addVisit(visit); 164 visit.setDescription("test"); 165 this.clinic.storeVisit(visit); 167 this.clinic.storePet(p7); 168 p7 = this.clinic.loadPet(7); 170 assertEquals(found + 1, p7.getVisits().size()); 171 } 172 173 } 174 | Popular Tags |