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.BufferedReader;
8   import java.io.File;
9   import java.io.FileNotFoundException;
10  import java.io.FileReader;
11  import java.io.IOException;
12  import java.io.InputStream;
13  import java.io.InputStreamReader;
14  import java.io.Reader;
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  import org.junit.After;
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.springframework.beans.factory.xml.XmlBeanFactory;
22  import org.springframework.core.io.ClassPathResource;
23  
24  import at.ac.tuwien.ifs.bpse.basic.domain.Student;
25  import at.ac.tuwien.ifs.bpse.basic.export_import.HtmlExport;
26  import at.ac.tuwien.ifs.bpse.basic.helper.Constants;
27  
28  /**
29   * Class containing the TestCases for the HTML Exporter. Because we have no
30   * import-mechanism for HTML we can only compare the generated HTML-File to a
31   * file which was checked manualy.
32   * 
33   * @author The SE-Team
34   * @version 1.0
35   */
36  public class HtmlExportTest {
37  
38  	//private static Log log = LogFactory.getLog(HtmlExportTest.class);
39  	private XmlBeanFactory xbf;
40  	
41  	/**
42  	 * Holds the absolute path of the current directory. 
43  	 */
44  	private String pathToFile;
45  	
46  	/**
47  	 * The list of Students used as testdata, also reinitialized for every new
48  	 * TestCase.
49  	 */
50  	private List<Student> studenten;
51  
52  
53  	/**
54  	 * This method is executen before every TestCase
55  	 */
56  	@Before
57  	public void setUp() throws Exception {
58  		ClassPathResource res = new ClassPathResource(Constants.SPRINGBEANS_TEST);
59  		xbf = new XmlBeanFactory(res);
60  		ClassPathResource currentWorkingDir = new ClassPathResource(".");
61  		pathToFile = currentWorkingDir.getFile().getAbsolutePath();		
62  		studenten = new ArrayList<Student>();
63  		Student s1 = (Student) xbf.getBean("StudentAlexanderSchatten");
64  		s1.setId(1);
65  		Student s2 = (Student) xbf.getBean("StudentHubertMeixner");
66  		s2.setId(2);
67  		studenten.add(s1);
68  		studenten.add(s2);
69  	}
70  
71  	/**
72  	 * This method is invoced after each TestCase
73  	 */
74  	@After
75  	public void tearDown() throws Exception {
76  		xbf.destroySingletons();
77  	}
78  
79  	/**
80  	 * Reads a file into a String.
81  	 * 
82  	 * @param filename
83  	 *            the File to read.
84  	 * @return The content of the file as String.
85  	 */
86  	private String readFile(Reader r) {
87  		StringBuffer fcb = new StringBuffer();
88  		try {
89  			BufferedReader br = new BufferedReader(r);
90  
91  			String line;
92  			while ((line = br.readLine()) != null) {
93  				fcb.append(line);
94  			}
95  			br.close();
96  
97  		} catch (FileNotFoundException e) {
98  			e.printStackTrace();
99  		} catch (IOException e) {
100 			e.printStackTrace();
101 		}
102 		return fcb.toString();
103 	}
104 
105 	/**
106 	 * TestCase to creating an HTML-File. Because we have no importer for HTML,
107 	 * we compare the result with a file that was manually checked.
108 	 * @throws IOException 
109 	 * 
110 	 */
111 	@Test
112 	public void testWrite() throws IOException {
113 		String filename_test = pathToFile + "/test/export.html";
114 		
115 		// export test data to html file
116 		HtmlExport he = new HtmlExport();
117 		he.setTitle("Html Export");
118 		try {
119 			he.write(studenten, filename_test);
120 		} catch (IOException e) {
121 			e.printStackTrace();
122 		}
123 		// check if export is correct
124 		//InputStream ipCorrect = getClass().getResourceAsStream("basic/target/test-classes/test/html-export.html");
125 		File newFile = new File("target/test-classes/test/html-export.html");
126 		String correct = readFile(new FileReader(newFile));
127 		//String correct = readFile(new InputStreamReader(ipCorrect));
128 
129 		InputStream ipCorrect = getClass().getResourceAsStream("/test/html-export.html");
130 
131 		correct = readFile(new InputStreamReader(ipCorrect));
132 		String test = readFile(new FileReader(filename_test));
133 		assertThat(test, is(correct));
134 		
135 		ipCorrect.close();
136 		
137 		// delete file
138 		File f = new File(filename_test);
139 		assertThat(f.delete(), is(not(false)));
140 	}
141 
142 }