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.chemistry.opencmis.tck.report;
20  
21  import java.io.IOException;
22  import java.io.Writer;
23  import java.util.Date;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.TreeMap;
27  
28  import org.apache.chemistry.opencmis.commons.SessionParameter;
29  import org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException;
30  import org.apache.chemistry.opencmis.tck.CmisTest;
31  import org.apache.chemistry.opencmis.tck.CmisTestGroup;
32  import org.apache.chemistry.opencmis.tck.CmisTestResult;
33  import org.apache.chemistry.opencmis.tck.CmisTestResultStatus;
34  import org.apache.chemistry.opencmis.tck.runner.AbstractRunner;
35  
36  /**
37   * HTML Report without header and footer.
38   */
39  public class CoreHtmlReport extends AbstractCmisTestReport {
40  
41      private int stackTraceCounter;
42      private String revision;
43  
44      public CoreHtmlReport() {
45      }
46  
47      @Override
48      public void createReport(Map<String, String> parameters, List<CmisTestGroup> groups, Writer writer)
49              throws IOException {
50          stackTraceCounter = 0;
51          if (parameters != null) {
52              revision = parameters.get(AbstractRunner.TCK_REVISION_PARAMETER);
53          }
54  
55          writer.write("<h1>OpenCMIS TCK Report</h1>\n");
56          writer.write((new Date()) + "\n");
57  
58          writer.write("\n<h2>Parameters</h2>\n");
59  
60          if (parameters != null) {
61              writer.write("<table>\n");
62              for (Map.Entry<String, String> p : (new TreeMap<String, String>(parameters)).entrySet()) {
63                  String value = p.getValue();
64                  if (SessionParameter.PASSWORD.endsWith(p.getKey())) {
65                      value = "*****";
66                  }
67  
68                  writer.write("<tr><td>" + escape(p.getKey()) + "</td><td>" + escape(value) + "</td></tr>\n");
69              }
70              writer.write("</table>\n");
71          }
72  
73          writer.write("\n<h2>Groups</h2>\n");
74  
75          if (groups != null) {
76              for (CmisTestGroup group : groups) {
77                  printGroupResults(group, writer);
78              }
79          }
80  
81          writer.flush();
82      }
83  
84      public static void printStyle(Writer writer) throws IOException {
85          writer.write("<style type=\"text/css\">\n");
86          writer.write("body { font-family: sans-serif; }\n");
87          writer.write(".tckResultINFO { margin-left: 10px; margin-right: 10px; padding: 2px; }\n");
88          writer.write(".tckResultSKIPPED { margin-left: 10px; margin-right: 10px; padding: 2px; background-color: #FFFFFF; }\n");
89          writer.write(".tckResultOK { margin-left: 10px; margin-right: 5px; padding: 2px; background-color: #00FF00; }\n");
90          writer.write(".tckResultWARNING { margin-left: 10px; margin-right: 10px; padding: 2px; background-color: #FFFF00; }\n");
91          writer.write(".tckResultFAILURE { margin-left: 10px; margin-right: 10px; padding: 2px; background-color: #FF6000; }\n");
92          writer.write(".tckResultUNEXPECTED_EXCEPTION { margin-left: 10px; margin-right: 10px; padding: 2px; background-color: #FF0000; }\n");
93          writer.write(".tckTraceLink { cursor: pointer; text-decoration: underline; }\n");
94          writer.write(".tckTrace { margin-left: 10px; margin-right: 10px; padding: 2px; border:2px solid #777777; background-color: #DDDDDD; }\n");
95          writer.write("</style>\n");
96      }
97  
98      public static void printJavaScript(Writer writer) throws IOException {
99          writer.write("<script type=\"text/javascript\">\n");
100         writer.write("function tckToggleDisplay(id) {\n");
101         writer.write("(function(style) { style.display = style.display === 'none' ? '' : 'none'; })(document.getElementById(id).style);\n");
102         writer.write("}\n");
103         writer.write("</script>\n");
104     }
105 
106     private void printGroupResults(CmisTestGroup group, Writer writer) throws IOException {
107         if (!group.isEnabled()) {
108             return;
109         }
110 
111         writer.write("\n<hr>\n<h3>" + escape(group.getName()) + "</h3>\n");
112 
113         if (group.getDescription() != null) {
114             writer.write("\n<p><i>" + escape(group.getDescription()) + "</i></p>\n");
115         }
116 
117         if (group.getTests() != null) {
118             for (CmisTest test : group.getTests()) {
119                 printTestResults(test, writer);
120             }
121         }
122     }
123 
124     private void printTestResults(CmisTest test, Writer writer) throws IOException {
125         if (!test.isEnabled()) {
126             return;
127         }
128 
129         writer.write("\n<h4>" + escape(test.getName()) + " (" + test.getTime() + " ms)</h4>\n");
130 
131         if (test.getDescription() != null) {
132             writer.write("\n<p><i>" + escape(test.getDescription()) + "</i></p>\n");
133         }
134 
135         if (test.getResults() != null) {
136             for (CmisTestResult result : test.getResults()) {
137                 writer.write("<div style=\"padding: 5px;\">\n");
138                 printResult(result, writer);
139                 writer.write("</div>\n");
140             }
141         }
142     }
143 
144     private void printResult(CmisTestResult result, Writer writer) throws IOException {
145         stackTraceCounter++;
146         String stackTraceId = "tckTrace" + stackTraceCounter;
147         String exceptionId = "tckException" + stackTraceCounter;
148 
149         boolean hasStackTrace = result.getStackTrace() != null && result.getStackTrace().length > 0;
150         boolean hasException = result.getStatus() == CmisTestResultStatus.UNEXPECTED_EXCEPTION
151                 && result.getException() != null;
152 
153         writer.write("<div class=\"tckResult" + result.getStatus().name() + "\">\n");
154 
155         writer.write("<b>" + result.getStatus() + "</b>: " + escape(result.getMessage()));
156 
157         if (hasStackTrace) {
158             writer.write(" (" + getSourceCodeLink(result.getStackTrace()[0], revision) + ")");
159             writer.write(" [<span class=\"tckTraceLink\" onClick=\"tckToggleDisplay('" + stackTraceId
160                     + "');\">stacktrace</span>]");
161         }
162 
163         if (hasException) {
164             writer.write(" [<span class=\"tckTraceLink\" onClick=\"tckToggleDisplay('" + exceptionId
165                     + "');\">exception details</span>]");
166         }
167 
168         writer.write("<br/>\n");
169 
170         if (hasStackTrace) {
171             writer.write("<div class=\"tckTrace\" id=\"" + stackTraceId + "\" style=\"display:none\">\n");
172 
173             for (StackTraceElement ste : result.getStackTrace()) {
174                 if (AbstractRunner.class.getName().equals(ste.getClassName())) {
175                     break;
176                 }
177                 writer.write(ste.getClassName() + "." + ste.getMethodName() + "(" + getSourceCodeLink(ste, revision)
178                         + ")<br/>\n");
179             }
180 
181             writer.write("</div>\n");
182         }
183 
184         if (hasException) {
185             writer.write("<div class=\"tckTrace\" id=\"" + exceptionId + "\" style=\"display:none\">\n");
186             writer.write("<b>Exception stack trace:</b><br/><br/>\n");
187 
188             for (StackTraceElement ste : result.getException().getStackTrace()) {
189                 if (AbstractRunner.class.getName().equals(ste.getClassName())) {
190                     break;
191                 }
192                 writer.write(ste.getClassName() + "." + ste.getMethodName() + "(" + getSourceCodeLink(ste, revision)
193                         + ")<br/>\n");
194             }
195 
196             if (result.getException() instanceof CmisBaseException) {
197                 CmisBaseException cbe = (CmisBaseException) result.getException();
198                 if (cbe.getErrorContent() != null) {
199                     writer.write("<br/>\n<b>Error content:</b><br/><br/><pre>\n");
200                     writer.write(escape(cbe.getErrorContent()) + "</pre>\n");
201                 }
202             }
203 
204             writer.write("</div>\n");
205         }
206 
207         for (CmisTestResult child : result.getChildren()) {
208             printResult(child, writer);
209         }
210 
211         writer.write("</div>\n");
212     }
213 
214     protected String getSourceCodeLink(StackTraceElement ste, String revision) {
215         StringBuilder result = new StringBuilder(1024);
216 
217         if (!ste.getClassName().startsWith("org.apache.chemistry.opencmis.tck.")) {
218             result.append(escape(ste.getFileName()));
219             if (ste.getLineNumber() > 0) {
220                 result.append(':');
221                 result.append(ste.getLineNumber());
222             }
223         } else {
224             result.append("<a href=\"https://svn.apache.org/viewvc/chemistry/opencmis/trunk/"
225                     + "chemistry-opencmis-test/chemistry-opencmis-test-tck/src/main/java/");
226             result.append(ste.getClassName().replaceAll("\\.", "/"));
227             result.append(".java?view=markup");
228             if (revision != null) {
229                 result.append("&revision=");
230                 result.append(revision);
231             }
232             if (ste.getLineNumber() > 0) {
233                 result.append("#l");
234                 result.append(ste.getLineNumber());
235             }
236 
237             result.append("\" target=\"_blank\">");
238 
239             result.append(escape(ste.getFileName()));
240             if (ste.getLineNumber() > 0) {
241                 result.append(':');
242                 result.append(ste.getLineNumber());
243             }
244 
245             result.append("</a>");
246         }
247         return result.toString();
248     }
249 
250     protected String escape(String s) {
251         if (s == null) {
252             return "";
253         }
254 
255         StringBuilder sb = new StringBuilder(s.length() + 32);
256 
257         for (int i = 0; i < s.length(); i++) {
258             char c = s.charAt(i);
259             switch (c) {
260             case '\"':
261                 sb.append("&quot;");
262                 break;
263             case '&':
264                 sb.append("&amp;");
265                 break;
266             case '<':
267                 sb.append("&lt;");
268                 break;
269             case '>':
270                 sb.append("&gt;");
271                 break;
272             default:
273                 sb.append(c);
274             }
275         }
276 
277         return sb.toString();
278     }
279 }