1 package net.objectlab.qalab.m2.util;
2
3 import junit.framework.TestCase;
4
5 import java.io.File;
6 import java.io.InputStream;
7 import java.io.IOException;
8 import javax.xml.transform.TransformerException;
9 import java.io.FileReader;
10 import java.io.BufferedReader;
11
12 /**
13 * Test of the <code>ReportUtils</code>.
14 * @author Dave Sag
15 */
16 public class XmlTransformerTest extends TestCase {
17
18 private static final String TEST_DATA = "XmlTransformerTest"
19 + File.separator;
20
21 private static final String TEST_XML = TEST_DATA + "test.xml";
22 private static final String TEST_XSLT = TEST_DATA + "test.xsl";
23 private static final String TEST_OUTPUT_DIR =
24 System.getProperty("java.io.tmpdir")
25 + File.separator
26 + "qalab-maven2-plugin" + File.separator
27 + "testoutput" + File.separator
28 + "XmlTransformerTest" + File.separator;
29
30 public void setUp() throws Exception {
31
32 File odir = new File(TEST_OUTPUT_DIR);
33 if (odir.exists()) {
34 try {
35 org.apache.commons.io.FileUtils.cleanDirectory(odir);
36 } catch(java.io.IOException e) {}
37 }
38 }
39
40 /**
41 * tests that if we pass null values an assetion error is thrown.
42 */
43 public void testConstructTransformerWithNullsFails() {
44 try {
45 XmlTransformer t = new XmlTransformer(null, null, null);
46 fail("expected this to throw an assertion error.");
47 } catch (AssertionError aer) {
48
49 }
50 }
51
52 /**
53 * tests that we can transform a simple file.
54 */
55 public void testTransformSimpleXML() throws IOException {
56 InputStream xml = Utils.extractAsInputStream(TEST_XML);
57 InputStream xslt = Utils.extractAsInputStream(TEST_XSLT);
58 File output = new File(TEST_OUTPUT_DIR
59 + "testTransformSimpleXML.html");
60
61 XmlTransformer t = new XmlTransformer(xml, xslt, output);
62
63 try {
64 t.transform();
65 } catch (TransformerException tex) {
66 fail("caught " + tex);
67 }
68
69 verifyFile(output);
70 }
71
72 /**
73 * tests that we can transform a typical qalab.xml file.
74 */
75 public void testTransformQALabXML() throws IOException {
76 InputStream xml = Utils.extractAsInputStream(TEST_DATA
77 + "qalab.xml");
78 InputStream xslt = Utils.extractAsInputStream(
79 "qalab-chart-html.xsl");
80 File output = new File(TEST_OUTPUT_DIR
81 + "qalab-tmp");
82
83 XmlTransformer t = new XmlTransformer(xml, xslt, output);
84 t.addParameter("targetdir", TEST_OUTPUT_DIR);
85 t.addParameter("types", "checkstyle");
86 t.addParameter("offset", "2005-11-07");
87
88 try {
89 t.transform();
90 } catch (TransformerException tex) {
91 fail("caught " + tex);
92 }
93
94
95
96
97
98 verifyFile(new File(TEST_OUTPUT_DIR + "all-packages.html"));
99 verifyFile(new File(TEST_OUTPUT_DIR + "index.html"));
100 verifyFile(new File(TEST_OUTPUT_DIR + "overview-summary.html"));
101
102
103
104 verifyFile(new File(TEST_OUTPUT_DIR + "stylesheet.css"));
105
106
107
108 verifyFileIsEmpty(output);
109 }
110
111 private void verifyFile(File aFile) {
112 assertTrue("could not read the file.",
113 aFile.canRead());
114 assertTrue("the file didn't contain any data.",
115 aFile.length() > 0l);
116 try {
117 BufferedReader reader = new BufferedReader(
118 new FileReader(aFile));
119 String firstline = reader.readLine();
120 assertTrue("expected the line to start with '<' but it was '"
121 + firstline + "'.",
122 firstline.startsWith("<"));
123 } catch (IOException ioex) {
124 fail("caught " + ioex);
125 }
126
127 }
128
129 private void verifyFileIsEmpty(File aFile) {
130 assertTrue("could not read the file.",
131 aFile.canRead());
132 assertTrue("the file didn't contain any data.",
133 aFile.length() > 0l);
134 try {
135 BufferedReader reader = new BufferedReader(
136 new FileReader(aFile));
137 String firstline = reader.readLine();
138 assertTrue("expected the line to be empty but it was '"
139 + firstline + "'.",
140 firstline.equals(""));
141 } catch (IOException ioex) {
142 fail("caught " + ioex);
143 }
144
145 }
146
147 }
148