Search in sources :

Example 81 with Script

use of groovy.lang.Script in project groovity by disney.

the class HttpLocatorTest method testHTTPCompile.

@Test
public void testHTTPCompile() throws Exception {
    stubFor(get(urlEqualTo("/")).willReturn(aResponse().withStatus(200).withHeader("Last-Modified", "100").withBody("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\r\n" + "<html>\r\n" + " <head>\r\n" + "  <title>Index of /groovy/nwsDynGroovy</title>\r\n" + " </head>\r\n" + " <body>\r\n" + "<h1>Index of /groovy/nwsDynGroovy</h1>\r\n" + "<ul><li><a href=\"/groovy/\"> Parent Directory</a></li>\r\n" + "<li><a href=\"test.grvt\"> test.grvt</a></li>\r\n" + "</ul>\r\n" + "</body></html>")));
    String lmod = DateUtils.formatDate(new Date());
    stubFor(head(urlEqualTo("/test.grvt")).willReturn(aResponse().withStatus(200).withHeader("Last-Modified", lmod)));
    stubFor(get(urlEqualTo("/test.grvt")).willReturn(aResponse().withStatus(200).withHeader("Last-Modified", lmod).withBody(" out << \"hello\" ")));
    Groovity groovity = new GroovityBuilder().setSourcePhases(EnumSet.of(GroovityPhase.STARTUP)).setSourceLocations(Arrays.asList(new URI("http://localhost:28187"))).build();
    CharArrayWriter writer = new CharArrayWriter();
    Binding binding = new Binding();
    Script script = groovity.load("/test", binding);
    binding.setProperty("out", writer);
    script.run();
    Assert.assertEquals("hello", writer.toString());
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) GroovityBuilder(com.disney.groovity.GroovityBuilder) Groovity(com.disney.groovity.Groovity) URI(java.net.URI) Date(java.util.Date) CharArrayWriter(java.io.CharArrayWriter) Test(org.junit.Test)

Example 82 with Script

use of groovy.lang.Script in project groovity by disney.

the class TestCoreGroovity method testConf.

@Test
public void testConf() throws Exception {
    Binding binding = new Binding();
    CharArrayWriter writer = new CharArrayWriter();
    binding.setVariable("out", writer);
    Script confScript = groovity.load("/conf", binding);
    GroovityClassLoader gcl = (GroovityClassLoader) confScript.getClass().getClassLoader();
    groovity.run("/conf", binding);
    String result = writer.toString();
    Assert.assertEquals("|false||0||test|||", result);
    writer.reset();
    gcl.configure(new Configurator() {

        @Override
        public void init() {
        }

        @Override
        public void destroy() {
        }

        @Override
        public void configure(String sourcePath, Set<String> propertyNames, BiConsumer<String, String> propertySetter) {
            propertySetter.accept("testDefaultBoolean", "true");
            propertySetter.accept("testBooleanType", "foo");
            propertySetter.accept("testDefaultInteger", "99");
            propertySetter.accept("testIntegerType", "111");
            propertySetter.accept("testDefaultString", "bar");
            propertySetter.accept("testStringType", "zzz");
            propertySetter.accept("testNull", "qq");
        }
    });
    groovity.run("/conf", binding);
    result = writer.toString();
    Assert.assertEquals("|true|false|99|111|bar|zzz|qq|", result);
    writer.reset();
    logRecords.clear();
    gcl.configure(new Configurator() {

        @Override
        public void init() {
        }

        @Override
        public void destroy() {
        }

        @Override
        public void configure(String sourcePath, Set<String> propertyNames, BiConsumer<String, String> propertySetter) {
            propertySetter.accept("testDefaultInteger", "badOnPurpose");
            propertySetter.accept("testIntegerType", "badOnPurpose");
        }
    });
    groovity.run("/conf", binding);
    result = writer.toString();
    Assert.assertEquals("|false||0||test|||", result);
    Assert.assertEquals(3, logRecords.size());
    Assert.assertEquals(Level.WARNING, logRecords.get(0).getLevel());
    Assert.assertEquals(Level.SEVERE, logRecords.get(1).getLevel());
    Assert.assertEquals(Level.SEVERE, logRecords.get(2).getLevel());
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) Configurator(com.disney.groovity.conf.Configurator) GroovityClassLoader(com.disney.groovity.compile.GroovityClassLoader) CharArrayWriter(java.io.CharArrayWriter) Test(org.junit.Test)

Example 83 with Script

use of groovy.lang.Script in project mdw-designer by CenturyLinkCloud.

the class GroovyTestCaseRun method run.

