home | career | drupal | java | mac | mysql | perl | scala | uml | unix

Groovy example source code file (Groovy662Bug.groovy)

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

groovy662, groovy662_groovyclass, groovy662_groovyclass, groovytestcase, hashmap, hashmap, hello, hello, string, string

The Groovy Groovy662Bug.groovy source code

package groovy.bugs

//  The order of the classes is crucial, the first must be the GroovyTestCase.  Its name doesn't
//  matter it just has to be first.

/**
 * Test class and support to realize the GROOVY-662 test.  There is a difference between
 * improper uses of properties between Groovy defined classes and Java defined classes.  There
 * is no difference between correct uses so this is not a problem just an anti-regression test.
 *
 * @author Russel Winder
 * @version $Revision: 21369 $
 */
class Groovy662 extends GroovyTestCase {
    private String expected = "Hello"

    private usePropertyCorrectly(def object) { return object.@myProperty }

    private usePropertyIncorrectly(def object) { return object.myProperty }

    private useMethod(def object) { return object.getMyProperty() }

    private void doAssertions(def object) {
        assertTrue(useMethod(object) == expected)
        assertTrue(usePropertyCorrectly(object) == expected)
    }

    private String theTestScriptDefinitions = """
        String expected = "Hello"
        def usePropertyCorrectly ( def object ) { return object.@myProperty }
        def usePropertyIncorrectly ( def object ) { return object.myProperty }
        def useMethod ( def object ) { return object.getMyProperty ( ) }
    """

    private String theTestScriptAssertions = """
        assert useMethod ( object ) == expected
        assert usePropertyCorrectly ( object ) == expected
    """

    public void testJavaClass() {
        def object = new groovy.bugs.Groovy662_JavaClass()
        doAssertions(object)
        assertTrue(usePropertyIncorrectly(object) == null)
    }

    public void testGroovyClass() {
        def object = new Groovy662_GroovyClass()
        doAssertions(object)
        assertTrue(usePropertyIncorrectly(object) == null)
    }

    public void testJavaClassAsScript() {
        assertScript(theTestScriptDefinitions + """
            def object = new groovy.bugs.Groovy662_JavaClass ( )
        """ + theTestScriptAssertions + """
            assert usePropertyIncorrectly ( object ) == null
        """)
    }

    public void testGroovyClassAsScript() {
        assertScript(theTestScriptDefinitions + """
            class Groovy662_GroovyClass extends HashMap {
                String myProperty = "Hello"
                public String getMyProperty ( ) { return myProperty }
            }
            def object = new Groovy662_GroovyClass ( )
        """ + theTestScriptAssertions + """
            assert usePropertyIncorrectly ( object ) == null
        """)
    }
}

class Groovy662_GroovyClass extends HashMap {
    String myProperty = "Hello"

    public String getMyProperty() {
        return myProperty
    }
}

Other Groovy examples (source code examples)

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

new blog posts

 

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