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.dom4j.Document;
12  import org.dom4j.DocumentException;
13  import org.dom4j.Element;
14  import org.junit.After;
15  import org.junit.Before;
16  import org.junit.Test;
17  import org.springframework.beans.factory.xml.XmlBeanFactory;
18  import org.springframework.core.io.ClassPathResource;
19  
20  import at.ac.tuwien.ifs.bpse.basic.domain.Student;
21  import at.ac.tuwien.ifs.bpse.basic.export_import.XmlExportImport;
22  import at.ac.tuwien.ifs.bpse.basic.helper.Constants;
23  
24  /**
25   * Class containing the TestCases for the XML-Importer and Exporter. Testing an
26   * Importer combined with an Exporter ist rather easy: At first we generate some
27   * TestData, then the TestData is exported and imported again. Finally we
28   * compare the Imported data with the data we have first exported.
29   * 
30   * @author The SE-Team
31   * @version 1.0
32   */
33  public class XmlExportImportTest {
34  
35  	/**
36  	 * The Spring Bean Factory, it is recreated for each new TestCase.
37  	 */
38  	private XmlBeanFactory xbf;
39  	
40  	/**
41  	 * Holds the absolute path of the current directory. 
42  	 */
43  	private String pathToFile;
44  
45  	/**
46  	 * The list of Students used as testdata, also reinitialized for every new
47  	 * TestCase.
48  	 */
49  	private List<Student> studenten;
50  
51  	/**
52  	 * This method is invoked before each TestCase.
53  	 */
54  	@Before
55  	public void setUp() throws Exception {
56  		ClassPathResource res = new ClassPathResource(Constants.SPRINGBEANS_TEST);
57  		xbf = new XmlBeanFactory(res);
58  		ClassPathResource currentWorkingDir = new ClassPathResource(".");
59  		pathToFile = currentWorkingDir.getFile().getAbsolutePath();		
60  		studenten = generateStudentList();
61  	}
62  
63  	/**
64  	 * This method is executed after every TestCase.
65  	 */
66  	@After
67  	public void tearDown() throws Exception {
68  		studenten = null;
69  		xbf.destroySingletons();
70  	}
71  
72  	/**
73  	 * Generate some testdata, this data is generated by the BeanFactory.
74  	 * 
75  	 * @return Testdata
76  	 */
77  	private List<Student> generateStudentList() {
78  		List<Student> studenten = new ArrayList<Student>();
79  		Student s1 = (Student) xbf.getBean("StudentAlexanderSchatten");
80  		s1.setId(1);
81  		Student s2 = (Student) xbf.getBean("StudentHubertMeixner");
82  		s2.setId(2);
83  		studenten.add(s1);
84  		studenten.add(s2);
85  		return studenten;
86  	}
87  
88  	/**
89  	 * This method checks an XML-Document. It also checks for valid XML.
90  	 * 
91  	 * @param doc
92  	 *            The Document to check
93  	 */
94  	private void checkXml(Document doc) {
95  		// check root element
96  		Element rootEl = doc.getRootElement();
97  		assertThat(rootEl.getName(), is("students"));
98  		// check two children
99  		List<Student> studentenEl = (List<Student>) rootEl.elements();
100 		assertThat(studentenEl.size(), is(2));
101 		// check one Student Element
102 		Element sEl = (Element) studentenEl.get(0);
103 		assertThat(sEl.attributeValue("id"), is("1"));
104 		assertThat(sEl.elementText("firstname"), is("Alexander"));
105 		assertThat(sEl.elementText("lastname"), is("Schatten"));
106 		assertThat(sEl.elementText("matnr"), is("8925164"));
107 		assertThat(sEl.elementText("email"), is("alexander@schatten.info"));
108 	}
109 
110 	/**
111 	 * TestCase for the generation of a XML file.
112 	 */
113 	@Test
114 	public void testGenerateXML() {
115 		XmlExportImport xexp = new XmlExportImport();
116 		xexp.generateXML(studenten);
117 		checkXml(xexp.getDocument());
118 	}
119 
120 	/**
121 	 * TestCase for saving an XML file.
122 	 */
123 	@Test
124 	public void testSave() {
125 		final String filename = pathToFile + "/test/studenten.xml";
126 		XmlExportImport xexp = new XmlExportImport();
127 		xexp.generateXML(studenten);
128 		// save and re-read document in XML
129 		try {
130 			xexp.save(filename);
131 			xexp = null;
132 			xexp = new XmlExportImport();
133 			xexp.readXml(filename);
134 			checkXml(xexp.getDocument());
135 		} catch (IOException e) {
136 			e.printStackTrace();
137 		} catch (DocumentException e) {
138 			e.printStackTrace();
139 		}
140 		// read data as List object
141 		List<Student> impStud = null;
142 		try {
143 			impStud = xexp.read(filename);
144 		} catch (IOException e) {
145 			e.printStackTrace();
146 		}
147 		//impStud.equals(studenten);
148 		assertThat(studenten, is(not(impStud)));
149 		assertThat(impStud.size(), is(studenten.size()));
150 		// Cycle through the imported students and compare them to the exported ones
151 		int i = 0;
152 		for (Student stud: impStud) {
153 			assertThat(stud.getId(), is(studenten.get(i).getId()));
154 			assertThat(stud.getMatnr(), is(studenten.get(i).getMatnr()));
155 			assertThat(stud.getFirstname(), is(studenten.get(i).getFirstname()));
156 			assertThat(stud.getLastname(), is(studenten.get(i).getLastname()));
157 			assertThat(stud.getEmail(), is(studenten.get(i).getEmail()));
158 			i++;
159 		}
160 	}
161 
162 }