Search in sources :

Example 1 with JSR223ScriptMacroParameters

use of org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters in project xwiki-platform by xwiki.

the class ClassLoadingTest method testExtraJarLocatedAtURL.

@Test
public void testExtraJarLocatedAtURL() throws Exception {
    // Use a dummy JAR to verify that some passed URL is indeed added to the CL. That JAR only contains
    // an empty Dummy class.
    JSR223ScriptMacroParameters params = new JSR223ScriptMacroParameters();
    params.setJars(getClass().getClassLoader().getResource("dummy.jar").toString());
    // The test would fail after the next line if the Dummy class wasn't present in the classloader used to
    // execute the script.
    this.macro.execute(params, "def var = new Dummy()", this.context);
}
Also used : JSR223ScriptMacroParameters(org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters) Test(org.junit.Test)

Example 2 with JSR223ScriptMacroParameters

use of org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters in project xwiki-platform by xwiki.

the class ClassLoadingTest method testDefineClassInFirstExecutionAndJarParamsInAnother.

/**
 * Verify that it works if we define an interface in a first macro execution and then define a second
 * macro execution which uses the defined interface and also has jar params different from the first
 * macro execution. Also test if a third execution with no params but using a previously defined
 * interface also works.
 */
@Test
public void testDefineClassInFirstExecutionAndJarParamsInAnother() throws Exception {
    this.macro.execute(new JSR223ScriptMacroParameters(), "class MyClass {}", this.context);
    // Second execution: use the defined class but with different jar params
    JSR223ScriptMacroParameters params = new JSR223ScriptMacroParameters();
    params.setJars(getClass().getClassLoader().getResource("dummy.jar").toString());
    this.macro.execute(params, "def var = new MyClass()", this.context);
    // Third execution without
    this.macro.execute(new JSR223ScriptMacroParameters(), "def var = new MyClass()", this.context);
}
Also used : JSR223ScriptMacroParameters(org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters) Test(org.junit.Test)

Example 3 with JSR223ScriptMacroParameters

use of org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters in project xwiki-platform by xwiki.

the class SecurityTest method executeGroovyMacro.

private void executeGroovyMacro(String script, boolean restricted) throws Exception {
    Macro macro = getComponentManager().getInstance(Macro.class, "groovy");
    JSR223ScriptMacroParameters parameters = new JSR223ScriptMacroParameters();
    MacroTransformationContext context = new MacroTransformationContext();
    context.getTransformationContext().setRestricted(restricted);
    context.setSyntax(Syntax.XWIKI_2_1);
    // The script macro checks the current block (which is a macro block) to see what engine to use.
    context.setCurrentMacroBlock(new MacroBlock("groovy", Collections.<String, String>emptyMap(), false));
    macro.execute(parameters, script, context);
}
Also used : Macro(org.xwiki.rendering.macro.Macro) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) JSR223ScriptMacroParameters(org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters) MacroBlock(org.xwiki.rendering.block.MacroBlock)

Example 4 with JSR223ScriptMacroParameters

use of org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters in project xwiki-platform by xwiki.

the class ClassLoadingTest method testDefineClassInOneExecutionAndUseInAnother.

@Test
public void testDefineClassInOneExecutionAndUseInAnother() throws Exception {
    // First execution: define a class
    JSR223ScriptMacroParameters params = new JSR223ScriptMacroParameters();
    this.macro.execute(params, "class MyClass {}", this.context);
    // Second execution: use the defined class
    this.macro.execute(params, "def var = new MyClass()", this.context);
}
Also used : JSR223ScriptMacroParameters(org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters) Test(org.junit.Test)

Example 5 with JSR223ScriptMacroParameters

use of org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters in project xwiki-platform by xwiki.

the class ClassLoadingTest method testJarParamsInSecondMacro.

/**
 * Verify that it's possible to add jars to the class loader used by the Groovy engine
 * across multiple invocations of the Groovy macro.
 */
@Test
public void testJarParamsInSecondMacro() throws Exception {
    JSR223ScriptMacroParameters params = new JSR223ScriptMacroParameters();
    // Execute a first macro without any jars param passed
    this.macro.execute(params, "def var", this.context);
    // Execute a second macro this time with jars param and verify it works
    params.setJars(getClass().getClassLoader().getResource("dummy.jar").toString());
    this.macro.execute(params, "def var = new Dummy()", this.context);
}
Also used : JSR223ScriptMacroParameters(org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters) Test(org.junit.Test)

Aggregations

JSR223ScriptMacroParameters (org.xwiki.rendering.macro.script.JSR223ScriptMacroParameters)5 Test (org.junit.Test)4 MacroBlock (org.xwiki.rendering.block.MacroBlock)1 Macro (org.xwiki.rendering.macro.Macro)1 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)1