PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/resources/com/onresolve/jira/groovy/test/TestScriptRunner.java

https://bitbucket.org/sorin/jira-plugin-intellij
Java | 405 lines | 267 code | 65 blank | 73 comment | 6 complexity | c4625bdc5a2388cb6e58c1e48e2970a8 MD5 | raw file
  1. package com.onresolve.jira.groovy.test;
  2. import com.atlassian.core.ofbiz.util.CoreTransactionUtil;
  3. import com.atlassian.jira.ComponentManager;
  4. import com.atlassian.jira.issue.AttachmentManager;
  5. import com.atlassian.jira.issue.IssueManager;
  6. import com.atlassian.jira.project.ProjectManager;
  7. import com.onresolve.jira.groovy.CannedScriptRunner;
  8. import com.onresolve.jira.groovy.GroovyRunner;
  9. import com.onresolve.jira.groovy.PackageScanner;
  10. import com.onresolve.jira.groovy.canned.CannedScript;
  11. import groovy.lang.GroovyClassLoader;
  12. import groovy.lang.GroovyObject;
  13. import junit.framework.TestCase;
  14. import org.apache.log4j.Category;
  15. import org.hsqldb.jdbcDriver;
  16. // import org.junit.Test;
  17. import org.junit.Test;
  18. import org.ofbiz.core.entity.GenericDelegator;
  19. // import static org.junit.Assert.*;
  20. import javax.script.*;
  21. import java.io.*;
  22. import java.net.URISyntaxException;
  23. import java.net.URL;
  24. import java.util.*;
  25. public class TestScriptRunner extends TestCase {
  26. Category log = Category.getInstance(TestScriptRunner.class);
  27. GroovyRunner groovyRunner;
  28. CannedScriptRunner cannedScriptRunner;
  29. private GenericDelegator genericDelegator;
  30. protected void setUp() throws Exception {
  31. super.setUp();
  32. CoreTransactionUtil.setUseTransactions(false);
  33. jdbcDriver jdbcDriver = new jdbcDriver(); //todo - ofbiz won't work unless this is specifically referenced. Have to work out why.
  34. genericDelegator = GenericDelegator.getGenericDelegator("default");
  35. groovyRunner = new GroovyRunner() {
  36. @Override
  37. public ProjectManager getProjectManager() {
  38. return null;
  39. }
  40. @Override
  41. protected IssueManager getIssueManager() {
  42. return null;
  43. }
  44. @Override
  45. protected AttachmentManager getAttachmentManager() {
  46. return null;
  47. }
  48. };
  49. cannedScriptRunner = new CannedScriptRunner() {
  50. @Override
  51. public ProjectManager getProjectManager() {
  52. return null;
  53. }
  54. @Override
  55. protected IssueManager getIssueManager() {
  56. return null;
  57. }
  58. @Override
  59. protected AttachmentManager getAttachmentManager() {
  60. return null;
  61. }
  62. };
  63. }
  64. /*
  65. Make sure an exception is thrown if the engine is not found
  66. */
  67. // @Test
  68. public void testNoScriptEngine() throws Exception {
  69. File file = getFile("examples/TestScript.nosuch");
  70. try {
  71. groovyRunner.run(file.getAbsolutePath(), Collections.EMPTY_MAP);
  72. } catch (Exception e) {
  73. assertEquals(e.getMessage(), "Cannot find a script engine for extension: \"nosuch\", (make sure it's on the classpath).");
  74. }
  75. }
  76. // @Test
  77. public void testJsScriptEngine() throws Exception {
  78. // check that we can run JavaScript
  79. File file = getFile("examples/TestScript.js");
  80. Map rt = groovyRunner.run(file.getAbsolutePath(), Collections.<String,Object>emptyMap());
  81. assertEquals(Collections.EMPTY_MAP, rt);
  82. // bindvars should be returned unaltered
  83. Map<String, Object> inputs = new HashMap<String, Object>();
  84. inputs.put("s", "a string");
  85. rt = groovyRunner.run(file.getAbsolutePath(), inputs);
  86. assertEquals("a string", rt.get("s"));
  87. // bindvars should be returned with a changed var as the script adds something
  88. inputs.put("s2", null);
  89. file = getFile("examples/TestScript2.js");
  90. rt = groovyRunner.run(file.getAbsolutePath(), inputs);
  91. assertEquals("newval", rt.get("s2"));
  92. }
  93. // @Test
  94. public void testJsScriptEngineWithScript() throws Exception {
  95. // check nothing happens with no script
  96. String s = groovyRunner.doExecute();
  97. assertEquals(s, GroovyRunner.SUCCESS);
  98. groovyRunner.setScript("print (\"js running\\n\");");
  99. try {
  100. groovyRunner.doExecute();
  101. } catch (Exception e) {
  102. assertEquals(e.getMessage(), "You must choose a script engine if not using a file.");
  103. }
  104. groovyRunner.setScriptLanguage("ECMAScript");
  105. groovyRunner.doExecute();
  106. }
  107. @Test
  108. public void testCachedScriptEngine() throws Exception {
  109. // test we get the same script engine each time
  110. groovyRunner.setScript("s = this.class.toString()");
  111. groovyRunner.setScriptLanguage("groovy");
  112. Map<String, Object> inputs = new HashMap<String, Object>();
  113. inputs.put("s", "a string");
  114. // if we run it twice with the same string then we should get the same class name - ie Script1
  115. Map rt = groovyRunner.run(null, inputs);
  116. Map rt2 = groovyRunner.run(null, inputs);
  117. assertEquals(rt.get("s"), rt2.get("s"));
  118. }
  119. @Test
  120. public void XtestNoOutOfPermGen() throws Exception {
  121. // observe no rise in # of classes loaded with visualvm
  122. for (int i = 0; i < 10000; i++) {
  123. groovyRunner.setScript("s = this.class.toString()");
  124. groovyRunner.setScriptLanguage("groovy");
  125. Map<String, Object> inputs = new HashMap<String, Object>();
  126. inputs.put("s", "a string");
  127. Map rt = groovyRunner.run(null, inputs);
  128. System.out.println(rt.get("s"));
  129. }
  130. }
  131. /*
  132. @SuppressWarnings("unchecked")
  133. public void testGroovyScriptEngine() throws Exception {
  134. // check nothing happens with no script
  135. groovyRunner.setScript("v = true");
  136. groovyRunner.setScriptLanguage("Groovy");
  137. Map inputs = new HashMap();
  138. inputs.put("v", false);
  139. Map rt = groovyRunner.run(null, inputs);
  140. assertTrue((Boolean) rt.get("v"));
  141. }
  142. */
  143. @SuppressWarnings("unchecked")
  144. public void testRubyScriptEngine() throws Exception {
  145. // check nothing happens with no script
  146. groovyRunner.setScript("puts 'ruby script running'; v = true;");
  147. groovyRunner.setScriptLanguage("ruby");
  148. Map<String, Object> inputs = new HashMap<String, Object>();
  149. inputs.put("v", false);
  150. Map rt = groovyRunner.run(null, inputs);
  151. System.out.println(rt.get("v"));
  152. assertTrue((Boolean) rt.get("v"));
  153. }
  154. private File getFile(String path) {
  155. try {
  156. URL url = ClassLoader.getSystemResource(path);
  157. return new File(url.toURI());
  158. } catch (URISyntaxException e) {
  159. e.printStackTrace();
  160. }
  161. return null;
  162. }
  163. // @Test
  164. public void testLogging() {
  165. log.debug("debug");
  166. log.error("error");
  167. System.out.println(log.getHierarchy().getCurrentLoggers());
  168. System.out.println(ComponentManager.getInstance());
  169. }
  170. public void XtestCompilable() throws Exception {
  171. File file = getFile("examples/TestScript.js");
  172. BufferedReader reader = groovyRunner.getReader(file);
  173. ScriptEngine scriptEngine = groovyRunner.getScriptEngine(file);
  174. long t1 = System.currentTimeMillis();
  175. for (int i = 0; i < 1000; i++) {
  176. System.out.println("eval");
  177. scriptEngine.eval(reader);
  178. }
  179. long t2 = System.currentTimeMillis();
  180. System.out.println("Time taken eval: " + (t2 - t1));
  181. if (scriptEngine instanceof Compilable) {
  182. System.out.println("comi");
  183. Compilable compilable = (Compilable) scriptEngine;
  184. CompiledScript script = compilable.compile(reader);
  185. t1 = System.currentTimeMillis();
  186. for (int i = 0; i < 1000; i++) {
  187. script.eval();
  188. }
  189. t2 = System.currentTimeMillis();
  190. System.out.println("Time taken compiled: " + (t2 - t1));
  191. }
  192. }
  193. @Test
  194. public void testPackageScanner() throws Exception {
  195. Set<String> aPackage = PackageScanner.getClassesForPackage("com.onresolve.jira.groovy.canned.admin");
  196. System.out.println(aPackage);
  197. assertTrue(aPackage.contains("com/onresolve/jira/groovy/canned/admin/WontCompile313.groovy"));
  198. }
  199. @Test
  200. public void testGetCannedScripts() throws Exception {
  201. List<CannedScript> scripts = cannedScriptRunner.getCannedScripts();
  202. for (CannedScript script : scripts) {
  203. System.out.println(script.getName());
  204. System.out.println(script.getDescription());
  205. script.doScript(Collections.EMPTY_MAP);
  206. }
  207. }
  208. @Test
  209. public void testRunScriptAsClass() throws Exception {
  210. // while (true) {
  211. CannedScript script = (CannedScript) GroovyRunner.class.getClassLoader().loadClass("com.onresolve.jira.groovy.canned.admin.RealClass").newInstance();
  212. System.out.println("out: " + script.toString());
  213. Thread.sleep(500);
  214. // }
  215. // cast to CannedScript etc
  216. }
  217. // @Test
  218. // public void testRunCannedScript() throws Exception {
  219. // // this successfully loads the script
  220. // ScriptEngine scriptEngine = groovyRunner.getScriptEngine(new File(".groovy"));
  221. // // InputStream stream = this.getClass().getResourceAsStream("/com/onresolve/jira/groovy/canned/admin/JustAScript.groovy.txt");
  222. // InputStream stream = this.getClass().getResourceAsStream("/com/onresolve/jira/groovy/canned/admin/RealClass.groovy");
  223. // InputStreamReader reader = new InputStreamReader(stream);
  224. // scriptEngine.put("args", "xyz");
  225. // Invocable invocableEngine = (Invocable) scriptEngine;
  226. // // this runs the script which we're trying to avoid
  227. // // but if we used a real class with the login in a method then it would work
  228. // scriptEngine.eval(reader);
  229. //
  230. // // System.out.println(invocableEngine.invokeFunction("doValidate", new HashMap()));
  231. // System.out.println(invocableEngine.invokeFunction("getParameters", new HashMap()));
  232. //
  233. //
  234. ///*
  235. // System.out.println(invocableEngine.invokeFunction("ctor"));
  236. // System.out.println(invocableEngine.invokeFunction("doValidation", new HashMap()));
  237. // // System.out.println(invocableEngine.invokeFunction("doScript", new HashMap()));
  238. // // we could just let it fail when parsed the first time... we need to make sure it doesn't actually do anything bad.
  239. // // keep trying with an interface.
  240. // reader.close();
  241. // // Thread.sleep(2000);
  242. //*/
  243. // }
  244. /**
  245. * this is the one we're going with
  246. * to make this work you need to exclude the RealClass from compilation by breaking it first
  247. * then ensure an old copy is not in the target directory
  248. * @throws Exception
  249. */
  250. @Test
  251. public void XtestGCL() throws Exception {
  252. GroovyClassLoader gcl = new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
  253. gcl.setShouldRecompile(true);
  254. while (true) {
  255. Class groovyClass = gcl.loadClass("com.onresolve.jira.groovy.canned.admin.RealClass", true, false);
  256. GroovyObject go = (GroovyObject)groovyClass.newInstance();
  257. System.out.println(go.toString());
  258. Thread.sleep(2000);
  259. }
  260. }
  261. @Test
  262. public void XXtestCompilable() throws Exception {
  263. ScriptEngine scriptEngine = groovyRunner.getScriptEngine(new File(".groovy"));
  264. Compilable compilingEngine = (Compilable) scriptEngine;
  265. InputStream stream = this.getClass().getResourceAsStream("/com/onresolve/jira/groovy/canned/admin/JustAScript.groovy.txt");
  266. InputStreamReader reader = new InputStreamReader(stream);
  267. CompiledScript compiledScript = compilingEngine.compile(reader);
  268. Invocable invocable = (Invocable) compiledScript.getEngine();
  269. invocable.invokeFunction("getName");
  270. // invocableEngine.invokeFunction(compiledScript, "getName");
  271. // System.out.println(invocableEngine.invokeFunction("getName"));
  272. // useless as we can't invoke particular methods
  273. reader.close();
  274. }
  275. @Test
  276. public void testRunCannedScript() throws Exception {
  277. // assertEquals (Collections.EMPTY_MAP, groovyRunner.runCannedScript("com.onresolve.jira.groovy.canned.admin.RealClass", Collections.EMPTY_MAP));
  278. assertEquals (Collections.EMPTY_MAP, groovyRunner.run("com.onresolve.jira.groovy.canned.admin.RealClass", Collections.EMPTY_MAP));
  279. }
  280. @Test(expected=FileNotFoundException.class)
  281. public void testMissingFile() throws Exception {
  282. try {
  283. groovyRunner.run("file_does_not_exist.groovy", Collections.EMPTY_MAP);
  284. } catch (FileNotFoundException e) {}
  285. }
  286. public void testLongRunningScript() throws Exception {
  287. // test we get the same script engine each time
  288. groovyRunner.setScript("15.times {\n" +
  289. " println (\"thread1: $x\")\n" +
  290. " Thread.sleep 1000\n" +
  291. "}");
  292. groovyRunner.setScriptLanguage("groovy");
  293. Map<String, Integer> inputs = new HashMap<String, Integer>();
  294. inputs.put("x", 1);
  295. // observe that the number printed does not change from 1 to 2
  296. RunnableThread thread = new RunnableThread("thread", groovyRunner, inputs);
  297. thread.start();
  298. thread.join(8000);
  299. Map<String, Integer> inputs2 = new HashMap<String, Integer>();
  300. inputs2.put("x", 2);
  301. groovyRunner.setScript("15.times {\n" +
  302. " println (\"thread2: $x\")\n" +
  303. " Thread.sleep 1000\n" +
  304. "}");
  305. RunnableThread interferer = new RunnableThread("interferer", groovyRunner, inputs2);
  306. interferer.start();
  307. thread.join();
  308. }
  309. class RunnableThread extends Thread {
  310. Thread runner;
  311. GroovyRunner groovyRunner;
  312. Map binding;
  313. public RunnableThread() {
  314. }
  315. public RunnableThread(String threadName, GroovyRunner groovyRunner, Map binding) {
  316. System.out.println(this.getName());
  317. this.groovyRunner = groovyRunner;
  318. this.binding = binding;
  319. }
  320. public void run() {
  321. //Display info about this particular thread
  322. try {
  323. groovyRunner.run(null, binding);
  324. } catch (Exception e) {
  325. e.printStackTrace();
  326. }
  327. System.out.println(Thread.currentThread() + " thread exiting");
  328. }
  329. }
  330. @Test
  331. public void testBusted() throws Exception {
  332. // run the dovalidation thing
  333. GroovyClassLoader gcl = new GroovyClassLoader(Thread.currentThread().getContextClassLoader());
  334. gcl.setShouldRecompile(true);
  335. CannedScript scriptDef = (CannedScript) gcl.loadClass("com.onresolve.jira.groovy.canned.workflow.conditions.TestCondition").newInstance();
  336. Map m = new HashMap();
  337. m.put("p","x");
  338. System.out.println(scriptDef.doValidate(m, false));
  339. }
  340. }