KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > petclinic > AbstractClinicTests


1 package org.springframework.samples.petclinic;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Date JavaDoc;
5
6 import org.springframework.samples.petclinic.util.EntityUtils;
7 import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
8
9 /**
10  * Base class for Clinic tests.
11  * Allows subclasses to specify context locations.
12  *
13  * <p>This class extends AbstractTransactionalDataSourceSpringContextTests,
14  * one of the valuable test superclasses provided in the org.springframework.test
15  * package. This represents best practice for integration tests with Spring.
16  * The AbstractTransactionalDataSourceSpringContextTests superclass provides the
17  * following services:
18  * <li>Injects test dependencies, meaning that we don't need to perform application
19  * context lookups. See the setClinic() method. Injection uses autowiring by type.
20  * <li>Executes each test method in its own transaction, which is automatically
21  * rolled back by default. This means that even if tests insert or otherwise
22  * change database state, there is no need for a teardown or cleanup script.
23  * <li>Provides useful inherited protected fields, such as a JdbcTemplate that can be
24  * used to verify database state after test operations, or verify the results of queries
25  * performed by application code. An ApplicationContext is also inherited, and can be
26  * used for explicit lookup if necessary.
27  *
28  * <p>The AbstractTransactionalDataSourceSpringContextTests and related classes are shipped
29  * in the spring-mock.jar.
30  *
31  * @see org.springframework.test.AbstractTransactionalDataSourceSpringContextTests
32  * @author Ken Krebs
33  * @author Rod Johnson
34  * @author Juergen Hoeller
35  */

36 public abstract class AbstractClinicTests extends AbstractTransactionalDataSourceSpringContextTests {
37
38     protected Clinic clinic;
39
40     /**
41      * This method is provided to set the Clinic instance being tested by the Dependency Injection
42      * injection behaviour of the superclass from the <code>org.springframework.test</code> package.
43      * @param clinic clinic to test
44      */

45     public void setClinic(Clinic clinic) {
46         this.clinic = clinic;
47     }
48
49     public void testGetVets() {
50         Collection JavaDoc vets = this.clinic.getVets();
51         
52         // Use the inherited JdbcTemplate (from AbstractTransactionalDataSourceSpringContextTests)
53
// to verify the results of the query
54
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 JavaDoc 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 JavaDoc 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         // Check lazy loading, by ending the transaction
93
endTransaction();
94         // Now Owners are "disconnected" from the data store.
95
// We might need to touch this collection if we switched to lazy loading
96
// in mapping files, but this test would pick this up.
97
o1.getPets();
98     }
99
100     public void testInsertOwner() {
101         Collection JavaDoc 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         // assertTrue(!owner.isNew()); -- NOT TRUE FOR TOPLINK (before commit)
107
owners = this.clinic.findOwners("Schultz");
108         assertEquals(found + 1, owners.size());
109     }
110
111     public void testUpdateOwner() throws Exception JavaDoc {
112         Owner o1 = this.clinic.loadOwner(1);
113         String JavaDoc 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 JavaDoc 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 JavaDoc types = this.clinic.getPetTypes();
139         pet.setType((PetType) EntityUtils.getById(types, PetType.class, 2));
140         pet.setBirthDate(new Date JavaDoc());
141         assertEquals(found + 1, o6.getPets().size());
142         // both storePet and storeOwner are necessary to cover all ORM tools
143
this.clinic.storePet(pet);
144         this.clinic.storeOwner(o6);
145         // assertTrue(!pet.isNew()); -- NOT TRUE FOR TOPLINK (before commit)
146
o6 = this.clinic.loadOwner(6);
147         assertEquals(found + 1, o6.getPets().size());
148     }
149
150     public void testUpdatePet() throws Exception JavaDoc {
151         Pet p7 = this.clinic.loadPet(7);
152         String JavaDoc 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         // both storeVisit and storePet are necessary to cover all ORM tools
166
this.clinic.storeVisit(visit);
167         this.clinic.storePet(p7);
168         // assertTrue(!visit.isNew()); -- NOT TRUE FOR TOPLINK (before commit)
169
p7 = this.clinic.loadPet(7);
170         assertEquals(found + 1, p7.getVisits().size());
171     }
172
173 }
174
Popular Tags