1 package net.jaekl.cfb.analyze;
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
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;
13 import javax.xml.parsers.ParserConfigurationException;
14 import javax.xml.parsers.SAXParser;
15 import javax.xml.parsers.SAXParserFactory;
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;
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;
30 public class HtmlReportTest {
31 private static class CheckWellFormedHandler extends DefaultHandler
34 public void warning(SAXParseException spe) throws SAXException
36 throw new SAXException(spe);
40 public void error(SAXParseException spe) throws SAXException
42 throw new SAXException(spe);
46 public void fatalError(SAXParseException spe) throws SAXException
48 throw new SAXException(spe);
53 BugReportData.getDmDefaultEncoding(),
54 BugReportData.getDmNumberCtor156(),
55 BugReportData.getRcnRedundantNullCheck(),
56 BugReportData.getVoVolatileIncrement()
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";
63 private CfbBundleMock m_bundle;
64 private MessageMapMock m_msgMap;
66 private Analysis analysisFromXml(String xml, String projectName, String version)
67 throws FileNotFoundException, IOException, SAXException
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);
77 private void assertContainsTagExactlyOnce(String html, String tag)
79 int pos = html.indexOf("<" + tag + ">");
81 String substr = html.substring(pos + 1);
82 assertFalse(substr.contains("<" + tag + ">"));
83 assertTrue(substr.contains("</" + tag + ">"));
86 private void checkForWellFormedXml(String xml) throws IOException, ParserConfigurationException, SAXException
88 SAXParserFactory spf = SAXParserFactory.newInstance();
89 spf.setValidating(false);
90 SAXParser parser = spf.newSAXParser();
92 try ( ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(Command.UTF_8)) )
94 InputSource source = new InputSource(bais);
95 CheckWellFormedHandler handler = new CheckWellFormedHandler();
97 parser.parse(source, handler);
101 private String createReport(String firstXml, String secondXml) throws IOException, SAXException
103 Analysis first = null;
104 if (null != firstXml) {
105 first = analysisFromXml(firstXml, PROJECT_NAME, FIRST_VERSION);
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);
113 HtmlReport htmlReport = new HtmlReport(m_bundle, msgColl, delta);
116 StringWriter sw = new StringWriter();
117 PrintWriter pw = new PrintWriter(sw);
120 htmlReport.write(pw);
123 return sw.toString();
127 private void doTestDelta(String firstXml, String secondXml) throws FileNotFoundException, IOException, SAXException, ParserConfigurationException
129 String html = createReport(firstXml, secondXml);
130 validateReport(html);
133 private void validateReport(String html) throws IOException, ParserConfigurationException, SAXException
137 // Report should be well-formed XHTML
138 checkForWellFormedXml(html);
140 // Certain tags should be present exactly once
141 for (String tag : new String[]{"HTML", "HEAD", "BODY", "STYLE"})
143 assertContainsTagExactlyOnce(html, tag);
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>"));
150 // The character set UTF-8 should be specified
151 assertTrue(html.contains("<META CHARSET=\"UTF-8\"/>"));
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>"));
158 public void setUp() throws FileNotFoundException, IOException, SAXException
160 m_bundle = new CfbBundleMock();
161 m_msgMap = new MessageMapMock();
163 try (ByteArrayInputStream bais = new ByteArrayInputStream(MessagesData.getData().getBytes(Command.UTF_8))) {
164 m_msgMap.parse(new InputSource(bais));
169 public void testVariousDeltas() throws FileNotFoundException, IOException, SAXException, ParserConfigurationException
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]);
178 String firstXml = sbFirst.toString() + BugReportData.getEpilogue();
179 String secondXml = sbSecond.toString() + BugReportData.getEpilogue();
181 doTestDelta(firstXml, secondXml);
187 public void testLocalVariable() throws FileNotFoundException, IOException, SAXException, ParserConfigurationException
189 String xml = BugReportData.getPrologue()
190 + BugReportData.getRcnRedundantNullCheck()
191 + BugReportData.getEpilogue();
193 String html = createReport(null, xml);
194 validateReport(html);
196 String expected = "con (LOCAL_VARIABLE_VALUE_OF)";
197 // expected string should be present exactly once
198 int pos = html.indexOf(expected);
200 assertFalse(html.substring(pos + expected.length()).contains(expected));
204 public void testField() throws FileNotFoundException, IOException, SAXException, ParserConfigurationException
206 String xml = BugReportData.getPrologue()
207 + BugReportData.getVoVolatileIncrement()
208 + BugReportData.getEpilogue();
210 String html = createReport(null, xml);
211 validateReport(html);
213 String expected = "junit.extensions.ActiveTestSuite.fActiveTestDeathCount";
215 // expected string should be present exactly once
216 int pos = html.indexOf(expected);
218 assertFalse(html.substring(pos + expected.length()).contains(expected));