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

Groovy example source code file (NestedClassTest.groovy)

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

groovytestcase, groovytestcase, nestedclasstest, nestedclasstest, u, u

The Groovy NestedClassTest.groovy source code

package groovy

class NestedClassTest extends GroovyTestCase {

    void testStaticInnerStaticMethod () {
        def script = new GroovyClassLoader(getClass().getClassLoader()).parseClass ("""
        package groovy

        JavaClass.StaticInner.it
        """).newInstance()
        assertEquals 30, script.run()
    }

    void testStaticInnerInstanceMethod () {
        def script = new GroovyClassLoader(getClass().getClassLoader()).parseClass ("""
        package groovy

        new JavaClass.StaticInner ().result
        """).newInstance()
        assertEquals 239, script.run()
    }

    void testParam () {
        def script = new GroovyClassLoader(getClass().getClassLoader()).parseClass ("""
        package groovy

        def method (JavaClass.StaticInner obj) { obj.result }

        method new JavaClass.StaticInner ()
        """).newInstance()

        assertEquals 239, script.run()
    }

    void testTypeDecl () {
        def script = new GroovyClassLoader(getClass().getClassLoader()).parseClass ("""
        package groovy

        JavaClass.StaticInner method () { 239 }

        method ()
        """).newInstance()

        shouldFail (org.codehaus.groovy.runtime.typehandling.GroovyCastException) {
          assertEquals 239, script.run()
        }
    }

    void testFieldDecl () {
        def script = new GroovyClassLoader(getClass().getClassLoader()).parseClass ("""
        package groovy

        JavaClass.StaticInner field = 239

        field
        """).newInstance()

        shouldFail (org.codehaus.groovy.runtime.typehandling.GroovyCastException) {
          assertEquals 239, script.run()
        }
    }

    void testInstanceof () {
        def script = new GroovyClassLoader(getClass().getClassLoader()).parseClass ("""
        package groovy

        JavaClass.CONST instanceof JavaClass.StaticInner
        """).newInstance()

        assertTrue script.run ()
    }

    void testExtends () {
        def script = new GroovyClassLoader(getClass().getClassLoader()).parseClass ("""
        package groovy

        class U extends JavaClass.StaticInner.Inner2 {}

        new U ()

        """).newInstance()

        assert script.run () instanceof JavaClass.StaticInner.Inner2
    }
}

Other Groovy examples (source code examples)

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