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

Groovy example source code file (ClosureResolvingTest.groovy)

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

closureresolvingtest, groovytestcase, i'm, i'm, string, testresolve1, testresolve1, testresolve2, testresolve2, testresolve3, testresolve3

The Groovy ClosureResolvingTest.groovy source code

/*
 * Copyright 2003-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovy.lang

/**
 * Tests how closures resolve to either a delegate or an owner for a given resolveStrategy

 * @author Graeme Rocher
 * @since 1.5
 */

class ClosureResolvingTest extends GroovyTestCase {

    def foo = "bar"
    def bar = "foo"

    protected void tearDown() {
        Closure.metaClass = null
    }

    void testResolveToSelf() {
        def c = { foo }
        assertEquals "bar", c.call()

        c.resolveStrategy = Closure.TO_SELF

        shouldFail {
            c.call()
        }

        def metaClass = c.class.metaClass
        metaClass.getFoo = {-> "hello!" }

        c.metaClass = metaClass

        assertEquals "hello!", c.call()

        c = { doStuff() }
        c.resolveStrategy = Closure.TO_SELF
        shouldFail {
            c.call()
        }
        metaClass = c.class.metaClass
        metaClass.doStuff = {-> "hello" }
        c.metaClass = metaClass

        assertEquals "hello", c.call()
    }

    def doStuff() { "stuff" }

    void testResolveDelegateFirst() {

        def c = { foo }

        assertEquals "bar", c.call()

        c.setResolveStrategy(Closure.DELEGATE_FIRST)
        c.delegate = [foo: "hello!"]

        assertEquals "hello!", c.call()


        c = { doStuff() }
        c.setResolveStrategy(Closure.DELEGATE_FIRST)

        assertEquals "stuff", c.call()
        c.delegate = new TestResolve1()
        assertEquals "foo", c.call()

    }

    void testResolveOwnerFirst() {
        def c = { foo }

        assertEquals "bar", c.call()

        c.delegate = [foo: "hello!"]

        assertEquals "bar", c.call()

        c = { doStuff() }
        c.delegate = new TestResolve1()
        assertEquals "stuff", c.call()
    }

    void testResolveDelegateOnly() {
        def c = { foo + bar }

        assertEquals "barfoo", c.call()

        c.resolveStrategy = Closure.DELEGATE_FIRST

        c.delegate = new TestResolve1()

        assertEquals "hellofoo", c.call()

        c.resolveStrategy = Closure.DELEGATE_ONLY
        shouldFail {
            c.call()
        }

        c.delegate = new TestResolve2()

        assertEquals "helloworld", c.call()

        c = { doStuff() }
        c.resolveStrategy = Closure.DELEGATE_ONLY
        c.delegate = new TestResolve1()
        assertEquals "foo", c.call()

    }

    void testResolveOwnerOnly() {
        def c = { foo + bar }

        assertEquals "barfoo", c.call()
        c.resolveStrategy = Closure.OWNER_ONLY

        c.delegate = new TestResolve2()
        assertEquals "barfoo", c.call()

        c = { doStuff() }
        assertEquals "stuff", c.call()
        c.resolveStrategy = Closure.OWNER_ONLY
        c.delegate = new TestResolve1()
        assertEquals "stuff", c.call()

    }

    void testOwnerDelegateChain() {
        def outerdel = new TestResolve3(del: "outer delegate")
        def innerdel = new TestResolve3(del: "inner delegate")

        def cout = {
            assert delegate == outerdel
            assert delegate.whoisThis() == outerdel
            assert delegate.del == "outer delegate"
            assert delegate.met() == "I'm the method inside 'outer delegate'"

            assert whoisThis() == outerdel
            assert del == "outer delegate"
            assert met() == "I'm the method inside 'outer delegate'"

            def cin = {
                assert delegate == innerdel
                assert delegate.whoisThis() == innerdel
                assert delegate.del == "inner delegate"
                assert delegate.met() == "I'm the method inside 'inner delegate'"

                assert whoisThis() == outerdel
                assert del == "outer delegate"
                assert met() == "I'm the method inside 'outer delegate'"

            }

            cin.delegate = innerdel
            cin()
        }

        cout.delegate = outerdel
        cout()
    }

}

class TestResolve1 {
    def foo = "hello"

    def doStuff() { "foo" }
}
class TestResolve2 {
    def foo = "hello"
    def bar = "world"

    def doStuff() { "bar" }
}

class TestResolve3 {
    def del;

    String toString() {del}

    def whoisThis() { return this }

    def met() { return "I'm the method inside '" + del + "'" }
}

Other Groovy examples (source code examples)

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