View Javadoc

1   package at.ac.tuwien.ifs.bpse.basic.test.dao;
2   
3   import static org.hamcrest.Matchers.greaterThan;
4   import static org.hamcrest.Matchers.greaterThanOrEqualTo;
5   import static org.hamcrest.Matchers.is;
6   import static org.hamcrest.Matchers.not;
7   import static org.hamcrest.Matchers.notNullValue;
8   import static org.hamcrest.Matchers.nullValue;
9   import static org.junit.Assert.assertThat;
10  
11  import java.util.List;
12  
13  import org.apache.log4j.Logger;
14  import org.junit.After;
15  import org.junit.Before;
16  import org.junit.BeforeClass;
17  import org.junit.Test;
18  import org.springframework.beans.factory.xml.XmlBeanFactory;
19  import org.springframework.core.io.ClassPathResource;
20  
21  import at.ac.tuwien.ifs.bpse.basic.dao.IStudentDAO;
22  import at.ac.tuwien.ifs.bpse.basic.dao.IStudentDAO.SortOrder;
23  import at.ac.tuwien.ifs.bpse.basic.domain.Student;
24  import at.ac.tuwien.ifs.bpse.basic.helper.Constants;
25  
26  /**
27   * Class containing the testcases for IStudentDAO. 
28   * 
29   * The test assumes that there is specific test-data in the database and reads additional test-data from test-beans.xml.
30   * 
31   * The hsqldb database with the test-data can be found in 
32   * 
33   *  /basic/src/test/resources/data
34   * 
35   * This database is used for the tests (as it is defined in the src/test/resources/test-beans.xml as the datasource). 
36   * 
37   * when "mvn compile" is executed, this database files are copied to the target folder which is:
38   * 
39   *  /basic/target/test-classes/data
40   *  
41   * Consequently, if you should "destroy" the test data in the target directory,
42   * just execute a "mvn clean compile" and a fresh database is again copied from the src/test/resources/data directory.
43   *  
44   *  
45   * One addtional thing should be noted: there are two beans.xml files and two database directories:
46   * 
47   * One set in the src/main directory the other set in the src/test directory.
48   *  
49   * This allows us to distinguish between running tests and running the application. For example: when running the application
50   * changing data cannot destroy the data that we need for the tests.
51   * 
52   * The "operative" beans.xml does not contain any test-data.
53   * 
54   * 
55   * @author The SE-Team
56   * @version 2.1
57   * 
58   */
59  public class JdbcStudentTest {
60  	
61  	/**
62  	 * Data Access Object for Student, fetched with xbf.
63  	 */
64  	private IStudentDAO studentDAO = null;
65  
66  	/**
67  	 * Spring Framework XML Bean Factory.
68  	 */
69  	private static XmlBeanFactory xbf;
70  	
71  	private static Logger log = Logger.getLogger(JdbcStudentTest.class);
72  
73  	
74  	/**
75  	 * This method is executed before all testcases.
76  	 * 
77  	 */
78  	@BeforeClass
79  	public static void setUpBefore() throws Exception {
80  		// notice, that the TEST beans.xml is loaded!
81  		ClassPathResource res = new ClassPathResource(Constants.SPRINGBEANS_TEST);
82  		xbf = new XmlBeanFactory(res);
83  		/* since the datasource configured in test-beans.xml is pointing to a file 
84  		   with predefined test-data, we don't need to insert any test-data here. */
85  	}
86  	
87  	/**
88  	 * This method is executed before every testcase.
89  	 * 
90  	 */
91  	@Before
92  	public void setUp() throws Exception {
93  		studentDAO = (IStudentDAO) xbf.getBean("StudentDAO");
94  	}
95  
96  	/**
97  	 * This method is executed after each testcase.
98  	 * 
99  	 */
100 	@After
101 	public void tearDown() throws Exception {
102 		studentDAO = null;
103 		//xbf.destroySingletons();
104 	}
105 
106 	/**
107 	 * Test StudentBean initialisation, make sure Spring Framework works and the 
108 	 * XML Bean Factory is returning test-data for our save & updateStudent tests.
109 	 * 
110 	 */
111 	@Test
112 	public void beanInit_shouldLoadStudentFromSpringBeanConfig() {
113 		Student student = (Student) xbf.getBean("StudentAlexanderSchatten");
114 		assertThat(student, is(notNullValue()));
115 		assertThat(student.getId(), is(1));
116 		assertThat(student.getFirstname(), is("Alexander"));
117 		assertThat(student.getLastname(), is("Schatten"));
118 		assertThat(student.getEmail(), is("alexander@schatten.info"));
119 	}
120 
121 	/**
122 	 * Test the method getStudent.
123 	 * 
124 	 * Test-data already exists in the database and the dataset with id=0 is read.
125 	 * 
126 	 * 
127 	 */
128 	@Test
129 	public void getStudent_shouldRetrieveStudentFromDB() {
130 		Student student = studentDAO.getStudent(0);
131 		assertThat(student, is(notNullValue()));
132 		assertThat(student.getFirstname(), is("Fritz"));
133 		assertThat(student.getLastname(), is("Haber"));
134 		assertThat(student.getEmail(), is("fritz@haber.test"));
135 	}
136 
137 	/**
138 	 * Test the methods saveStudent and deleteStudent - actually this is 
139 	 * a complete CRD (Create, Read, Delete) lifecycle.
140 	 * 
141 	 * 
142 	 */
143 	@Test
144 	public void saveStudent_shouldAddStudentToDB() {
145 		// get a test-dataset from Spring config
146 		Student student = (Student) xbf.getBean("StudentAddUpdateDelete");
147 		Integer oldId = student.getId();
148 		
149 		// Add Student
150 		studentDAO.saveStudent(student);
151 		// new ID should be different from old ID
152 		assertThat(student.getId(), is(not(oldId)));
153 
154 		// check if student was added
155 		// read student from database using the new generated ID
156 		// then compare if the newly read data equals the old one,
157 		// then check is complete
158 		Student checkStudent = studentDAO.getStudent(student.getId());
159 		assertThat(checkStudent.getFirstname(), is("Test"));
160 		assertThat(checkStudent.getLastname(), is("Student"));
161 		assertThat(checkStudent.getMatnr(), is("0926759"));
162 	
163 		//	 Delete Student
164 		assertThat(studentDAO.deleteStudent(student.getId()), is(notNullValue()));
165 		
166 		// check if student was deleted
167 		// try to retrieve student by ID again
168 		checkStudent = studentDAO.getStudent(student.getId());
169 		// no dataset must be found (as it was deleted above)
170 		assertThat(checkStudent, is(nullValue()));
171 	}
172 	
173 	/**
174 	 * Specifically test the update method 
175 	 *
176 	 */
177 	@Test
178 	public void updateStudent_shouldUpdateStudentInDB() {
179 		// First add one student to database
180 		Student student = (Student) xbf.getBean("StudentAddUpdateDelete");
181 		studentDAO.saveStudent(student);
182 		
183 		//change the student data and update this student in database
184 		student.setFirstname(student.getFirstname() + "2");
185 		student.setLastname(student.getLastname() + "2");
186 		student.setEmail(student.getEmail() + "2");
187 		assertThat(studentDAO.updateStudent(student), is(notNullValue()));
188 
189 		// retrieve student again from database and check if all fields are updated correctly
190 		Student newStudent = studentDAO.getStudent(student.getId());
191 		assertThat(newStudent.getLastname(), is(student.getLastname()));
192 		assertThat(newStudent.getFirstname(), is(student.getFirstname()));
193 		assertThat(newStudent.getEmail(),is(student.getEmail()));
194 		
195 		// delete student again
196 		studentDAO.deleteStudent(student.getId());
197 	}
198 
199 	/**
200 	 * Test the method getStudents, ordered by Matrikelnummer.
201 	 * 
202 	 */
203 	@Test
204 	public void getStudents_shouldRetrieveAllStudentsOrderedByMatnrFromDB() {
205 		// Order by Matrikelnummer
206 		List<Student> students = studentDAO.getStudents(SortOrder.StudentId);
207 		assertThat(students, is(notNullValue()));
208 		Student pstud = null;
209 		// See if the Matrikelnumbers in the list are actually in order
210 		for (Student stud : students) {
211 			if (pstud != null) {
212 				// ID from DB aswell as Matrikelnumber must be unique
213 				assertThat(pstud.getId(), is(not(stud.getId())));
214 				assertThat(pstud.getMatnr(), is(not(stud.getMatnr())));
215 				log.info("Comparing student " + stud.getMatnr() + " to student " + pstud.getMatnr());
216 				assertThat(stud.getMatnr().compareTo(pstud.getMatnr()), is(greaterThan(0)));
217 			}
218 			pstud = stud;
219 		}
220 	}
221 
222 	/**
223 	 * Test the method getStudents, ordered by Nachname.
224 	 * 
225 	 * 
226 	 */
227 	@Test
228 	public void getStudents_shouldRetrieveAllStudentsOrderedByNachnameFromDB() {
229 		// Order by Nachname
230 		List<Student> students = studentDAO.getStudents(SortOrder.LastName);
231 		assertThat(students, is(notNullValue()));
232 		Student pstud = null;
233 		// See if the Last Names (Nachname) in the list are actually in order
234 		for (Student stud : students) {
235 			if (pstud != null) {
236 				// ID from DB aswell as Matrikelnumber must be unique
237 				assertThat(pstud.getId(), is(not(stud.getId())));
238 				assertThat(pstud.getMatnr(), is(not(stud.getMatnr())));
239 				assertThat(stud.getLastname().compareTo(pstud.getLastname()), is(greaterThanOrEqualTo(0)));
240 			}
241 			pstud = stud;
242 		}
243 	}
244 
245 }