All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.api.PluginServiceInjectionIntegrationTest.groovy Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2018 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 org.gradle.api

import org.gradle.api.internal.GeneratedSubclass
import org.gradle.api.plugins.ExtensionAware
import org.gradle.integtests.fixtures.AbstractIntegrationSpec

import javax.inject.Inject


class PluginServiceInjectionIntegrationTest extends AbstractIntegrationSpec {
    def setup() {
        buildFile << """
            import ${Inject.name}
        """
    }

    def "can apply a plugin with @Inject services constructor arg"() {
        buildFile << """
            class CustomPlugin implements Plugin {
                private final WorkerExecutor executor

                @Inject
                CustomPlugin(WorkerExecutor executor) {
                    this.executor = executor
                }
                
                void apply(Project p) {
                    println(executor != null ? "got it" : "NOT IT")

                    // is not generated
                    assert getClass() == CustomPlugin
                }
            }
            
            apply plugin: CustomPlugin
        """

        expect:
        succeeds()
        outputContains("got it")
    }

    def "fails when plugin constructor is not annotated with @Inject"() {
        buildFile << """
            class CustomPlugin implements Plugin {
                CustomPlugin(WorkerExecutor executor) {
                }
                
                void apply(Project p) {
                }
            }
            
            apply plugin: CustomPlugin
        """

        expect:
        fails()
        failure.assertHasCause("Failed to apply plugin [class 'CustomPlugin']")
        failure.assertHasCause("Could not create plugin of type 'CustomPlugin'.")
        failure.assertHasCause("The constructor for class CustomPlugin should be annotated with @Inject.")
    }

    def "fails when plugin constructor requests unknown service"() {
        buildFile << """
            interface Unknown { }
            
            class CustomPlugin implements Plugin {
                @Inject
                CustomPlugin(Unknown x) {
                }
                
                void apply(Project p) {
                }
            }
            
            apply plugin: CustomPlugin
        """

        expect:
        fails()
        failure.assertHasCause("Failed to apply plugin [class 'CustomPlugin']")
        failure.assertHasCause("Could not create plugin of type 'CustomPlugin'.")
        failure.assertHasCause("Unable to determine constructor argument #1: missing parameter of interface Unknown, or no service of type interface Unknown")
    }

    def "can inject service using getter method"() {
        buildFile << """
            class CustomPlugin implements Plugin {
                @Inject
                WorkerExecutor getExecutor() { }
                
                void apply(Project p) {
                    println(executor != null ? "got it" : "NOT IT")

                    // is generated but not extensible
                    assert getClass() != CustomPlugin
                    assert (this instanceof ${GeneratedSubclass.name}) 
                    assert !(this instanceof ${ExtensionAware.name}) 
                }
            }
            
            apply plugin: CustomPlugin
        """

        expect:
        succeeds()
        outputContains("got it")
    }

    def "can inject service using abstract getter method"() {
        buildFile << """
            abstract class CustomPlugin implements Plugin {
                @Inject
                abstract WorkerExecutor getExecutor()
                
                void apply(Project p) {
                    println(executor != null ? "got it" : "NOT IT")

                    // is generated but not extensible
                    assert getClass() != CustomPlugin
                    assert (this instanceof ${GeneratedSubclass.name}) 
                    assert !(this instanceof ${ExtensionAware.name}) 
                }
            }
            
            apply plugin: CustomPlugin
        """

        expect:
        succeeds()
        outputContains("got it")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy