/src/main/resources/com/onresolve/jira/groovy/test/TestScriptRunner.java
Java | 405 lines | 267 code | 65 blank | 73 comment | 6 complexity | c4625bdc5a2388cb6e58c1e48e2970a8 MD5 | raw file
- package com.onresolve.jira.groovy.test;
- import com.atlassian.core.ofbiz.util.CoreTransactionUtil;
- import com.atlassian.jira.ComponentManager;
- import com.atlassian.jira.issue.AttachmentManager;
- import com.atlassian.jira.issue.IssueManager;
- import com.atlassian.jira.project.ProjectManager;
- import com.onresolve.jira.groovy.CannedScriptRunner;
- import com.onresolve.jira.groovy.GroovyRunner;
- import com.onresolve.jira.groovy.PackageScanner;
- import com.onresolve.jira.groovy.canned.CannedScript;
- import groovy.lang.GroovyClassLoader;
- import groovy.lang.GroovyObject;
- import junit.framework.TestCase;
- import org.apache.log4j.Category;
- import org.hsqldb.jdbcDriver;
- // import org.junit.Test;
- import org.junit.Test;
- import org.ofbiz.core.entity.GenericDelegator;
- // import static org.junit.Assert.*;
- import javax.script.*;
- import java.io.*;
- import java.net.URISyntaxException;
- import java.net.URL;
- import java.util.*;
- public class TestScriptRunner extends TestCase {
- Category log = Category.getInstance(TestScriptRunner.class);
- GroovyRunner groovyRunner;
- CannedScriptRunner cannedScriptRunner;
- private GenericDelegator genericDelegator;
- protected void setUp() throws Exception {
- super.setUp();
- CoreTransactionUtil.setUseTransactions(false);
- jdbcDriver jdbcDriver = new jdbcDriver(); //todo - ofbiz won't work unless this is specifically referenced. Have to work out why.
- genericDelegator = GenericDelegator.getGenericDelegator("default");
- groovyRunner = new GroovyRunner() {
- @Override
- public ProjectManager getProjectManager() {
- return null;
- }
- @Override
- protected IssueManager getIssueManager() {
- return null;
- }
- @Override
- protected AttachmentManager getAttachmentManager() {
- return null;
- }
- };
- cannedScriptRunner = new CannedScriptRunner() {
- @Override
- public ProjectManager getProjectManager() {
- return null;
- }
- @Override
- protected IssueManager getIssueManager() {
- return null;
- }
- @Override
- protected AttachmentManager getAttachmentManager() {
- return null;
- }
- };
- }
- /*
- Make sure an exception is thrown if the engine is not found
- */
- // @Test
- public void testNoScriptEngine() throws Exception {
- File file = getFile("examples/TestScript.nosuch");
- try {
- groovyRunner.run(file.getAbsolutePath(), Collections.EMPTY_MAP);
- } catch (Exception e) {
- assertEquals(e.getMessage(), "Cannot find a script engine for extension: \"nosuch\", (make sure it's on the classpath).");
- }
- }
- // @Test
- public void testJsScriptEngine() throws Exception {
- // check that we can run JavaScript
- File file = getFile("examples/TestScript.js");
- Map rt = groovyRunner.run(file.getAbsolutePath(), Collections.<String,Object>emptyMap());
- assertEquals(Collections.EMPTY_MAP, rt);
- // bindvars should be returned unaltered
- Map<String, Object> inputs = new HashMap<String, Object>();
- inputs.put("s", "a string");
- rt = groovyRunner.run(file.getAbsolutePath(), inputs);
- assertEquals("a string", rt.get("s"));
- // bindvars should be returned with a changed var as the script adds something
- inputs.put("s2", null);
- file = getFile("examples/TestScript2.js");
- rt = groovyRunner.run(file.getAbsolutePath(), inputs);
- assertEquals("newval", rt.get("s2"));
- }
- // @Test
- public void testJsScriptEngineWithScript() throws Exception {
- // check nothing happens with no script
- String s = groovyRunner.doExecute();
- assertEquals(s, GroovyRunner.SUCCESS);
- groovyRunner.setScript("print (\"js running\\n\");");
- try {
- groovyRunner.doExecute();
- } catch (Exception e) {
- assertEquals(e.getMessage(), "You must choose a script engine if not using a file.");
- }
- groovyRunner.setScriptLanguage("ECMAScript");
- groovyRunner.doExecute();
- }
- @Test
- public void testCachedScriptEngine() throws Exception {
- // test we get the same script engine each time
- groovyRunner.setScript("s = this.class.toString()");
- groovyRunner.setScriptLanguage("groovy");
- Map<String, Object> inputs = new HashMap<String, Object>();
- inputs.put("s", "a string");
- // if we run it twice with the same string then we should get the same class name - ie Script1
- Map rt = groovyRunner.run(null, inputs);
- Map rt2 = groovyRunner.run(null, inputs);
- assertEquals(rt.get("s"), rt2.get("s"));
- }
- @Test
- public void XtestNoOutOfPermGen() throws Exception {
- // observe no rise in # of classes loaded with visualvm
- for (int i = 0; i < 10000; i++) {
- groovyRunner.setScript("s = this.class.toString()");
- groovyRunner.setScriptLanguage("groovy");
- Map<String, Object> inputs = new HashMap<String, Object>();
- inputs.put("s", "a string");
- Map rt = groovyRunner.run(null, inputs);
- System.out.println(rt.get("s"));
- }
- }
- /*
- @SuppressWarnings("unchecked")
- public void testGroovyScriptEngine() throws Exception {
- // check nothing happens with no script
- groovyRunner.setScript("v = true");
- groovyRunner.setScriptLanguage("Groovy");
- Map inputs = new HashMap();
- inputs.put("v", false);
- Map rt = groovyRunner.run(null, inputs);
- assertTrue((Boolean) rt.get("v"));
- }
- */
- @SuppressWarnings("unchecked")
- public void testRubyScriptEngine() throws Exception {
- // check nothing happens with no script
- groovyRunner.setScript("puts 'ruby script running'; v = true;");
- groovyRunner.setScriptLanguage("ruby");
- Map<String, Object> inputs = new HashMap<String, Object>();
- inputs.put("v", false);
- Map rt = groovyRunner.run(null, inputs);
- System.out.println(rt.get("v"));
- assertTrue((Boolean) rt.get("v"));
- }
- private File getFile(String path) {
- try {
- URL url = ClassLoader.getSystemResource(path);
- return new File(url.toURI());
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- return null;
- }
- // @Test
- public void testLogging() {
- log.debug("debug");
- log.error("error");
- System.out.println(log.getHierarchy().getCurrentLoggers());
- System.out.println(ComponentManager.getInstance());
- }
- public void XtestCompilable() throws Exception {
- File file = getFile("examples/TestScript.js");
- BufferedReader reader = groovyRunner.getReader(file);
- ScriptEngine scriptEngine = groovyRunner.getScriptEngine(file);
- long t1 = System.currentTimeMillis();
- for (int i = 0; i < 1000; i++) {
- System.out.println("eval");
- scriptEngine.eval(reader);
- }
- long t2 = System.currentTimeMillis();
- System.out.println("Time taken eval: " + (t2 - t1));
- if (scriptEngine instanceof Compilable) {
- System.out.println("comi");
- Compilable compilable = (Compilable) scriptEngine;
- CompiledScript script = compilable.compile(reader);
- t1 = System.currentTimeMillis();
- for (int i = 0; i < 1000; i++) {
- script.eval();
- }
- t2 = System.currentTimeMillis();
- System.out.println("Time taken compiled: " + (t2 - t1));
- }
- }
- @Test
- public void testPackageScanner() throws Exception {
- Set<String> aPackage = PackageScanner.getClassesForPackage("com.onresolve.jira.groovy.canned.admin");
- System.out.println(aPackage);
- assertTrue(aPackage.contains("com/onresolve/jira/groovy/canned/admin/WontCompile313.groovy"));
- }
- @Test
- public void testGetCannedScripts() throws Exception {
- List<CannedScript> scripts = cannedScriptRunner.getCannedScripts();
- for (CannedScript script : scripts) {
- System.out.println(script.getName());
- System.out.println(script.getDescription());
- script.doScript(Collections.EMPTY_MAP);
- }
- }
- @Test
- public void testRunScriptAsClass() throws Exception {
- // while (true) {
- CannedScript script = (CannedScript) GroovyRunner.class.getClassLoader().loadClass("com.onresolve.jira.groovy.canned.admin.RealClass").newInstance();
- System.out.println("out: " + script.toString());
- Thread.sleep(500);
- // }
- // cast to CannedScript etc
- }
- // @Test
- // public void testRunCannedScript() throws Exception {
- // // this successfully loads the script
- // ScriptEngine scriptEngine = groovyRunner.getScriptEngine(new File(".groovy"));
- // // InputStream stream = this.getClass().getResourceAsStream("/com/onresolve/jira/groovy/canned/admin/JustAScript.groovy.txt");
- // InputStream stream = this.getClass().getResourceAsStream("/com/onresolve/jira/groovy/canned/admin/RealClass.groovy");
- // InputStreamReader reader = new InputStreamReader(stream);
- // scriptEngine.put("args", "xyz");
- // Invocable invocableEngine = (Invocable) scriptEngine;
- // // this runs the script which we're trying to avoid
- // // but if we used a real class with the login in a method then it would work
- // scriptEngine.eval(reader);
- //
- // // System.out.println(invocableEngine.invokeFunction("doValidate", new HashMap()));
- // System.out.println(invocableEngine.invokeFunction("getParameters", new HashMap()));
- //
- //
- ///*
- // System.out.println(invocableEngine.invokeFunction("ctor"));
- // System.out.println(invocableEngine.invokeFunction("doValidation", new HashMap()));
- // // System.out.println(invocableEngine.invokeFunction("doScript", new HashMap()));
- // // we could just let it fail when parsed the first time... we need to make sure it doesn't actually do anything bad.
- // // keep trying with an interface.
- // reader.close();
- // // Thread.sleep(2000);
- //*/
- // }
- /**
- * this is the one we're going with
- * to make this work you need to exclude the RealClass from compilation by breaking it first
- * then ensure an old copy is not in the target directory
- * @throws Exception
- */
- @Test
- public void XtestGCL() throws Exception {
- GroovyClassLoader gcl = new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
- gcl.setShouldRecompile(true);
- while (true) {
- Class groovyClass = gcl.loadClass("com.onresolve.jira.groovy.canned.admin.RealClass", true, false);
- GroovyObject go = (GroovyObject)groovyClass.newInstance();
- System.out.println(go.toString());
- Thread.sleep(2000);
- }
-
- }
- @Test
- public void XXtestCompilable() throws Exception {
- ScriptEngine scriptEngine = groovyRunner.getScriptEngine(new File(".groovy"));
- Compilable compilingEngine = (Compilable) scriptEngine;
- InputStream stream = this.getClass().getResourceAsStream("/com/onresolve/jira/groovy/canned/admin/JustAScript.groovy.txt");
- InputStreamReader reader = new InputStreamReader(stream);
- CompiledScript compiledScript = compilingEngine.compile(reader);
- Invocable invocable = (Invocable) compiledScript.getEngine();
- invocable.invokeFunction("getName");
- // invocableEngine.invokeFunction(compiledScript, "getName");
- // System.out.println(invocableEngine.invokeFunction("getName"));
- // useless as we can't invoke particular methods
- reader.close();
- }
- @Test
- public void testRunCannedScript() throws Exception {
- // assertEquals (Collections.EMPTY_MAP, groovyRunner.runCannedScript("com.onresolve.jira.groovy.canned.admin.RealClass", Collections.EMPTY_MAP));
- assertEquals (Collections.EMPTY_MAP, groovyRunner.run("com.onresolve.jira.groovy.canned.admin.RealClass", Collections.EMPTY_MAP));
- }
- @Test(expected=FileNotFoundException.class)
- public void testMissingFile() throws Exception {
- try {
- groovyRunner.run("file_does_not_exist.groovy", Collections.EMPTY_MAP);
- } catch (FileNotFoundException e) {}
- }
- public void testLongRunningScript() throws Exception {
- // test we get the same script engine each time
- groovyRunner.setScript("15.times {\n" +
- " println (\"thread1: $x\")\n" +
- " Thread.sleep 1000\n" +
- "}");
- groovyRunner.setScriptLanguage("groovy");
- Map<String, Integer> inputs = new HashMap<String, Integer>();
- inputs.put("x", 1);
- // observe that the number printed does not change from 1 to 2
- RunnableThread thread = new RunnableThread("thread", groovyRunner, inputs);
- thread.start();
- thread.join(8000);
- Map<String, Integer> inputs2 = new HashMap<String, Integer>();
- inputs2.put("x", 2);
- groovyRunner.setScript("15.times {\n" +
- " println (\"thread2: $x\")\n" +
- " Thread.sleep 1000\n" +
- "}");
- RunnableThread interferer = new RunnableThread("interferer", groovyRunner, inputs2);
- interferer.start();
- thread.join();
- }
- class RunnableThread extends Thread {
- Thread runner;
- GroovyRunner groovyRunner;
- Map binding;
- public RunnableThread() {
- }
- public RunnableThread(String threadName, GroovyRunner groovyRunner, Map binding) {
- System.out.println(this.getName());
- this.groovyRunner = groovyRunner;
- this.binding = binding;
- }
- public void run() {
- //Display info about this particular thread
- try {
- groovyRunner.run(null, binding);
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread() + " thread exiting");
- }
- }
- @Test
- public void testBusted() throws Exception {
- // run the dovalidation thing
- GroovyClassLoader gcl = new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
- gcl.setShouldRecompile(true);
- CannedScript scriptDef = (CannedScript) gcl.loadClass("com.onresolve.jira.groovy.canned.workflow.conditions.TestCondition").newInstance();
- Map m = new HashMap();
- m.put("p","x");
- System.out.println(scriptDef.doValidate(m, false));
- }
- }