/*
* $Id$
*
* Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.javatest.servlets;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.javatest.Status;
import com.sun.javatest.TestDescription;
import com.sun.javatest.TestResult;
import com.sun.javatest.util.StringArray;
public class ResultBrowser extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String uri = req.getRequestURI();
File file = new File(req.getRealPath(uri));
if (!file.exists()) {
res.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
TestResult tr;
try {
tr = new TestResult(file);
}
catch (TestResult.Fault e) {
String msg =
"File does not appear to be a valid test result file. " +
"The following exception was received while trying to open it: " +
e.getLocalizedMessage();
res.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
return;
}
res.setContentType("text/html");
PrintWriter out = new PrintWriter(new OutputStreamWriter(res.getOutputStream(), StandardCharsets.UTF_8), true);
out.println("");
out.println("
");
out.println("" + file + "");
out.println("");
out.println("");
// header
try {
TestDescription td = tr.getDescription();
out.println("Test Results: " + td.getRootRelativeURL() + "
");
}
catch (TestResult.Fault e) {
out.println("(Unknown test)
");
}
String[] colors = {"lime", "red", "yellow", "aqua", null};
// color names are per HTML 3.2 specification; see http://www.w3.org/TR/REC-html32.html
String[] outcomes = {"Passed", "Failed", "Check output", "Error", "Not run"};
Status status = tr.getStatus();
String color = colors[status.getType()];
String outcome = outcomes[status.getType()];
out.println("" +
"" + outcome + " | " + status.getReason() + " |
");
out.println("");
out.println("- Test Description properties");
out.println("
- Test Result properties");
out.println("
- Test Environment");
out.println("
- Test Output");
out.println("
");
for (int i = 0; i < tr.getSectionCount(); i++) {
try {
TestResult.Section s = tr.getSection(i);
out.println("- " + s.getTitle() + "");
}
catch (TestResult.Fault f) {
out.println("The following exception occurred while trying to determine the test description: " + f.getLocalizedMessage());
}
}
out.println("
");
out.println("
");
out.println("
");
// test description properties
out.println("");
try {
TestDescription td = tr.getDescription();
out.println("");
for (Iterator iter = td.getParameterKeys(); iter.hasNext(); ) {
String key = (iter.next());
String value = td.getParameter(key);
if (key.equals("$root") || key.equals("$file") || key.equals("testsuite") || key.equals("file"))
out.println("" + key + " | " + filter(value, false) + "");
else if (key.equals("source")) {
out.println(" |
" + key + " | ");
String[] srcs = StringArray.split(value);
if (srcs != null) {
File tdFile = td.getFile();
String tdFilePath = (tdFile == null ? null : tdFile.getPath());
String tdDir = (tdFilePath == null ? null : tdFilePath.substring(0, tdFilePath.lastIndexOf('/') + 1)); // File.separator?
for (int i = 0; i < srcs.length; i++) {
if (tdDir == null)
out.println(srcs[i]);
else
out.println("" + srcs[i] + "");
}
}
}
else
out.println(" |
" + key + " | " + filter(value, true));
}
out.println(" |
");
}
catch (TestResult.Fault e) {
out.println("The following exception occurred while trying to determine the test description: " + e.getLocalizedMessage());
}
out.println("
");
// test result properties
out.println("");
try {
out.println("");
for (Enumeration e = tr.getPropertyNames(); e.hasMoreElements(); ) {
String key = (e.nextElement());
out.println("" + key + " | " + filter(tr.getProperty(key), true));
}
}
catch (TestResult.Fault e) {
out.println("The following exception occurred while trying to determine the test result properties: " + e.getLocalizedMessage());
}
finally {
out.println(" |
");
}
out.println("
");
// test environment
out.println("");
try {
Map env = tr.getEnvironment();
if (env.size() == 0) {
out.println("No environment details found");
}
else {
out.println("");
for (Iterator> i = env.entrySet().iterator(); i.hasNext(); ) {
Map.Entry e = (i.next());
String key = (e.getKey());
String value = (e.getValue());
out.println("" + key + " | " + filter(value, true));
}
out.println(" | ");
}
}
catch (TestResult.Fault e) {
out.println("The following exception occurred while trying to determine the test environment: " + e.getLocalizedMessage());
}
out.println(" ");
// output
out.println("");
if (tr.getSectionCount() == 0)
out.println("No output recorded.");
else {
try {
for (int i = 0; i < tr.getSectionCount(); i++) {
TestResult.Section s = tr.getSection(i);
if (i > 0)
out.println(" ");
out.println("");
String[] sects = s.getOutputNames();
for (int j = 0; j < sects.length; j++) {
if (!sects[j].equals("messages"))
out.println("Output: " + sects[j] + "");
String output = s.getOutput(sects[j]);
if (output.equals(""))
out.println("(No output.)");
out.println("" + output + " ");
}
if (s.getStatus() != null) {
out.println("Status");
out.println(s.getStatus());
}
} // for
} // try
catch (TestResult.ReloadFault f) {
out.println("Internal error while reading test results.");
}
}
out.println(" ");
// trailer
out.println("File: " + file + "");
out.println("");
out.println("");
out.close();
}
private String filter(String s, boolean newlines) {
if (s.indexOf('<') == -1 && s.indexOf('>') == -1 && (!newlines || (s.indexOf('\n') == -1)))
return s;
else {
StringBuffer sb = new StringBuffer(s.length() * 2);
for (int i = 0; i < s.length(); i++) {
char c;
switch (c = s.charAt(i)) {
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
case '\n':
if (newlines)
sb.append(" ");
sb.append(c);
break;
default:
sb.append(c);
}
}
return sb.toString();
}
}
}
|