Search in sources :

Example 1 with TestClassAttributes

use of org.stjs.testing.driver.TestClassAttributes in project st-js by st-js.

the class LongPollingBrowser method sendTestFixture.

/**
 * Writes to the HTTP response the HTML and/or javascript code that is necessary for the browser to execute the specified test.
 *
 * @param meth     The test to send to the browser
 * @param exchange contains the HTTP response that must be written to
 */
public void sendTestFixture(MultiTestMethod meth, HttpExchange exchange) throws Exception {
    TestClassAttributes attr = testClasses.getAttributes(meth.getTestClass());
    final Test test = meth.getMethod().getAnnotation(Test.class);
    StringBuilder resp = new StringBuilder(8192);
    resp.append("<html>\n");
    resp.append("<head>\n");
    appendScriptTag(resp, httpPath(new URI("webjar:/stjs.js")));
    appendScriptTag(resp, "/junit.js");
    resp.append("<script language='javascript'>stjs.mainCallDisabled=true;</script>\n");
    // scripts added explicitly
    for (String script : attr.getScripts()) {
        appendScriptTag(resp, script);
    }
    // scripts before - new style
    for (String script : attr.getScriptsBefore()) {
        appendScriptTag(resp, script);
    }
    Set<URI> jsUris = new LinkedHashSet<>();
    for (ClassWithJavascript dep : attr.getDependencies()) {
        if (!attr.getScripts().isEmpty() && dep instanceof BridgeClass) {
            // bridge dependencies are not added when using @Scripts
            System.out.println("WARNING: You're using @Scripts deprecated annotation that disables the automatic inclusion of the Javascript files of " + "the bridges you're using! " + "Please consider using @ScriptsBefore and/or @ScriptsAfter instead.");
            continue;
        }
        for (URI file : dep.getJavascriptFiles()) {
            jsUris.add(file);
        }
    }
    for (URI uri : jsUris) {
        appendScriptTag(resp, httpPath(uri));
    }
    // scripts after - new style
    for (String script : attr.getScriptsAfter()) {
        appendScriptTag(resp, script);
    }
    resp.append("<script language='javascript'>\n");
    if (getConfig().isDebugJavaScript()) {
        resp.append(" function runTest() {\n");
        resp.append("    (elem=document.getElementById('startSection')).parentNode.removeChild(elem);\n");
    } else {
        resp.append("  window.onload=function(){\n");
    }
    // resp.append("    console.error(document.getElementsByTagName('html')[0].innerHTML);\n");
    // Adapter between generated assert (not global) and JS-test-driver assert (which is a
    // set of global methods)
    resp.append("    Assert=window;\n");
    resp.append("    try{\n");
    String testedClassName = attr.getStjsClass().getJavascriptClassName();
    resp.append("        parent.startingTest('" + testedClassName + "', '" + meth.getName() + "');\n");
    resp.append("        var stjsTest = new " + testedClassName + "();\n");
    resp.append("        var stjsResult = 'OK';\n");
    String expectedExceptionConstructor = "null";
    if (test.expected() != Test.None.class) {
        ClassWithJavascript exceptionClass = getConfig().getStjsClassResolver().resolve(test.expected().getName());
        expectedExceptionConstructor = exceptionClass.getJavascriptClassName();
    }
    resp.append("        var expectedException = " + expectedExceptionConstructor + ";\n");
    // call before methods
    for (FrameworkMethod beforeMethod : attr.getBeforeMethods()) {
        resp.append("      stjsTest." + beforeMethod.getName() + "();\n");
    }
    // call the test's method
    resp.append("      stjsTest." + meth.getName() + "();\n");
    resp.append("      if(expectedException){\n");
    resp.append("        stjsResult = 'Expected an exception, but none was thrown';\n");
    resp.append("      }\n");
    resp.append("    }catch(ex){\n");
    // an exception was caught while executing the test method
    resp.append("      if(!expectedException){\n");
    resp.append("        stjsResult = ex;\n");
    resp.append("      } else if (!stjs.isInstanceOf(ex.constructor,expectedException)){\n");
    resp.append("        stjsResult = ex;\n");
    resp.append("      }\n");
    resp.append("    }finally{\n");
    // call after methods
    for (FrameworkMethod afterMethod : attr.getAfterMethods()) {
        resp.append("     stjsTest." + afterMethod.getName() + "();\n");
    }
    resp.append("      parent.reportResultAndRunNextTest(stjsResult, stjsResult.location);\n");
    resp.append("     }\n");
    resp.append("  }\n");
    resp.append("</script>\n");
    resp.append("</head>\n");
    resp.append("<body>\n");
    if (getConfig().isDebugJavaScript()) {
        resp.append("<div id='startSection'>\n");
        resp.append("  <h2>JavaScript debugging mode</h2>\n");
        resp.append("  <ul>\n");
        resp.append("    <li>Open your developer tools</li>\n");
        resp.append("    <li>Setup your breakpoints and debugging options</li>\n");
        resp.append("    <li>Start the test</li>\n");
        resp.append("  </ul>\n");
        resp.append("  <button onclick='runTest()'>\n");
        resp.append("    Start " + attr.getStjsClass().getJavaClass().getSimpleName() + "." + meth.getName() + "\n");
        resp.append("  </button>\n");
        resp.append("</div>\n");
    }
    if (attr.getHtmlFixture() != null) {
        if (!Strings.isNullOrEmpty(attr.getHtmlFixture().value())) {
            resp.append(attr.getHtmlFixture().value());
        } else if (!Strings.isNullOrEmpty(attr.getHtmlFixture().url())) {
            StringWriter writer = new StringWriter();
            getConfig().getResource(attr.getHtmlFixture().url()).copyTo(writer);
            resp.append(writer.toString());
        }
    }
    resp.append("</body>\n");
    resp.append("</html>\n");
    sendResponse(resp.toString(), exchange);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BridgeClass(org.stjs.generator.BridgeClass) TestClassAttributes(org.stjs.testing.driver.TestClassAttributes) StringWriter(java.io.StringWriter) Test(org.junit.Test) ClassWithJavascript(org.stjs.generator.ClassWithJavascript) URI(java.net.URI) FrameworkMethod(org.junit.runners.model.FrameworkMethod)

Aggregations

StringWriter (java.io.StringWriter)1 URI (java.net.URI)1 LinkedHashSet (java.util.LinkedHashSet)1 Test (org.junit.Test)1 FrameworkMethod (org.junit.runners.model.FrameworkMethod)1 BridgeClass (org.stjs.generator.BridgeClass)1 ClassWithJavascript (org.stjs.generator.ClassWithJavascript)1 TestClassAttributes (org.stjs.testing.driver.TestClassAttributes)1