1 /*
2 * $Id: GroovyShellTest.java,v 1.7 2005/03/02 08:21:04 jstrachan Exp $version
3 * Nov 23, 2003 9:02:55 PM $user Exp $
4 *
5 * Copyright 2003 (C) Sam Pullara. All Rights Reserved.
6 *
7 * Redistribution and use of this software and associated documentation
8 * ("Software"), with or without modification, are permitted provided that the
9 * following conditions are met: 1. Redistributions of source code must retain
10 * copyright statements and notices. Redistributions must also contain a copy
11 * of this document. 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the distribution. 3.
14 * The name "groovy" must not be used to endorse or promote products derived
15 * from this Software without prior written permission of The Codehaus. For
16 * written permission, please contact info@codehaus.org. 4. Products derived
17 * from this Software may not be called "groovy" nor may "groovy" appear in
18 * their names without prior written permission of The Codehaus. "groovy" is a
19 * registered trademark of The Codehaus. 5. Due credit should be given to The
20 * Codehaus - http://groovy.codehaus.org/
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``AS IS'' AND ANY
23 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR
26 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32 * DAMAGE.
33 *
34 */
35 package groovy.lang;
36
37 import groovy.util.GroovyTestCase;
38
39 import java.io.ByteArrayInputStream;
40 import java.util.HashMap;
41 import java.util.Map;
42
43 import org.codehaus.groovy.control.CompilerConfiguration;
44
45 import junit.framework.Test;
46 import junit.framework.TestSuite;
47 import junit.textui.TestRunner;
48
49 /***
50 * @author sam
51 *
52 * To change the template for this generated type comment go to Window -
53 * Preferences - Java - Code Generation - Code and Comments
54 */
55 public class GroovyShellTest extends GroovyTestCase {
56
57 private String script1 = "test = 1";
58
59 public static void main(String[] args) {
60 TestRunner.run(suite());
61 }
62
63 public static Test suite() {
64 return new TestSuite(GroovyShellTest.class);
65 }
66
67 public void testExecuteScript() {
68 GroovyShell shell = new GroovyShell();
69 try {
70 Object result = shell.evaluate(new ByteArrayInputStream(script1.getBytes()), "Test.groovy");
71 assertEquals(new Integer(1), result);
72 }
73 catch (Exception e) {
74 assertTrue(false);
75 }
76 }
77
78 private static class PropertyHolder {
79 private Map map = new HashMap();
80 public void set(String key, Object value) {
81 map.put(key, value);
82 }
83 public Object get(String key) {
84 return map.get(key);
85 }
86 }
87
88 private String script2 = "test.prop = 2\nreturn test.prop";
89
90 public void testExecuteScriptWithContext() {
91 Binding context = new Binding();
92 context.setVariable("test", new PropertyHolder());
93 GroovyShell shell = new GroovyShell(context);
94 try {
95 Object result = shell.evaluate(new ByteArrayInputStream(script2.getBytes()), "Test.groovy");
96 assertEquals(new Integer(2), result);
97 }
98 catch (Exception e) {
99 fail(e.toString());
100 }
101 }
102
103 public void testScriptWithDerivedBaseClass() throws Exception {
104 Binding context = new Binding();
105 CompilerConfiguration config = new CompilerConfiguration();
106 config.setScriptBaseClass(DerivedScript.class.getName());
107 GroovyShell shell = new GroovyShell(context, config);
108 Object result = shell.evaluate("x = 'abc'; doSomething(cheese)");
109 assertEquals("I like Cheddar", result);
110 assertEquals("abc", context.getVariable("x"));
111 }
112 }