1 46 47 package groovy.lang; 48 49 import org.codehaus.groovy.classgen.TestSupport; 50 import org.codehaus.groovy.control.CompilationFailedException; 51 import org.codehaus.groovy.runtime.MethodClosure; 52 53 import java.io.IOException ; 54 55 60 public class ScriptTest extends TestSupport 61 { 62 70 public void testInvokeMethodFallsThroughToMethodClosureInBinding() throws IOException , CompilationFailedException, IllegalAccessException , InstantiationException 71 { 72 String text = "if (method() == 3) { println 'succeeded' }"; 73 74 GroovyCodeSource codeSource = new GroovyCodeSource(text, "groovy.script", "groovy.script"); 75 GroovyClassLoader loader = new GroovyClassLoader(Thread.currentThread().getContextClassLoader()); 76 Class clazz = loader.parseClass(codeSource); 77 Script script = ((Script) clazz.newInstance()); 78 79 Binding binding = new Binding(); 80 binding.setVariable("method", new MethodClosure(new Dummy(), "method")); 81 script.setBinding(binding); 82 83 script.run(); 84 } 85 86 public static class Dummy { 87 public Integer method() { 88 return new Integer (3); 89 } 90 } 91 } 92 | Popular Tags |