Search in sources :

Example 1 with ClassWithJavascript

use of org.stjs.generator.ClassWithJavascript 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)

Example 2 with ClassWithJavascript

use of org.stjs.generator.ClassWithJavascript in project st-js by st-js.

the class DependencyTest method testGlobalScopeDep.

@Test
public void testGlobalScopeDep() {
    generate(Dep5.class);
    ClassWithJavascript jsClass = stjsClass(Dep5.class);
    assertNotNull(jsClass);
    assertTrue(!jsClass.getDirectDependencies().isEmpty());
    assertDependency(jsClass.getDirectDependencies(), JSGlobal.class);
}
Also used : ClassWithJavascript(org.stjs.generator.ClassWithJavascript) AbstractStjsTest(org.stjs.generator.utils.AbstractStjsTest) Test(org.junit.Test)

Example 3 with ClassWithJavascript

use of org.stjs.generator.ClassWithJavascript in project st-js by st-js.

the class DependencyTest method testStaticInitializerBlockDep.

@Test
public void testStaticInitializerBlockDep() {
    generate(Dep11b.class);
    generate(Dep11.class);
    ClassWithJavascript jsClass = stjsClass(Dep11.class);
    assertNotNull(jsClass);
    assertDependency(jsClass.getDirectDependencyMap(), Dep11b.class, DependencyType.STATIC);
}
Also used : ClassWithJavascript(org.stjs.generator.ClassWithJavascript) AbstractStjsTest(org.stjs.generator.utils.AbstractStjsTest) Test(org.junit.Test)

Example 4 with ClassWithJavascript

use of org.stjs.generator.ClassWithJavascript in project st-js by st-js.

the class DependencyTest method testStaticInitializerBlockWithInnerClassConstructorDep.

@Test
public void testStaticInitializerBlockWithInnerClassConstructorDep() {
    generate(Dep13b.class);
    generate(Dep13.class);
    ClassWithJavascript jsClass = stjsClass(Dep13.class);
    assertNotNull(jsClass);
    assertDependency(jsClass.getDirectDependencyMap(), Dep13b.class, DependencyType.STATIC);
}
Also used : ClassWithJavascript(org.stjs.generator.ClassWithJavascript) AbstractStjsTest(org.stjs.generator.utils.AbstractStjsTest) Test(org.junit.Test)

Example 5 with ClassWithJavascript

use of org.stjs.generator.ClassWithJavascript in project st-js by st-js.

the class DependencyTest method testStaticDep.

@Test
public void testStaticDep() {
    generate(Dep7s.class);
    generate(Dep7.class);
    ClassWithJavascript jsClass = stjsClass(Dep7.class);
    assertNotNull(jsClass);
    assertDependency(jsClass.getDirectDependencyMap(), Dep7s.class, DependencyType.STATIC);
}
Also used : ClassWithJavascript(org.stjs.generator.ClassWithJavascript) AbstractStjsTest(org.stjs.generator.utils.AbstractStjsTest) Test(org.junit.Test)

Aggregations

ClassWithJavascript (org.stjs.generator.ClassWithJavascript)16 Test (org.junit.Test)12 AbstractStjsTest (org.stjs.generator.utils.AbstractStjsTest)11 File (java.io.File)3 IOException (java.io.IOException)3 URLClassLoader (java.net.URLClassLoader)3 BridgeClass (org.stjs.generator.BridgeClass)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 DependencyResolutionRequiredException (org.apache.maven.artifact.DependencyResolutionRequiredException)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 InclusionScanException (org.codehaus.plexus.compiler.util.scan.InclusionScanException)2 SourceMapping (org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping)2 SuffixMapping (org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping)2 GenerationDirectory (org.stjs.generator.GenerationDirectory)2 Generator (org.stjs.generator.Generator)2 GeneratorConfiguration (org.stjs.generator.GeneratorConfiguration)2 GeneratorConfigurationBuilder (org.stjs.generator.GeneratorConfigurationBuilder)2 JavascriptFileGenerationException (org.stjs.generator.JavascriptFileGenerationException)2