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());
}
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());
}
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);
}
}
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);
}
}
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);
}
Aggregations