<commit_before>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2017 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.blackbuild.groovy.configdsl.transform

import org.codehaus.groovy.control.MultipleCompilationErrorsException
import org.codehaus.groovy.runtime.typehandling.GroovyCastException
import spock.lang.Ignore

@SuppressWarnings("GroovyAssignabilityCheck")
class OwnerReferencesSpec extends AbstractDSLSpec {

    def "if owners is specified, no owner accessor is created"() {
        when:
        createClass('''
            package pk

            @DSL
            class Foo {
                Bar bar
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')

        then:
        getClass("pk.Bar").metaClass.getMetaMethod("owner", clazz) == null
    }

    def "error: two different owners in hierarchy"() {
        when:
        createClass('''
            package pk

            @DSL
            class Foo {
                Bar bar
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }

            @DSL(owner="owner2")
            class ChildBar extends Bar {
                @Owner Foo owner2
            }
        ''')

        then:
        thrown(MultipleCompilationErrorsException)
    }

    def "error: owner points to non existing field"() {
        when:
        createClass('''
            package pk

            @DSL
            class Foo {
                Bar bar
            }

            @DSL(owner="own")
            class Bar {
                @Owner Foo owner
            }
        ''')

        then:
        thrown(MultipleCompilationErrorsException)
    }

    @Ignore("Currently, we allow non dsl-owners (for example Object)")
    def "error: owner field is no dsl object"() {
        when:
        createClass('''
            package pk

            @DSL
            class Foo {
                Bar bar
            }

            @DSL
            class Bar {
                @Owner String owner
            }
        ''')

        then:
        thrown(MultipleCompilationErrorsException)
    }

    def "owner reference for single dsl object"() {
        given:
        createClass('''
            package pk

            @DSL
            class Foo {
                Bar bar
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')

        when:
        instance = clazz.create {
            bar {}
        }

        then:
        instance.bar.owner.is(instance)

        when:
        instance = clazz.create {
            bar(getClass("pk.Bar")) {}
        }

        then:
        instance.bar.owner.is(instance)
    }

    def "using of existing objects in list closure sets owner"() {
        given:
        createInstance('''
            package pk

            @DSL
            class Foo {
                List<Bar> bars
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')
        def aBar = create("pk.Bar") {}

        when:
        instance.apply {
            bars {
                bar aBar
            }
        }

        then:
        instance.bars[0].owner.is(instance)
    }

    def "reusing of objects in list closure does not set owner"() {
        given:
        createInstance('''
            package pk

            @DSL
            class Foo {
                List<Bar> bars
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')

        when:
        def aBar
        instance.bars {
            aBar = bar {}
        }

        def otherInstance = create("pk.Foo") {
            bars {
                bar(aBar)
            }
        }

        then: "bar's owner should still be the first object"
        otherInstance.bars[0].owner.is(instance)
    }

    def "reusing of existing objects in map closure does not set owner"() {
        given:
        createClass('''
            package pk

            @DSL
            class Foo {
                Bar bar
            }

            @DSL
            class Bar {
                @Key String name
                @Owner Object owner
            }

            @DSL
            class Fum {
                Map<String, Bar> bars
            }
        ''')

        when:
        def aBar
        instance = create("pk.Foo") {
            aBar = bar("Klaus") {}
        }

        create("pk.Fum") {
            bars {
                bar aBar
            }
        }

        then:
        aBar.owner.is(instance)
    }


    def "using existing objects in map closure sets owner"() {
        given:
        createInstance('''
            package pk

            @DSL
            class Foo {
                Map<String, Bar> bars
            }

            @DSL
            class Bar {
                @Key String name
                @Owner Foo owner
            }
        ''')
        def aBar = create("pk.Bar", "Klaus") {}

        when:
        instance.bars {
            bar(aBar)
        }

        then:
        instance.bars.Klaus.owner.is(instance)
    }

    def "owner reference for dsl object list"() {
        given:
        createClass('''
            package pk

            @DSL
            class Foo {
                List<Bar> bars
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')

        when:
        instance = clazz.create {
            bars {
                bar {}
                bar(getClass("pk.Bar")) {}
            }
        }

        then:
        instance.bars[0].owner.is(instance)
        instance.bars[1].owner.is(instance)
    }

    def "owner reference for dsl object map"() {
        given:
        createClass('''
            package pk

            @DSL
            class Foo {
                Map<String, Bar> bars
            }

            @DSL
            class Bar {
                @Owner Foo owner
                @Key String name
            }
        ''')

        when:
        instance = clazz.create {
            bars {
                bar("Klaus") {}
                bar(getClass("pk.Bar"), "Dieter") {}
            }
        }

        then:
        instance.bars.Klaus.owner.is(instance)
        instance.bars.Dieter.owner.is(instance)
    }

    def "owner will not be overridden"() {
        given:
        createInstance('''
            package pk

            @DSL
            class Foo {
                List<Bar> bars
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')
        def bar = create("pk.Bar") {}
        bar.owner = instance

        when:
        bar.owner = clazz.create {}

        then:
        bar.owner == instance
    }

    def "owner is not overriden in Maps"() {
        given:
        createInstance('''
            package pk

            @DSL
            class Foo {
                Map<String, Bar> bars
            }

            @DSL
            class Bar {
                @Key String name
                @Owner Foo owner
            }
        ''')
        def aBar = create("pk.Bar", "Klaus") {}
        def otherFoo = create("pk.Foo") {
            bar(aBar)
        }

        when:
        instance.bars {
            bar(aBar)
        }

        then:
        instance.bars.Klaus.owner.is(otherFoo)
    }

    def "owner is not overridden in Lists"() {
        given:
        createInstance('''
            package pk

            @DSL
            class Foo {
                List<Bar> bars
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')
        def aBar = create("pk.Bar") {}
        def otherFoo = create("pk.Foo") {
            bar(aBar)
        }

        when:
        instance.bars {
            bar(aBar)
        }

        then:
        instance.bars[0].owner.is(otherFoo)
    }


    def "bug: Reusing an object in a different structure throws ClassCastException"() {
        given:
        createInstance('''
            package pk

            @DSL
            class Foo {
                List<Bar> bars
            }

            @DSL
            class OtherOwner {
                List<Bar> bars
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')

        when:
        def aFoo = create("pk.Foo") {
            bars {
                bar {}
            }
        }

        def secondFoo = create("pk.OtherOwner") {
            bars {
                bar aFoo.bars[0]
            }
        }

        then:
        notThrown(GroovyCastException)
        secondFoo.bars[0].owner == aFoo
    }

    def "owner must be set before calling the closure"() {
        given:
        createClass('''
            package pk

            @DSL
            class Foo {
                Bar bar
                List<Bar> listBars
                Map<String, Keyed> keyeds
            }

            @DSL
            class Bar {
                @Owner Foo foo
            }

            @DSL
            class Keyed {
                @Owner Foo foo
                @Key String name
            }
        ''')

        when:
        instance = clazz.create {
            bar {
                assert foo != null
            }
            listBars {
                bar {
                    assert foo != null
                }
            }
            keyeds {
                keyed("bla") {
                    assert foo != null
                }
            }

        }

        then:
        noExceptionThrown()
    }
}
<commit_msg>Changed test to always use apply<commit_after>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2017 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.blackbuild.groovy.configdsl.transform

import org.codehaus.groovy.control.MultipleCompilationErrorsException
import org.codehaus.groovy.runtime.typehandling.GroovyCastException
import spock.lang.Ignore

@SuppressWarnings("GroovyAssignabilityCheck")
class OwnerReferencesSpec extends AbstractDSLSpec {

    def "if owners is specified, no owner accessor is created"() {
        when:
        createClass('''
            package pk

            @DSL
            class Foo {
                Bar bar
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')

        then:
        getClass("pk.Bar").metaClass.getMetaMethod("owner", clazz) == null
    }

    def "error: two different owners in hierarchy"() {
        when:
        createClass('''
            package pk

            @DSL
            class Foo {
                Bar bar
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }

            @DSL(owner="owner2")
            class ChildBar extends Bar {
                @Owner Foo owner2
            }
        ''')

        then:
        thrown(MultipleCompilationErrorsException)
    }

    def "error: owner points to non existing field"() {
        when:
        createClass('''
            package pk

            @DSL
            class Foo {
                Bar bar
            }

            @DSL(owner="own")
            class Bar {
                @Owner Foo owner
            }
        ''')

        then:
        thrown(MultipleCompilationErrorsException)
    }

    @Ignore("Currently, we allow non dsl-owners (for example Object)")
    def "error: owner field is no dsl object"() {
        when:
        createClass('''
            package pk

            @DSL
            class Foo {
                Bar bar
            }

            @DSL
            class Bar {
                @Owner String owner
            }
        ''')

        then:
        thrown(MultipleCompilationErrorsException)
    }

    def "owner reference for single dsl object"() {
        given:
        createClass('''
            package pk

            @DSL
            class Foo {
                Bar bar
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')

        when:
        instance = clazz.create {
            bar {}
        }

        then:
        instance.bar.owner.is(instance)

        when:
        instance = clazz.create {
            bar(getClass("pk.Bar")) {}
        }

        then:
        instance.bar.owner.is(instance)
    }

    def "using of existing objects in list closure sets owner"() {
        given:
        createInstance('''
            package pk

            @DSL
            class Foo {
                List<Bar> bars
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')
        def aBar = create("pk.Bar") {}

        when:
        instance.apply {
            bars {
                bar aBar
            }
        }

        then:
        instance.bars[0].owner.is(instance)
    }

    def "reusing of objects in list closure does not set owner"() {
        given:
        createInstance('''
            package pk

            @DSL
            class Foo {
                List<Bar> bars
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')

        when:
        def aBar
        instance.apply {
            bars {
                aBar = bar {}
            }
        }

        def otherInstance = create("pk.Foo") {
            bars {
                bar(aBar)
            }
        }

        then: "bar's owner should still be the first object"
        otherInstance.bars[0].owner.is(instance)
    }

    def "reusing of existing objects in map closure does not set owner"() {
        given:
        createClass('''
            package pk

            @DSL
            class Foo {
                Bar bar
            }

            @DSL
            class Bar {
                @Key String name
                @Owner Object owner
            }

            @DSL
            class Fum {
                Map<String, Bar> bars
            }
        ''')

        when:
        def aBar
        instance = create("pk.Foo") {
            aBar = bar("Klaus") {}
        }

        create("pk.Fum") {
            bars {
                bar aBar
            }
        }

        then:
        aBar.owner.is(instance)
    }


    def "using existing objects in map closure sets owner"() {
        given:
        createInstance('''
            package pk

            @DSL
            class Foo {
                Map<String, Bar> bars
            }

            @DSL
            class Bar {
                @Key String name
                @Owner Foo owner
            }
        ''')
        def aBar = create("pk.Bar", "Klaus") {}

        when:
        instance.apply {
            bars {
                bar(aBar)
            }
        }

        then:
        instance.bars.Klaus.owner.is(instance)
    }

    def "owner reference for dsl object list"() {
        given:
        createClass('''
            package pk

            @DSL
            class Foo {
                List<Bar> bars
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')

        when:
        instance = clazz.create {
            bars {
                bar {}
                bar(getClass("pk.Bar")) {}
            }
        }

        then:
        instance.bars[0].owner.is(instance)
        instance.bars[1].owner.is(instance)
    }

    def "owner reference for dsl object map"() {
        given:
        createClass('''
            package pk

            @DSL
            class Foo {
                Map<String, Bar> bars
            }

            @DSL
            class Bar {
                @Owner Foo owner
                @Key String name
            }
        ''')

        when:
        instance = clazz.create {
            bars {
                bar("Klaus") {}
                bar(getClass("pk.Bar"), "Dieter") {}
            }
        }

        then:
        instance.bars.Klaus.owner.is(instance)
        instance.bars.Dieter.owner.is(instance)
    }

    def "owner will not be overridden"() {
        given:
        createInstance('''
            package pk

            @DSL
            class Foo {
                List<Bar> bars
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')
        def bar = create("pk.Bar") {}
        bar.owner = instance

        when:
        bar.owner = clazz.create {}

        then:
        bar.owner == instance
    }

    def "owner is not overriden in Maps"() {
        given:
        createInstance('''
            package pk

            @DSL
            class Foo {
                Map<String, Bar> bars
            }

            @DSL
            class Bar {
                @Key String name
                @Owner Foo owner
            }
        ''')
        def aBar = create("pk.Bar", "Klaus") {}
        def otherFoo = create("pk.Foo") {
            bar(aBar)
        }

        when:
        instance.apply {
            bars {
                bar(aBar)
            }
        }

        then:
        instance.bars.Klaus.owner.is(otherFoo)
    }

    def "owner is not overridden in Lists"() {
        given:
        createInstance('''
            package pk

            @DSL
            class Foo {
                List<Bar> bars
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')
        def aBar = create("pk.Bar") {}
        def otherFoo = create("pk.Foo") {
            bar(aBar)
        }

        when:
        instance.apply {
            bars {
                bar(aBar)
            }
        }

        then:
        instance.bars[0].owner.is(otherFoo)
    }


    def "bug: Reusing an object in a different structure throws ClassCastException"() {
        given:
        createInstance('''
            package pk

            @DSL
            class Foo {
                List<Bar> bars
            }

            @DSL
            class OtherOwner {
                List<Bar> bars
            }

            @DSL
            class Bar {
                @Owner Foo owner
            }
        ''')

        when:
        def aFoo = create("pk.Foo") {
            bars {
                bar {}
            }
        }

        def secondFoo = create("pk.OtherOwner") {
            bars {
                bar aFoo.bars[0]
            }
        }

        then:
        notThrown(GroovyCastException)
        secondFoo.bars[0].owner == aFoo
    }

    def "owner must be set before calling the closure"() {
        given:
        createClass('''
            package pk

            @DSL
            class Foo {
                Bar bar
                List<Bar> listBars
                Map<String, Keyed> keyeds
            }

            @DSL
            class Bar {
                @Owner Foo foo
            }

            @DSL
            class Keyed {
                @Owner Foo foo
                @Key String name
            }
        ''')

        when:
        instance = clazz.create {
            bar {
                assert foo != null
            }
            listBars {
                bar {
                    assert foo != null
                }
            }
            keyeds {
                keyed("bla") {
                    assert foo != null
                }
            }

        }

        then:
        noExceptionThrown()
    }
}

{'fraction_non_alphanumeric': 0.08320663441603317, 'fraction_numerical': 0.0015664593411656301, 'mean_word_length': 1.030115974560419, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '423148', 'n_tokens_mistral': 5896, 'n_tokens_neox': 5421, 'n_words': 1571}