View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.rule;
17  
18  import groovy.lang.Binding;
19  import groovy.lang.GroovyShell;
20  import org.junit.Assert;
21  import org.junit.Test;
22  
23  import javax.script.ScriptEngine;
24  import javax.script.ScriptEngineManager;
25  
26  /**
27   * Tests that Groovy can be loaded via Bean Scripting Framework
28  
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public class LoadEmbeddedGroovyTest {
32      @Test public void testNativeGroovy() {
33          Binding binding = new Binding();
34          binding.setVariable("foo", new Integer(2));
35          GroovyShell shell = new GroovyShell(binding);
36  
37          Object value = shell.evaluate("println 'Hello World!'; x = 123; return foo * 10");
38          Assert.assertTrue(value.equals(new Integer(20)));
39          Assert.assertTrue(binding.getVariable("x").equals(new Integer(123)));
40      }
41  
42      @Test public void testJSR223Groovy() throws Exception {
43          ScriptEngineManager factory = new ScriptEngineManager();
44          ScriptEngine engine = factory.getEngineByName("groovy");
45          engine.eval("println 'hello embedded groovy world'");
46      }
47  
48      @Test public void testJSR223Groovy2() throws Exception {
49          ScriptEngineManager factory = new ScriptEngineManager();
50          ScriptEngine engine = factory.getEngineByName("groovy");
51          engine.eval("var = 0\r\ndef foo() {\r\n var++\r\n }\r\n foo()");
52      }
53  }