Display LocalVariables along with Locations in 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         
154         @Before
155         public void setUp() throws FileNotFoundException, IOException, SAXException 
156         {
157                 m_bundle = new CfbBundleMock();
158                 m_msgMap = new MessageMapMock();
159                 
160                 try (ByteArrayInputStream bais = new ByteArrayInputStream(MessagesData.getData().getBytes(Command.UTF_8))) {
161                         m_msgMap.parse(new InputSource(bais));
162                 }
163         }
164
165         @Test
166         public void testVariousDeltas() throws FileNotFoundException, IOException, SAXException, ParserConfigurationException 
167         {
168                 StringBuilder sbFirst = new StringBuilder(BugReportData.getPrologue());
169                 StringBuilder sbSecond = new StringBuilder(BugReportData.getPrologue());
170                 for (int i = 0; i < BUG_XMLS.length; ++i) {
171                         sbFirst.append(BUG_XMLS[i]);
172                         for (int j = 0; j < BUG_XMLS.length; ++j) {
173                                 sbSecond.append(BUG_XMLS[j]);
174                                 
175                                 String firstXml = sbFirst.toString() + BugReportData.getEpilogue();
176                                 String secondXml = sbSecond.toString() + BugReportData.getEpilogue();
177                                 
178                                 doTestDelta(firstXml, secondXml);
179                         }
180                 }
181         }
182         
183         @Test
184         public void testLocalVariable() throws FileNotFoundException, IOException, SAXException, ParserConfigurationException 
185         {
186                 String xml = BugReportData.getPrologue()
187                                    + BugReportData.getRcnRedundantNullCheck()
188                                    + BugReportData.getEpilogue();
189
190                 String html = createReport(null, xml);
191                 validateReport(html);
192                 
193                 String expected = "con (LOCAL_VARIABLE_VALUE_OF)";
194                 // expected string should be present exactly once
195                 int pos = html.indexOf(expected);
196                 assertTrue(pos > 0);
197                 assertFalse(html.substring(pos + expected.length()).contains(expected));
198         }
199 }