View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.surefire.report;
20  
21  import java.io.File;
22  import java.io.StringWriter;
23  
24  import junit.framework.TestCase;
25  import org.apache.maven.doxia.module.xhtml5.Xhtml5Sink;
26  import org.apache.maven.doxia.sink.Sink;
27  import org.apache.maven.doxia.tools.SiteTool;
28  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
29  import org.apache.maven.plugin.surefire.log.api.NullConsoleLogger;
30  import org.codehaus.plexus.i18n.DefaultI18N;
31  
32  import static java.util.Collections.singletonList;
33  import static org.apache.maven.plugins.surefire.report.Utils.toSystemNewLine;
34  import static org.hamcrest.CoreMatchers.containsString;
35  import static org.hamcrest.MatcherAssert.assertThat;
36  
37  /**
38   * Prevent fom NPE if failure type and message is null however detail presents.
39   */
40  public class Surefire597Test extends TestCase {
41      @SuppressWarnings("checkstyle:linelength")
42      public void testCorruptedTestCaseFailureWithMissingErrorTypeAndMessage() throws Exception {
43          File basedir = new File(".").getCanonicalFile();
44          File report = new File(basedir, "target/test-classes/surefire-597");
45          StringWriter writer = new StringWriter();
46          Sink sink = new Xhtml5Sink(writer) {};
47          DefaultI18N i18n = new DefaultI18N();
48          i18n.initialize();
49          ConsoleLogger consoleLogger = new NullConsoleLogger();
50          SurefireReportRenderer r = new SurefireReportRenderer(
51                  sink, i18n, "surefire", SiteTool.DEFAULT_LOCALE, consoleLogger, true, singletonList(report), null);
52          r.render();
53          String xml = writer.toString();
54          assertThat(
55                  xml,
56                  containsString(toSystemNewLine("<table border=\"0\" class=\"bodyTable\">\n"
57                          + "<tr class=\"a\">\n"
58                          + "<th>Tests</th>\n"
59                          + "<th>Errors</th>\n"
60                          + "<th>Failures</th>\n"
61                          + "<th>Skipped</th>\n"
62                          + "<th>Success Rate</th>\n"
63                          + "<th>Time</th></tr>\n"
64                          + "<tr class=\"b\">\n"
65                          + "<td align=\"left\">1</td>\n"
66                          + "<td>1</td>\n"
67                          + "<td>0</td>\n"
68                          + "<td>0</td>\n"
69                          + "<td>0%</td>\n"
70                          + "<td>0 s</td>"
71                          + "</tr>"
72                          + "</table>")));
73          assertThat(
74                  xml,
75                  containsString(toSystemNewLine("<table border=\"0\" class=\"bodyTable\">\n"
76                          + "<tr class=\"a\">\n"
77                          + "<th>Package</th>\n"
78                          + "<th>Tests</th>\n"
79                          + "<th>Errors</th>\n"
80                          + "<th>Failures</th>\n"
81                          + "<th>Skipped</th>\n"
82                          + "<th>Success Rate</th>\n"
83                          + "<th>Time</th></tr>\n"
84                          + "<tr class=\"b\">\n"
85                          + "<td align=\"left\"><a href=\"#surefire\">surefire</a></td>\n"
86                          + "<td>1</td>\n"
87                          + "<td>1</td>\n"
88                          + "<td>0</td>\n"
89                          + "<td>0</td>\n"
90                          + "<td>0%</td>\n"
91                          + "<td>0 s</td></tr></table>")));
92          assertThat(
93                  xml,
94                  containsString(toSystemNewLine("<table border=\"0\" class=\"bodyTable\">\n"
95                          + "<tr class=\"a\">\n"
96                          + "<th>-</th>\n"
97                          + "<th>Class</th>\n"
98                          + "<th>Tests</th>\n"
99                          + "<th>Errors</th>\n"
100                         + "<th>Failures</th>\n"
101                         + "<th>Skipped</th>\n"
102                         + "<th>Success Rate</th>\n"
103                         + "<th>Time</th></tr>\n"
104                         + "<tr class=\"b\">\n"
105                         + "<td align=\"left\"><a href=\"#surefire.MyTest\"><img src=\"images/icon_error_sml.gif\" alt=\"\" /></a></td>\n"
106                         + "<td><a href=\"#surefire.MyTest\">MyTest</a></td>\n"
107                         + "<td>1</td>\n"
108                         + "<td>1</td>\n"
109                         + "<td>0</td>\n"
110                         + "<td>0</td>\n"
111                         + "<td>0%</td>\n"
112                         + "<td>0 s</td></tr></table>")));
113         assertThat(
114                 xml,
115                 containsString(toSystemNewLine("<table border=\"0\" class=\"bodyTable\">\n"
116                         + "<tr class=\"a\">\n"
117                         + "<td align=\"left\"><img src=\"images/icon_error_sml.gif\" alt=\"\" /></td>\n"
118                         + "<td><a id=\"surefire.MyTest.test\"></a>test</td></tr>\n"
119                         + "<tr class=\"b\">\n"
120                         + "<td align=\"left\">-</td>\n"
121                         + "<td>java.lang.RuntimeException: java.lang.IndexOutOfBoundsException: msg</td></tr>\n"
122                         + "<tr class=\"a\">\n"
123                         + "<td align=\"left\">-</td>\n"
124                         + "<td>\n"
125                         + "<div id=\"test-error\">surefire.MyTest:13</div></td></tr></table>")));
126     }
127 }