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

Groovy example source code file (GroovySwingTestCase.groovy)

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

class, class, constructor, constructor, groovyswingtestcase, groovytestcase, gui, reflection, string, string, swing, throwable, throwable

The Groovy GroovySwingTestCase.groovy source code

package groovy.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException
import javax.swing.SwingUtilities;

public class GroovySwingTestCase extends GroovyTestCase {
    private static boolean headless;

    /**
     * A boolean indicating if we are running in headless mode.
     * Check this flag if you believe your test may make use of AWT/Swing
     * features, then simply return rather than running your test.
     *
     * @return true if running in headless mode
     */
    public static boolean isHeadless() {
        return headless;
    }

    /**
     * Alias for isHeadless().
     *
     * @return true if running in headless mode
     */
    public static boolean getHeadless() {
        return isHeadless();
    }

    public static void testInEDT(Closure test) {
        Throwable exception = null
        if (headless) {
            return
        }
        SwingUtilities.invokeAndWait {
            try {
                test()
            } catch (Throwable t) {
                exception = t
            }
        }
        if (exception != null) {
            throw exception;
        }
    }

    static {
        try {
            final Class jframe = Class.forName("javax.swing.JFrame");
            final Constructor constructor = jframe.getConstructor([String] as Class[]);
            constructor.newInstance(["testing"] as String[]);
            headless = false;
        } catch (Throwable t) {
            // any exception means headless
            headless = true;
        }
    }

}

Other Groovy examples (source code examples)

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