alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (GroovyShellTest.java)

This example Groovy source code file (GroovyShellTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

binding, binding, compilerconfiguration, exception, file, groovycodesource, groovyshell, groovyshell, io, net, network, object, object, string, string, test, testsuite, util

The Groovy GroovyShellTest.java source code

/*
 * Copyright 2003-2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovy.lang;

import groovy.util.GroovyTestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * @author sam
 * @author Merlyn Albery-Speyer
 */
public class GroovyShellTest extends GroovyTestCase {

    private String script1 = "test = 1";

    public static void main(String[] args) {
        TestRunner.run(suite());
    }

    public static Test suite() {
        return new TestSuite(GroovyShellTest.class);
    }

    public void testExecuteScript() {
        GroovyShell shell = new GroovyShell();
        try {
            Object result = shell.evaluate(new ByteArrayInputStream(script1.getBytes()), "Test.groovy");
            assertEquals(new Integer(1), result);
        }
        catch (Exception e) {
            fail(e.toString());
        }
    }

    private static class PropertyHolder {
        private Map map = new HashMap();

        public void set(String key, Object value) {
            map.put(key, value);
        }

        public Object get(String key) {
            return map.get(key);
        }
    }

    private String script2 = "test.prop = 2\nreturn test.prop";

    public void testExecuteScriptWithContext() {
        Binding context = new Binding();
        context.setVariable("test", new PropertyHolder());
        GroovyShell shell = new GroovyShell(context);
        try {
            Object result = shell.evaluate(new ByteArrayInputStream(script2.getBytes()), "Test.groovy");
            assertEquals(new Integer(2), result);
        }
        catch (Exception e) {
            fail(e.toString());
        }
    }

    public void testScriptWithDerivedBaseClass() throws Exception {
        Binding context = new Binding();
        CompilerConfiguration config = new CompilerConfiguration();
        config.setScriptBaseClass(DerivedScript.class.getName());
        GroovyShell shell = new GroovyShell(context, config);
        Object result = shell.evaluate("x = 'abc'; doSomething(cheese)");
        assertEquals("I like Cheddar", result);
        assertEquals("abc", context.getVariable("x"));
    }
    
    public void testClassLoader() {
        Binding context = new Binding();
        CompilerConfiguration config = new CompilerConfiguration();
        config.setScriptBaseClass(DerivedScript.class.getName());
        GroovyShell shell = new GroovyShell(context, config);
        String script = "evaluate '''\n"+
                        "class XXXX{}\n"+
                        "assert evaluate('XXXX') == XXXX\n"+
                        "'''";
        shell.evaluate(script);
     
    }

    public void testWithGCSWithURL() throws Exception {
        String scriptFileName = "src/test/groovy/bugs/GROOVY3934Helper.groovy";
        File helperScript = new File(scriptFileName);
        if(!helperScript.exists()) {
            fail("File " + scriptFileName + " does not exist");
        } else {
            URL url = helperScript.toURL();
            GroovyCodeSource gcs = new GroovyCodeSource(url);
            GroovyShell shell = new GroovyShell();
            Object result = shell.evaluate(gcs);
            assertEquals("GROOVY3934Helper script called", result);
        }
    }
    
    public void testLaunchesJUnitTestSuite() throws Exception {
        // create a valid (empty) test suite on disk
        String testName = "GroovyShellTestJUnit3Test"+System.currentTimeMillis();
        File testSuite = new File(System.getProperty("java.io.tmpdir"), testName);
        DefaultGroovyMethods.write(testSuite, "import junit.framework.*; \r\n"+
            "public class "+testName+" extends TestSuite { \r\n"+
            "    public static Test suite() { \r\n"+
            "        return new TestSuite(); \r\n"+
            "    } \r\n" +
            "} \r\n");
        testSuite.deleteOnExit();
        
        PrintStream out = System.out;
        System.setOut( new PrintStream(new ByteArrayOutputStream()) );
        try {
            // makes this more of an integration test than a unit test...
            GroovyShell.main( new String[] { testSuite.getCanonicalPath() });
        } finally {
            System.setOut( out );
        }
    } 
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy GroovyShellTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.