/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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 org.codehaus.groovy.transform

import org.codehaus.groovy.control.CompilationFailedException
import org.junit.jupiter.api.Test

import static groovy.test.GroovyAssert.assertScript
import static groovy.test.GroovyAssert.shouldFail

/**
 * Tests for the {@code @Final} transformation.
 */
final class FinalTransformTest {

    private final GroovyShell shell = GroovyShell.withConfig {
        imports {
            staticMember 'java.lang.reflect.Modifier','isFinal'
            star 'groovy.transform'
        }
    }

    @Test
    void testUsageInAnnoationCollectorForClass() {
        assertScript shell, '''
            @AnnotationCollector
            @Canonical
            @Final
            @interface MyCanonical {
            }
            @MyCanonical class Foo {
            }

            assert isFinal(Foo.modifiers)
        '''
    }

    @Test
    void testUsageDirectDisabled() {
        assertScript shell, '''
            @Final(enabled=false)
            class Foo {
            }

            assert !isFinal(Foo.modifiers)
        '''
    }

    @Test
    void testUsageInAnnoationCollectorForClassDisabled() {
        assertScript shell, '''
            @AnnotationCollector(mode=AnnotationCollectorMode.PREFER_EXPLICIT_MERGED)
            @Canonical
            @Final
            @interface MyCanonical {
            }
            @MyCanonical
            @Final(enabled=false)
            class Foo {
            }

            assert !isFinal(Foo.modifiers)
        '''
    }

    @Test
    void testUsageInAnnoationCollectorForMethod() {
        assertScript shell, '''import static groovy.test.GroovyAssert.shouldFail
            @AnnotationCollector
            @NullCheck
            @Final
            @interface MyNullCheck {
            }
            class Foo {
                @MyNullCheck
                String upper(String s) {
                    s.toUpperCase()
                }
            }

            assert isFinal(Foo.getMethod('upper', String).modifiers)
            def foo = new Foo()
            assert foo.upper('foo') == 'FOO'
            shouldFail(IllegalArgumentException) {
                foo.upper(null)
            }
        '''
    }

    @Test
    void testDirectClassUsage() {
        def err = shouldFail shell, CompilationFailedException, '''
            @Final class Foo {
            }
            class Bar extends Foo {
            }
        '''
        assert err =~ /You are not allowed to extend the final class 'Foo'/
    }

    // GROOVY-11860
    @Test
    void testDirectConstructorUsage() {
        def err = shouldFail shell, CompilationFailedException, '''
            class C {
                @Final C() {
                }
            }
        '''
        assert err =~ /Error during Final processing: cannot modify a constructor/
    }
}
