Search in sources :

Example 1 with Renderer

use of org.apache.sling.junit.Renderer in project sling by apache.

the class JUnitHealthCheck method execute.

public Result execute() {
    final String extension = "json";
    final FormattingResultLog resultLog = new FormattingResultLog();
    final CustomRunListener listener = new CustomRunListener(resultLog);
    final Renderer r = new CustomRenderer(listener, extension, resultLog);
    final Collection<String> testNames = testsManager.getTestNames(testSelector);
    if (testNames.isEmpty()) {
        resultLog.warn("No tests found for selector {}", testSelector);
    } else {
        try {
            testsManager.executeTests(testNames, r, testSelector);
            if (listener.nTests == 0) {
                resultLog.warn("No tests executed by {}", testSelector);
            }
        } catch (Exception e) {
            resultLog.warn("Exception while executing tests (" + testSelector + ")" + e);
        }
    }
    return new Result(resultLog);
}
Also used : FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) Renderer(org.apache.sling.junit.Renderer) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Result(org.apache.sling.hc.api.Result)

Example 2 with Renderer

use of org.apache.sling.junit.Renderer in project sling by apache.

the class ServletProcessor method doPost.

/** POST request executes tests */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    final TestSelector selector = getTestSelector(request);
    final boolean forceReload = getForceReloadOption(request);
    log.info("POST request, executing tests: {}, {}={}", new Object[] { selector, FORCE_RELOAD_PARAM, forceReload });
    final Renderer renderer = rendererSelector.getRenderer(selector);
    if (renderer == null) {
        throw new ServletException("No Renderer found for " + selector);
    }
    renderer.setup(response, getClass().getSimpleName());
    final List<String> testNames = getTestNames(selector, forceReload);
    if (testNames.isEmpty()) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "No tests found for " + selector);
    }
    try {
        testsManager.executeTests(testNames, renderer, selector);
    } catch (Exception e) {
        throw new ServletException(e);
    }
    renderer.cleanup();
}
Also used : ServletException(javax.servlet.ServletException) TestSelector(org.apache.sling.junit.TestSelector) Renderer(org.apache.sling.junit.Renderer) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 3 with Renderer

use of org.apache.sling.junit.Renderer in project sling by apache.

the class ServletProcessor method doGet.

/** GET request lists available tests */
public void doGet(final HttpServletRequest request, final HttpServletResponse response, final String servletPath) throws ServletException, IOException {
    final boolean forceReload = getForceReloadOption(request);
    // Redirect to / if called without it, and serve CSS if requested
    {
        final String pi = request.getPathInfo();
        if (pi == null) {
            response.sendRedirect(request.getContextPath() + servletPath + "/");
        } else if (pi.endsWith(CSS)) {
            sendCss(response);
            return;
        }
    }
    final TestSelector selector = getTestSelector(request);
    final List<String> testNames = getTestNames(selector, forceReload);
    // 404 if no tests found
    if (testNames.isEmpty()) {
        final String msg = "WARNING: no test classes found for selector " + selector + ", check the requirements of the active " + "TestsProvider services for how to supply tests.";
        response.sendError(HttpServletResponse.SC_NOT_FOUND, msg);
        return;
    }
    final Renderer renderer = rendererSelector.getRenderer(selector);
    if (renderer == null) {
        throw new ServletException("No Renderer found for " + selector);
    }
    log.debug("GET request: {}", selector);
    renderer.setup(response, getClass().getSimpleName());
    renderer.info("info", "Test selector: " + selector);
    try {
        testsManager.listTests(testNames, renderer);
        final String postPath = getTestExecutionPath(request, selector, renderer.getExtension());
        renderer.link("Execute these tests", postPath, "POST");
    } catch (Exception e) {
        throw new ServletException(e);
    }
    renderer.cleanup();
}
Also used : ServletException(javax.servlet.ServletException) TestSelector(org.apache.sling.junit.TestSelector) Renderer(org.apache.sling.junit.Renderer) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 4 with Renderer

use of org.apache.sling.junit.Renderer in project sling by apache.

the class RendererSelectorImpl method getRenderer.

public Renderer getRenderer(TestSelector selector) {
    if (renderersTracker.getTrackingCount() != renderersTrackerTrackingCount) {
        log.debug("Rebuilding list of {}", Renderer.class.getSimpleName());
        renderersTrackerTrackingCount = renderersTracker.getTrackingCount();
        final ServiceReference[] refs = renderersTracker.getServiceReferences();
        renderers.clear();
        if (refs != null) {
            for (ServiceReference ref : refs) {
                renderers.add((Renderer) bundleContext.getService(ref));
            }
        }
        log.info("List of {} rebuilt: {}", Renderer.class.getSimpleName(), renderers);
    }
    for (Renderer r : renderers) {
        if (r.appliesTo(selector)) {
            if (r instanceof RendererFactory) {
                return ((RendererFactory) r).createRenderer();
            }
            throw new UnsupportedOperationException("Renderers must implement RendererFactory, this one does not:" + r);
        }
    }
    return null;
}
Also used : RendererFactory(org.apache.sling.junit.RendererFactory) Renderer(org.apache.sling.junit.Renderer) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

Renderer (org.apache.sling.junit.Renderer)4 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)2 TestSelector (org.apache.sling.junit.TestSelector)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Result (org.apache.sling.hc.api.Result)1 FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)1 RendererFactory (org.apache.sling.junit.RendererFactory)1 ServiceReference (org.osgi.framework.ServiceReference)1