@Override
public void run() {
    startExecution();
    try {
        String groovyScript = getTestCase().getGroovyScript();
        runOnServer = testCaseAsset != null && testCaseAsset.getPackageName().endsWith(".server");
        if (runOnServer) {
            JSONObject actionRequest = new JSONObject();
            JSONObject action = new JSONObject();
            action.put("name", "UnitTest");
            JSONArray parameters = new JSONArray();
            JSONObject appName = new JSONObject();
            appName.put("appName", "MDW Automated Testing");
            parameters.put(appName);
            action.put("parameters", parameters);
            actionRequest.put("Action", action);
            JSONObject script = new JSONObject();
            script.put("name", getTestCase().getCaseName());
            script.put("groovy", groovyScript);
            actionRequest.put("Script", script);
            String response;
            if (dao.getDatabaseSchemaVersion() >= 6001)
                response = dao.executeUnitTest(script.toString(2), getDefaultMessageHeaders());
            else
                response = dao.sendMessage("REST", actionRequest.toString(2), getDefaultMessageHeaders());
            JSONObject responseMsg = new JSONObject(response);
            JSONObject status = responseMsg.getJSONObject("status");
            if (status.getInt("code") != 0) {
                String msg = status.getString("message");
                if (status.has("location"))
                    msg = status.getString("location") + " " + msg;
                throw new TestException(msg);
            }
        } else {
            CompilerConfiguration compilerConfig = new CompilerConfiguration();
            if (classpathList != null)
                compilerConfig.setClasspathList(classpathList);
            compilerConfig.setScriptBaseClass(GroovyTestCaseScript.class.getName());
            Binding binding = new Binding();
            binding.setVariable("testCaseRun", this);
            GroovyShell shell = new GroovyShell(this.getClass().getClassLoader(), binding, compilerConfig);
            Script gScript = shell.parse(groovyScript);
            gScript.setProperty("out", log);
            gScript.run();
        }
        finishExecution(null);
    } catch (Throwable ex) {
        finishExecution(ex);
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyShell(groovy.lang.GroovyShell)

Example 84 with Script

use of groovy.lang.Script in project spring-integration by spring-projects.

the class GroovyScriptExecutingMessageProcessor method execute.

private Object execute(Map<String, Object> variables) throws ScriptCompilationException {
    try {
        GroovyObject goo = (GroovyObject) this.scriptClass.newInstance();
        VariableBindingGroovyObjectCustomizerDecorator groovyObjectCustomizer = new BindingOverwriteGroovyObjectCustomizerDecorator(new BeanFactoryFallbackBinding(variables));
        groovyObjectCustomizer.setCustomizer(this.customizerDecorator);
        if (goo instanceof Script) {
            // Allow metaclass and other customization.
            groovyObjectCustomizer.customize(goo);
            // A Groovy script, probably creating an instance: let's execute it.
            return ((Script) goo).run();
        } else {
            // An instance of the scripted class: let's return it as-is.
            return goo;
        }
    } catch (InstantiationException ex) {
        throw new ScriptCompilationException(this.scriptSource, "Could not instantiate Groovy script class: " + this.scriptClass.getName(), ex);
    } catch (IllegalAccessException ex) {
        throw new ScriptCompilationException(this.scriptSource, "Could not access Groovy script constructor: " + this.scriptClass.getName(), ex);
    }
}
Also used : Script(groovy.lang.Script) ScriptCompilationException(org.springframework.scripting.ScriptCompilationException) GroovyObject(groovy.lang.GroovyObject)

Example 85 with Script

use of groovy.lang.Script in project DeskChan by DeskChan.

the class Main method loadByPath.

@Override
public void loadByPath(Path path) throws Throwable {
    String id;
    if (Files.isDirectory(path)) {
        id = path.getFileName().toString();
        path = path.resolve("plugin.groovy");
    } else {
        if (path.getFileName().toString().equals("plugin.groovy"))
            id = path.getParent().getFileName().toString();
        else {
            id = path.getFileName().toString();
            id = id.substring(0, id.length() - 7);
        }
    }
    System.setProperty("groovy.grape.report.downloads", "true");
    CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
    compilerConfiguration.setSourceEncoding("UTF-8");
    compilerConfiguration.setScriptBaseClass("info.deskchan.groovy_support.GroovyPlugin");
    compilerConfiguration.setClasspath(path.getParent().toString());
    GroovyShell groovyShell = new GroovyShell(compilerConfiguration);
    Script script = groovyShell.parse(path.toFile());
    GroovyPlugin plugin = (GroovyPlugin) script;
    plugin.setPluginDirPath(path.getParent());
    PluginConfig config = new PluginConfig("Groovy");
    path = path.getParent().resolve("manifest.json");
    config.appendFromJson(path);
    PluginManager.getInstance().initializePlugin(id, plugin, config);
}
Also used : Script(groovy.lang.Script) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyShell(groovy.lang.GroovyShell)

Aggregations

Script (groovy.lang.Script)123 Binding (groovy.lang.Binding)59 GroovyShell (groovy.lang.GroovyShell)23 IOException (java.io.IOException)21 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)13 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)13 File (java.io.File)12 Map (java.util.Map)12 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)11 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 GroovityClassLoader (com.disney.groovity.compile.GroovityClassLoader)8 Closure (groovy.lang.Closure)8 PrintWriter (java.io.PrintWriter)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)8 MissingMethodException (groovy.lang.MissingMethodException)7