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

Groovy example source code file (StaticScopeTest.groovy)

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

a, a, b, b, c, c, class, class, compilabletestsupport, date, foo, missingmethodexception, pi, staticscopetest

The Groovy StaticScopeTest.groovy source code

package gls.scope

import gls.CompilableTestSupport

public class StaticScopeTest extends CompilableTestSupport {

    public void testNormalStaticScopeInScript() {
        shouldNotCompile """
        static foo() {
            foo = 1
        }
        """

        shouldCompile """
        static foo() {
            def foo=1
        }
        """
    }

    public void testStaticImportInclass() {
        assertScript """
        import static java.lang.Math.*
        class B {
            static main(args) { assert cos(2 * PI) == 1.0 }
        }
        """
    }

    public void testStaticMethodInConstructor() {
        assertScript """
        class B {
            def instanceVariable = 0
            static classVariable = 0
            B() { instanceVariable = method(); init() }
            static int method() { 1 }
            static int init() { classVariable = 2 }
        }
        assert new B().instanceVariable == 1
        assert B.classVariable == 2
        """
    }

    public void testStaticMethodInSpecialConstructorCall() {
        assertScript """
        class A {
            def instanceVariable = 0
            A(x) { instanceVariable = x }
            A(x, y) { this( method(x + y) ) }
            static int method(x) { 2 * x }
        }
        assert new A(2).instanceVariable == 2
        assert new A(2, 5).instanceVariable == 14
        """
        assertScript """
        class B {
            def instanceVariable = 0
            B(x) { instanceVariable = x }
        }
        class C extends B {
            C(x, y) { super( method(x + y) ) }
            static int method(x) { 2 * x }
        }
        assert new B(2).instanceVariable == 2
        assert new C(2, 5).instanceVariable == 14
        """
    }

    public void testStaticImportProperty() {
        assertScript """
        import static A.*
        class B {
            static main(args) { assert temperature == 42 }
        }
        class A { static temperature = 42 }
        """
    }

    public void testNormalStaticScopeInClass() {
        assertScript """
        class A {
            static i
            static foo() {
                i=1
            }
        }
        A.foo()
        assert A.i == 1
        """

        shouldNotCompile """
        class A {
            def i
            static foo() {
                i=1
            }
        }
        """
    }

    public void testClosureInStaticScope() {
        shouldCompile """
        5.times { foo=2 }
        """

        shouldCompile """
        5.times { foo=it }
        """
    }

    public void testScriptMethodCall() {
        assertScript """
        import static java.util.Calendar.getInstance as now
        def now = now().time
        assert now.class == Date
        """

        // TODO: why does in-lining of now variable from above break? GROOVY-1809 issue?
        shouldCompile """
        import static java.util.Calendar.getInstance as now
        assert now().time.class == Date
        """

        shouldCompile """
        import static java.lang.Math.*
        cos(cos(cos(PI)))
        """
    }

    public void testFullyQualifiedClassName() {
        assertScript """
        static foo() {java.lang.Integer}
        assert foo() == java.lang.Integer
        """

        shouldNotCompile """
        static foo() { java.lang.JavaOrGroovyThatsTheQuestion }
        """

        shouldCompile """
        foo() { java.lang.JavaOrGroovyThatsTheQuestion }
        """
    }

    public void testStaticPropertyInit() {
        // GROOVY-1910
        assertScript """
        class Foo {
             static p1 = 1
             static p2 = p1
        }
        assert Foo.p2 == Foo.p1
        assert Foo.p1 == 1
        """

        // should not compile for mistyped name
        shouldNotCompile """
        class Foo {
            static p1 = 1
            static p2 = x1
        }
        assert Foo.p2 == Foo.p1
        assert Foo.p1 == 1
        """
    }

    public void testSpecialConstructorAccess() {
        shouldCompile """
        class A{ A(x){} }
        class B extends A {
            B(x) { super(x) }
        }
        """

        shouldCompile """
        class A{ A(x){} }
        class B extends A {
            B(x) { super(x.something) }
        }
        """

        shouldNotCompile """
        class A{ A(x){} }
        class B extends A {
            B(x) { super(nonExistingParameter) }
        }
        """

        shouldNotCompile """
        class A{ A(x){} }
        class B extends A {
            def doNotAccessDynamicFieldsOrProperties
            B(x) { super(doNotAccessDynamicFieldsOrProperties) }
        }
        """

        shouldCompile """
        class A{ A(x){} }
        class B extends A {
            static allowUsageOfStaticPropertiesAndFields
            B(x) { super(allowUsageOfStaticPropertiesAndFields) }
        }
        """
    }
    
    public void testStaticMethodAccessingDynamicField() {
        shouldFail MissingMethodException, """
            class A {
                def x = { }
                static foo() { x() }
            } 
            A.foo()
        """
    }
    
    public void testStaticThisWithClass() {
        assertScript """
            static foo(){this}
            assert foo() instanceof Class
            assert foo() != Class
        """
        
        assertScript """
            static foo(){this.class}
            assert foo() == Class
        """
    }    
}

Other Groovy examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.