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

Groovy example source code file (GroovyTest.java)

This example Groovy source code file (GroovyTest.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

exception, exception, file, file, flag, flag, groovyruntimeexception, groovytestcase, io, pattern, project, regex, string, string, stringwriter, throwable

The Groovy GroovyTest.java source code

package org.codehaus.groovy.ant;

import groovy.lang.GroovyRuntimeException;
import groovy.util.GroovyTestCase;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.regex.Pattern;

/**
 * Unit tests for the {@link Groovy} ant task.
 * Caution: the *.groovy files used by this test should not get compiled with the rest of the
 * test classes compilation process otherwise they would be available in the classpath
 * and the tests here would be meaningless (tested by testClasspath_missing).
 *
 * @author Marc Guillemot
 */
public class GroovyTest extends GroovyTestCase {
    public static String FLAG = null;
    private final File antFile = new File("src/test/org/codehaus/groovy/ant/GroovyTest.xml");
    private Project project;

    protected void setUp() throws Exception {
        super.setUp();
        project = new Project();
        project.init();
        ProjectHelper.getProjectHelper().parse(project, antFile);
        FLAG = null;
    }

    protected void tearDown() throws Exception {
        super.tearDown();
        // helps if we don't do a clean between runs
        new File("target/test-classes/GroovyTest3Class.class").delete();
    }

    public void testGroovyCodeWithinTag() {
        assertNull(FLAG);
        project.executeTarget("groovyCodeWithinTask");
        assertEquals("from groovy inlined in ant", FLAG);
    }

    public void testGroovyCodeExternalFile() {
        assertNull(FLAG);
        project.executeTarget("groovyCodeInExternalFile");
        assertEquals("from groovy file called from ant", FLAG);
    }

    public void testGroovyCodeInExternalFileWithOtherClass() {
        assertNull(FLAG);
        project.executeTarget("groovyCodeInExternalFileWithOtherClass");
        assertEquals("from GroovyTest2Class.doSomething()", FLAG);
    }

    public void testClasspath_missing() {
        try {
            project.executeTarget("groovyClasspath_missing");
            fail();
        }
        catch (final Exception e) {
            assertEquals(BuildException.class, e.getClass());
        }

    }

    public void testClasspath_classpathAttribute() {
        assertNull(FLAG);
        project.executeTarget("groovyClasspath_classpathAttribute");
        assertEquals("from groovytest3.GroovyTest3Class.doSomething()", FLAG);
    }

    public void testClasspath_classpathrefAttribute() {
        assertNull(FLAG);
        project.executeTarget("groovyClasspath_classpathrefAttribute");
        assertEquals("from groovytest3.GroovyTest3Class.doSomething()", FLAG);
    }

    public void testClasspath_nestedclasspath() {
        assertNull(FLAG);
        project.executeTarget("groovyClasspath_nestedClasspath");
        assertEquals("from groovytest3.GroovyTest3Class.doSomething()", FLAG);
    }

    public void testGroovyArgUsage() {
        assertNull(FLAG);
        project.executeTarget("groovyArgUsage");
        assertEquals("from groovytest3.GroovyTest3Class.doSomethingWithArgs() 1 2 3", FLAG);
    }

    /**
     * Test that helpful "file name" appears in the stack trace and not just "Script1" 
     */
    public void testFileNameInStackTrace() {
        testFileNameInStackTrace("groovyErrorMsg", "\\(embedded_script_in_.*GroovyTest_dot_xml");
        testFileNameInStackTrace("groovyErrorMsg_ExternalFile", "GroovyTest_errorMessage.groovy");
    }

    private void testFileNameInStackTrace(final String target, final String fileNamePattern) {
        try {
            project.executeTarget(target);
            fail();
        }
        catch (final BuildException e) {
            assertEquals(BuildException.class, e.getClass());
            final Throwable cause = e.getCause();
            assertTrue(cause instanceof GroovyRuntimeException);

            final StringWriter sw = new StringWriter();
            cause.printStackTrace(new PrintWriter(sw));
            
            final String stackTrace = sw.toString();
            final Pattern pattern = Pattern.compile(fileNamePattern);
            assertTrue("Does >" + stackTrace + "< contain >" + fileNamePattern + "

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy GroovyTest.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.