View Javadoc

1   package at.ac.tuwien.ifs.bpse.basic.test.export;
2   
3   import static org.hamcrest.Matchers.is;
4   import static org.hamcrest.Matchers.not;
5   import static org.junit.Assert.assertThat;
6   
7   import java.io.IOException;
8   import java.util.ArrayList;
9   import java.util.List;
10  
11  import org.junit.After;
12  import org.junit.Before;
13  import org.junit.Test;
14  import org.springframework.beans.factory.xml.XmlBeanFactory;
15  import org.springframework.core.io.ClassPathResource;
16  
17  import at.ac.tuwien.ifs.bpse.basic.domain.Student;
18  import at.ac.tuwien.ifs.bpse.basic.export_import.Export;
19  import at.ac.tuwien.ifs.bpse.basic.export_import.Import;
20  import at.ac.tuwien.ifs.bpse.basic.helper.Constants;
21  
22  /**
23   * Class containing the testcases for the Importers and Exporters. The instanced
24   * for this TestCase are retrieved by the BeanFactory. Testing an Importer
25   * combined with an Exporter ist rather easy: At first we generate some
26   * TestData, then the TestData is exported and imported again. Finally we
27   * compare the Imported data with the data we have first exported.
28   * 
29   * @author The SE-Team
30   * @version 1.0
31   */
32  public class ExportImportTest {
33  
34  	/**
35  	 * Spring Frameworks XML Bean Factory.
36  	 */
37  	private XmlBeanFactory xbf;
38  	
39  	/**
40  	 * Holds the absolute path of the current directory. 
41  	 */
42  	private String pathToFile;
43  	
44  	/**
45  	 * The list of Students used as testdata, also reinitialized for every new
46  	 * TestCase.
47  	 */
48  	private List<Student> studenten;
49  
50  	/**
51  	 * This method is executen before every TestCase.
52  	 */
53  	@Before
54  	public void setUp() throws Exception {
55  		ClassPathResource res = new ClassPathResource(Constants.SPRINGBEANS_TEST);
56  		xbf = new XmlBeanFactory(res);
57  		ClassPathResource currentWorkingDir = new ClassPathResource(".");
58  		pathToFile = currentWorkingDir.getFile().getAbsolutePath();		
59  		studenten = generateStudentList();
60  	}
61  
62  	/**
63  	 * This method is invoced after each TestCase.
64  	 */
65  	@After
66  	public void tearDown() throws Exception {
67  		studenten = null;
68  		xbf.destroySingletons();
69  	}
70  
71  	/**
72  	 * Generate some testdata. The data is generated by the BeanFactory.
73  	 * 
74  	 * @return Testdata
75  	 */
76  	private List<Student> generateStudentList() {
77  		List<Student> studenten = new ArrayList<Student>();
78  		Student s1 = (Student) xbf.getBean("StudentAlexanderSchatten");
79  		s1.setId(1);
80  		Student s2 = (Student) xbf.getBean("StudentHubertMeixner");
81  		s2.setId(2);
82  		studenten.add(s1);
83  		studenten.add(s2);
84  		return studenten;
85  	}
86  
87  	/**
88  	 * TestCase for an Export followed by an Import. Afterwards the imported
89  	 * data is compared with the data that was previous exported.
90  	 * 
91  	 */
92  	@Test
93  	public void testWriteRead() {
94  		final String filename = pathToFile + "/test/studenten.xml";
95  		Export export = (Export) xbf.getBean("Export");
96  
97  		// save and re-read document
98  		try {
99  			export.write(studenten, filename);
100 		} catch (IOException e) {
101 			e.printStackTrace();
102 		}
103 		// read data as List object
104 		List<Student> impStud = null;
105 		try {
106 			Import imp = (Import) xbf.getBean("Import");
107 			impStud = imp.read(filename);
108 		} catch (IOException e2) {
109 			e2.printStackTrace();
110 		}
111 		assertThat(studenten, is(not(impStud)));
112 		assertThat(impStud.size(), is(studenten.size()));
113 		// Cycle through the imported students and compare them to the exported ones
114 		int i = 0;
115 		for (Student stud: impStud) {
116 			assertThat(stud.getId(), is(studenten.get(i).getId()));
117 			assertThat(stud.getMatnr(), is(studenten.get(i).getMatnr()));
118 			assertThat(stud.getFirstname(), is(studenten.get(i).getFirstname()));
119 			assertThat(stud.getLastname(), is(studenten.get(i).getLastname()));
120 			assertThat(stud.getEmail(), is(studenten.get(i).getEmail()));
121 			i++;
122 		}
123 	}
124 
125 }