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

org.gradle.buildinit.plugins.internal.maven.MavenProjectsCreatorSpec.groovy Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2012 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.buildinit.plugins.internal.maven

import org.gradle.api.internal.artifacts.mvnsettings.DefaultMavenSettingsProvider
import org.gradle.api.internal.artifacts.mvnsettings.MavenFileLocations
import org.gradle.buildinit.plugins.internal.BuildScriptBuilderFactory
import org.gradle.test.fixtures.file.TestNameTestDirectoryProvider
import org.junit.Rule
import spock.lang.Specification

class MavenProjectsCreatorSpec extends Specification {

    @Rule TestNameTestDirectoryProvider temp = new TestNameTestDirectoryProvider(getClass())
    private settings = new DefaultMavenSettingsProvider({} as MavenFileLocations)
    private creator = new MavenProjectsCreator()

    def "creates single module project"() {
        given:
        def pom = temp.file("pom.xml")
        pom.text = """
  4.0.0
  util
  util
  2.5
  jar
"""

        when:
        def mavenProjects = creator.create(settings.buildSettings(), pom) as List

        then:
        mavenProjects.size() == 1
        mavenProjects[0].name == 'util'
    }

    def "creates multi module project"() {
        given:
        def parentPom = temp.file("pom.xml")
        parentPom.text = """
  4.0.0
  org.gradle.webinar
  webinar-parent
  1.0-SNAPSHOT
  pom

  
    webinar-api
  

"""

        temp.file("webinar-api/pom.xml").text = """
  4.0.0
  webinar-api
  jar

  
    org.gradle.webinar
    webinar-parent
    1.0-SNAPSHOT
  

"""

        when:
        def mavenProjects = creator.create(settings.buildSettings(), parentPom) as List

        then:
        mavenProjects.size() == 2
        mavenProjects[0].name == 'webinar-parent'
        mavenProjects[1].name == 'webinar-api'
    }

    def "fails with decent exception if pom is incorrect"() {
        given:
        def pom = temp.file("pom.xml")
        pom.text = """
  4.0.0
  util
  2.5
  jar
"""

        when:
        creator.create(settings.buildSettings(), pom) as List

        then:
        def ex = thrown(MavenConversionException)
        ex.message == "Unable to create Maven project model using POM $pom."
    }

    def "fails with decent exception if pom does not exist"() {
        def pom = temp.file("pom.xml")

        when:
        creator.create(settings.buildSettings(), pom) as List

        then:
        def ex = thrown(MavenConversionException)
        ex.message == "Unable to create Maven project model. The POM file $pom does not exist."
    }

    def "creates multi module project with same artifactId"() {
        given:
        def parentPom = temp.file("pom.xml")
        parentPom.text = """\

  4.0.0
  pom
  org.gradle
  test
  0

  
    commons
  

"""

        temp.file("commons/pom.xml").text = """\

  
    org.gradle
    test
    0
  

  4.0.0
  commons
  pom

  
    commons
  


"""
        temp.file("commons/commons/pom.xml").text = """\

  
    org.gradle
    commons
    0
  

  4.0.0
  org.gradle.commons
  commons


"""
        def mavenProjects = creator.create(settings.buildSettings(), parentPom)
        def converter = new Maven2Gradle(mavenProjects, temp.testDirectory, Stub(BuildScriptBuilderFactory))

        expect:
        converter.convert()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy