1 /* 2 * $Id$ 3 * 4 * Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved. 5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6 * 7 * This code is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 only, as 9 * published by the Free Software Foundation. Oracle designates this 10 * particular file as subject to the "Classpath" exception as provided 11 * by Oracle in the LICENSE file that accompanied this code. 12 * 13 * This code is distributed in the hope that it will be useful, but WITHOUT 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16 * version 2 for more details (a copy is included in the LICENSE file that 17 * accompanied this code). 18 * 19 * You should have received a copy of the GNU General Public License version 20 * 2 along with this work; if not, write to the Free Software Foundation, 21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22 * 23 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 24 * or visit www.oracle.com if you need additional information or have any 25 * questions. 26 */ 27 package com.sun.javatest.servlets; 28 29 import java.io.File; 30 import java.io.IOException; 31 import java.io.OutputStreamWriter; 32 import java.io.PrintWriter; 33 import java.nio.charset.StandardCharsets; 34 import java.util.Enumeration; 35 import java.util.Iterator; 36 import java.util.Map; 37 import javax.servlet.ServletException; 38 import javax.servlet.http.HttpServlet; 39 import javax.servlet.http.HttpServletRequest; 40 import javax.servlet.http.HttpServletResponse; 41 import com.sun.javatest.Status; 42 import com.sun.javatest.TestDescription; 43 import com.sun.javatest.TestResult; 44 import com.sun.javatest.util.StringArray; 45 46 public class ResultBrowser extends HttpServlet { 47 public void doGet(HttpServletRequest req, HttpServletResponse res) 48 throws ServletException, IOException { 49 String uri = req.getRequestURI(); 50 File file = new File(req.getRealPath(uri)); 51 52 if (!file.exists()) { 53 res.sendError(HttpServletResponse.SC_NOT_FOUND); 54 return; 55 } 56 57 TestResult tr; 58 try { 59 tr = new TestResult(file); 60 } 61 catch (TestResult.Fault e) { 62 String msg = 63 "File does not appear to be a valid test result file. " + 64 "The following exception was received while trying to open it: " + 65 e.getLocalizedMessage(); 66 res.sendError(HttpServletResponse.SC_NOT_FOUND, msg); 67 return; 68 } 69 70 res.setContentType("text/html"); 71 PrintWriter out = new PrintWriter(new OutputStreamWriter(res.getOutputStream(), StandardCharsets.UTF_8), true); 72 out.println("<html>"); 73 out.println("<head>"); 74 out.println("<title>" + file + "</title>"); 75 out.println("</head>"); 76 out.println("<body>"); 77 78 // header 79 try { 80 TestDescription td = tr.getDescription(); 81 out.println("<h1>Test Results: " + td.getRootRelativeURL() + "</h1>"); 82 } 83 catch (TestResult.Fault e) { 84 out.println("<h1>(Unknown test)</h1>"); 85 } 86 87 String[] colors = {"lime", "red", "yellow", "aqua", null}; 88 // color names are per HTML 3.2 specification; see http://www.w3.org/TR/REC-html32.html 89 String[] outcomes = {"Passed", "Failed", "Check output", "Error", "Not run"}; 90 Status status = tr.getStatus(); 91 String color = colors[status.getType()]; 92 String outcome = outcomes[status.getType()]; 93 out.println("<table cellpadding=5><tr><td" + (color != null ? " bgcolor=" + color : "") +">" + 94 "<b>" + outcome + "</b><td><b>" + status.getReason() + "</b></table>"); 95 96 out.println("<ul>"); 97 out.println("<li><a href=\"#td\">Test Description properties</a>"); 98 out.println("<li><a href=\"#tr\">Test Result properties</a>"); 99 out.println("<li><a href=\"#tr\">Test Environment</a>"); 100 out.println("<li><a href=\"#output\">Test Output</a>"); 101 out.println("<ul>"); 102 for (int i = 0; i < tr.getSectionCount(); i++) { 103 try { 104 TestResult.Section s = tr.getSection(i); 105 out.println("<li><a href=\"#output-" + s.getTitle() + "\">" + s.getTitle() + "</a>"); 106 } 107 catch (TestResult.Fault f) { 108 out.println("The following exception occurred while trying to determine the test description: " + f.getLocalizedMessage()); 109 } 110 } 111 out.println("</ul>"); 112 out.println("</ul>"); 113 out.println("<p><hr>"); 114 115 // test description properties 116 out.println("<h2><a name=td>Test Description properties</a></h2>"); 117 try { 118 TestDescription td = tr.getDescription(); 119 out.println("<table>"); 120 for (Iterator iter = td.getParameterKeys(); iter.hasNext(); ) { 121 String key = (String) (iter.next()); 122 String value = td.getParameter(key); 123 if (key.equals("$root") || key.equals("$file") || key.equals("testsuite") || key.equals("file")) 124 out.println("<tr><td align=top>" + key + "<td><a href=\"" + value + "\">" + filter(value, false) + "</a>"); 125 else if (key.equals("source")) { 126 out.println("<tr><td align=top>" + key + "<td>"); 127 String[] srcs = StringArray.split(value); 128 if (srcs != null) { 129 File tdFile = td.getFile(); 130 String tdFilePath = (tdFile == null ? null : tdFile.getPath()); 131 String tdDir = (tdFilePath == null ? null : tdFilePath.substring(0, tdFilePath.lastIndexOf('/') + 1)); // File.separator? 132 for (int i = 0; i < srcs.length; i++) { 133 if (tdDir == null) 134 out.println(srcs[i]); 135 else 136 out.println("<a href=\"" + tdDir + srcs[i] + "\">" + srcs[i] + "</a>"); 137 } 138 } 139 } 140 else 141 out.println("<tr><td align=top>" + key + "<td>" + filter(value, true)); 142 } 143 out.println("</table>"); 144 } 145 catch (TestResult.Fault e) { 146 out.println("The following exception occurred while trying to determine the test description: " + e.getLocalizedMessage()); 147 } 148 out.println("<p><hr>"); 149 150 // test result properties 151 out.println("<h2><a name=tr>Test Result properties</a></h2>"); 152 try { 153 out.println("<table>"); 154 for (Enumeration e = tr.getPropertyNames(); e.hasMoreElements(); ) { 155 String key = (String)(e.nextElement()); 156 out.println("<tr><td>" + key + "<td>" + filter(tr.getProperty(key), true)); 157 } 158 } 159 catch (TestResult.Fault e) { 160 out.println("The following exception occurred while trying to determine the test result properties: " + e.getLocalizedMessage()); 161 } 162 finally { 163 out.println("</table>"); 164 } 165 out.println("<p><hr>"); 166 167 // test environment 168 out.println("<h2><a name=env>Test Environment</a></h2>"); 169 try { 170 Map env = tr.getEnvironment(); 171 if (env.size() == 0) { 172 out.println("<tr><td>No environment details found"); 173 } 174 else { 175 out.println("<table>"); 176 for (Iterator i = env.entrySet().iterator(); i.hasNext(); ) { 177 Map.Entry e = (Map.Entry) (i.next()); 178 String key = (String) (e.getKey()); 179 String value = (String) (e.getValue()); 180 out.println("<tr><td>" + key + "<td>" + filter(value, true)); 181 } 182 out.println("</table>"); 183 } 184 } 185 catch (TestResult.Fault e) { 186 out.println("The following exception occurred while trying to determine the test environment: " + e.getLocalizedMessage()); 187 } 188 out.println("<p><hr>"); 189 190 // output 191 out.println("<h2><a name=output>Test Output</a></h2>"); 192 193 if (tr.getSectionCount() == 0) 194 out.println("No output recorded."); 195 else { 196 try { 197 for (int i = 0; i < tr.getSectionCount(); i++) { 198 TestResult.Section s = tr.getSection(i); 199 if (i > 0) 200 out.println("<p><hr align=left width=\"25%\">"); 201 out.println("<h3><a name=\"output-" + s.getTitle() + "\">" + s.getTitle() + "</a></h3>"); 202 String[] sects = s.getOutputNames(); 203 for (int j = 0; j < sects.length; j++) { 204 if (!sects[j].equals("messages")) 205 out.println("<h4>Output: " + sects[j] + "</h4>"); 206 String output = s.getOutput(sects[j]); 207 if (output.equals("")) 208 out.println("<em>(No output.)</em>"); 209 out.println("<pre>" + output + "</pre>"); 210 } 211 if (s.getStatus() != null) { 212 out.println("<h4>Status</h4>"); 213 out.println(s.getStatus()); 214 } 215 } // for 216 } // try 217 catch (TestResult.ReloadFault f) { 218 out.println("<b>Internal error while reading test results.</b>"); 219 } 220 } 221 out.println("<p><hr>"); 222 223 // trailer 224 out.println("File: <em>" + file + "</em>"); 225 out.println("</body>"); 226 out.println("</html>"); 227 out.close(); 228 } 229 230 private String filter(String s, boolean newlines) { 231 if (s.indexOf('<') == -1 && s.indexOf('>') == -1 && (!newlines || (s.indexOf('\n') == -1))) 232 return s; 233 else { 234 StringBuffer sb = new StringBuffer(s.length() * 2); 235 for (int i = 0; i < s.length(); i++) { 236 char c; 237 switch (c = s.charAt(i)) { 238 case '<': 239 sb.append("<"); 240 break; 241 case '>': 242 sb.append(">"); 243 break; 244 case '\n': 245 if (newlines) 246 sb.append("<br>"); 247 sb.append(c); 248 break; 249 default: 250 sb.append(c); 251 } 252 } 253 return sb.toString(); 254 } 255 } 256 }