adds footer to the HTML report.
[cfb.git] / test / net / jaekl / cfb / analyze / HtmlReportTest.java
1 package net.jaekl.cfb.analyze;
2
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6
7 import java.io.ByteArrayInputStream;
8 import java.io.FileNotFoundException;
9 import java.io.IOException;
10 import java.io.PrintWriter;
11 import java.io.StringWriter;
12
13 import javax.xml.parsers.ParserConfigurationException;
14 import javax.xml.parsers.SAXParser;
15 import javax.xml.parsers.SAXParserFactory;
16
17 import net.jaekl.cfb.CfbBundle;
18 import net.jaekl.cfb.CfbBundleMock;
19 import net.jaekl.cfb.util.Command;
20 import net.jaekl.cfb.xml.messages.MessageCollection;
21 import net.jaekl.cfb.xml.messages.MessagesData;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.xml.sax.InputSource;
26 import org.xml.sax.SAXException;
27 import org.xml.sax.SAXParseException;
28 import org.xml.sax.helpers.DefaultHandler;
29
30 public class HtmlReportTest {
31         private static class CheckWellFormedHandler extends DefaultHandler 
32         {
33                 @Override
34                 public void warning(SAXParseException spe) throws SAXException 
35                 {
36                         throw new SAXException(spe);
37                 }
38                 
39                 @Override
40                 public void error(SAXParseException spe) throws SAXException 
41                 {
42                         throw new SAXException(spe);
43                 }
44                 
45                 @Override 
46                 public void fatalError(SAXParseException spe) throws SAXException
47                 {
48                         throw new SAXException(spe);
49                 }
50         }
51         
52         String[] BUG_XMLS = { 
53                 BugReportData.getDmDefaultEncoding(),
54                 BugReportData.getDmNumberCtor156(),
55                 BugReportData.getRcnRedundantNullCheck(),
56                 BugReportData.getVoVolatileIncrement()
57         };
58
59         private static final String PROJECT_NAME = "Project Name";
60         private static final String FIRST_VERSION = "1.0.1.3145";
61         private static final String SECOND_VERSION = "1.0.1.3146";
62
63         private CfbBundleMock m_bundle;
64         private MessageMapMock m_msgMap;
65         
66         private Analysis analysisFromXml(String xml, String projectName, String version) 
67                         throws FileNotFoundException, IOException, SAXException 
68         {
69                 Analysis analysis = new Analysis(projectName, version);
70                 ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(Command.UTF_8));
71                 InputSource inputSource = new InputSource(bais); 
72                 analysis.parse(inputSource);
73                 
74                 return analysis;
75         }
76         
77         private void assertContainsTagExactlyOnce(String html, String tag)
78         {
79                 int pos = html.indexOf("<" + tag + ">");
80                 assertTrue(pos >= 0);
81                 String substr = html.substring(pos + 1);
82                 assertFalse(substr.contains("<" + tag + ">"));
83                 assertTrue(substr.contains("</" + tag + ">"));
84         }
85         
86         private void checkForWellFormedXml(String xml) throws IOException, ParserConfigurationException, SAXException
87         {
88                 SAXParserFactory spf = SAXParserFactory.newInstance();
89                 spf.setValidating(false);
90                 SAXParser parser = spf.newSAXParser();
91                 
92                 try ( ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(Command.UTF_8)) )
93                 {
94                         InputSource source = new InputSource(bais);
95                         CheckWellFormedHandler handler = new CheckWellFormedHandler();
96                         
97                         parser.parse(source, handler);
98                 }
99         }
100         
101         private String createReport(String firstXml, String secondXml) throws IOException, SAXException
102         {
103                 Analysis first = null;
104                 if (null != firstXml) {
105                         first = analysisFromXml(firstXml, PROJECT_NAME, FIRST_VERSION);
106                 }
107                 
108                 Analysis second = analysisFromXml(secondXml, PROJECT_NAME, SECOND_VERSION);
109                 Delta delta = new Delta(first, second);
110                 MessageCollection msgColl = m_msgMap.getColl();
111                 assertNotNull(msgColl);
112                 
113                 HtmlReport htmlReport = new HtmlReport(m_bundle, msgColl, delta);
114
115                 try (
116                                 StringWriter sw = new StringWriter();
117                                 PrintWriter pw = new PrintWriter(sw);
118                         )
119                 {
120                         htmlReport.write(pw);
121                         pw.close();
122                         sw.close();
123                         return sw.toString();
124                 }               
125         }
126
127         private void doTestDelta(String firstXml, String secondXml) throws FileNotFoundException, IOException, SAXException, ParserConfigurationException 
128         {
129                 String html = createReport(firstXml, secondXml);
130                 validateReport(html);
131         }
132         
133         private void validateReport(String html) throws IOException, ParserConfigurationException, SAXException 
134         {
135                 assertNotNull(html);
136                 
137                 // Report should be well-formed XHTML
138                 checkForWellFormedXml(html);
139                 
140                 // Certain tags should be present exactly once
141                 for (String tag : new String[]{"HTML", "HEAD", "BODY", "STYLE"}) 
142                 {
143                         assertContainsTagExactlyOnce(html, tag);
144                 }
145                 
146                 // Title should be the result of translating CFB_REPORT
147                 String title = m_bundle.get(CfbBundle.CFB_REPORT);
148                 assertTrue(html.contains("<TITLE>" + title + "</TITLE>"));
149                                 
150                 // The character set UTF-8 should be specified
151                 assertTrue(html.contains("<META CHARSET=\"UTF-8\"/>"));         
152                 
153                 // The footer should be present
154                 assertTrue(html.contains("DIV CLASS=\"Footer\">Report generated by <A HREF=\"https://jaekl.net/cfb/\">CFB</A></DIV>"));
155         }
156         
157         @Before
158         public void setUp() throws FileNotFoundException, IOException, SAXException 
159         {
160                 m_bundle = new CfbBundleMock();
161                 m_msgMap = new MessageMapMock();
162                 
163                 try (ByteArrayInputStream bais = new ByteArrayInputStream(MessagesData.getData().getBytes(Command.UTF_8))) {
164                         m_msgMap.parse(new InputSource(bais));
165                 }
166         }
167
168         @Test
169         public void testVariousDeltas() throws FileNotFoundException, IOException, SAXException, ParserConfigurationException 
170         {
171                 StringBuilder sbFirst = new StringBuilder(BugReportData.getPrologue());
172                 StringBuilder sbSecond = new StringBuilder(BugReportData.getPrologue());
173                 for (int i = 0; i < BUG_XMLS.length; ++i) {
174                         sbFirst.append(BUG_XMLS[i]);
175                         for (int j = 0; j < BUG_XMLS.length; ++j) {
176                                 sbSecond.append(BUG_XMLS[j]);
177                                 
178                                 String firstXml = sbFirst.toString() + BugReportData.getEpilogue();
179                                 String secondXml = sbSecond.toString() + BugReportData.getEpilogue();
180                                 
181                                 doTestDelta(firstXml, secondXml);
182                         }
183                 }
184         }
185         
186         @Test
187         public void testLocalVariable() throws FileNotFoundException, IOException, SAXException, ParserConfigurationException 
188         {
189                 String xml = BugReportData.getPrologue()
190                                    + BugReportData.getRcnRedundantNullCheck()
191                                    + BugReportData.getEpilogue();
192
193                 String html = createReport(null, xml);
194                 validateReport(html);
195                 
196                 String expected = "con (LOCAL_VARIABLE_VALUE_OF)";
197                 // expected string should be present exactly once
198                 int pos = html.indexOf(expected);
199                 assertTrue(pos > 0);
200                 assertFalse(html.substring(pos + expected.length()).contains(expected));
201         }
202         
203         @Test
204         public void testField() throws FileNotFoundException, IOException, SAXException, ParserConfigurationException 
205         {
206                 String xml = BugReportData.getPrologue()
207                                    + BugReportData.getVoVolatileIncrement()
208                                    + BugReportData.getEpilogue();
209
210                 String html = createReport(null, xml);
211                 validateReport(html);
212                 
213                 String expected = "junit.extensions.ActiveTestSuite.fActiveTestDeathCount";
214                 
215                 // expected string should be present exactly once
216                 int pos = html.indexOf(expected);
217                 assertTrue(pos > 0);
218                 assertFalse(html.substring(pos + expected.length()).contains(expected));
219         }
220
221 }