/* * Copyright (c) 2004, KNOPFLERFISH project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * - Neither the name of the KNOPFLERFISH project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.knopflerfish.bundle.junit; import java.util.*; import java.net.URL; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import org.osgi.framework.*; import org.osgi.util.tracker.*; import org.osgi.service.http.*; import junit.framework.*; import org.knopflerfish.service.junit.*; public class JUnitServlet extends HttpServlet { static final String ID = "id"; static final String SUBID = "subid"; static final String CMD = "cmd"; static final String FMT = "fmt"; ServiceTracker junitTracker; JUnitServlet() { junitTracker = new ServiceTracker(Activator.bc, JUnitService.class.getName(), null); junitTracker.open(); } JUnitService getJUnitService() { JUnitService ju = (JUnitService)junitTracker.getService(); if(ju == null) { throw new RuntimeException("No JUnitService available"); } return ju; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ClassLoader oldLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); PrintWriter out = response.getWriter(); String fmt = request.getParameter(FMT); String cmd = request.getParameter(CMD); String id = request.getParameter(ID); if(id == null && cmd == null) { cmd = "list"; fmt = "html"; } else { if(cmd == null) { cmd = "run"; } } if("html".equals(fmt)) { response.setContentType ("text/html"); } else { response.setContentType ("text/xml"); } if("html".equals(fmt)) { handleCommandHTML(request, response, cmd, out); } else { handleCommandXML(request, response, cmd, out); } out.flush (); } catch (RuntimeException e) { Activator.log.error("servlet failed ", e); throw e; } finally { Thread.currentThread().setContextClassLoader(oldLoader); } } void handleCommandHTML(HttpServletRequest request, HttpServletResponse response, String cmd, PrintWriter out) throws ServletException, IOException { String id = request.getParameter(ID); String subid = request.getParameter(SUBID); out.println(""); out.println("
"); out.print("");
e.printStackTrace(out);
out.println("");
}
out.println("");
out.println("");
}
void showTestsHTML(HttpServletRequest request,
HttpServletResponse response,
PrintWriter out) throws Exception
{
out.println("");
out.println("# of tests: " + tr.runCount());
out.println("# of failures: " + tr.failureCount());
out.println("# of errors: " + tr.errorCount());
out.println("");
if(tr.failureCount() > 0 ) {
int i = 0;
for(Enumeration e = tr.failures(); e.hasMoreElements(); ) {
i++;
TestFailure tf = (TestFailure)e.nextElement();
dumpFailureHTML(tf, out, "Failure #" + i);
}
}
if(tr.errorCount() > 0 ) {
int i = 0;
for(Enumeration e = tr.errors(); e.hasMoreElements(); ) {
i++;
TestFailure tf = (TestFailure)e.nextElement();
dumpFailureHTML(tf, out, "Error #" + i);
}
}
}
void dumpFailureHTML(TestFailure tf, PrintWriter out,
String prefix) throws IOException {
out.println("");
if(failedTest instanceof TestCase) {
TestCase tc = (TestCase)failedTest;
String name = tc.getName();
if(name == null) {
name = tc.getClass().getName();
}
out.println("Name: " + escape(name) + "
");
out.println("Class: " + tc.getClass().getName() + "");
} else {
out.println("" + tf.failedTest() + "");
}
out.println("
");
out.println("");
out.print(tf.trace());
out.println("");
out.println("