|
| 1 | +/* |
| 2 | + * Copyright 2012 - present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.spring.initializr.generator.spring.build.gradle; |
| 18 | + |
| 19 | +import java.util.stream.Stream; |
| 20 | + |
| 21 | +import io.spring.initializr.generator.buildsystem.BuildSystem; |
| 22 | +import io.spring.initializr.generator.buildsystem.Dependency; |
| 23 | +import io.spring.initializr.generator.buildsystem.MavenRepository; |
| 24 | +import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSettings; |
| 25 | +import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem; |
| 26 | +import io.spring.initializr.generator.language.Language; |
| 27 | +import io.spring.initializr.generator.language.groovy.GroovyLanguage; |
| 28 | +import io.spring.initializr.generator.language.java.JavaLanguage; |
| 29 | +import io.spring.initializr.generator.language.kotlin.KotlinLanguage; |
| 30 | +import io.spring.initializr.generator.spring.AbstractComplianceTests; |
| 31 | +import io.spring.initializr.generator.spring.build.BuildCustomizer; |
| 32 | +import io.spring.initializr.generator.test.project.ProjectStructure; |
| 33 | +import io.spring.initializr.generator.version.VersionReference; |
| 34 | +import org.junit.jupiter.params.ParameterizedTest; |
| 35 | +import org.junit.jupiter.params.provider.Arguments; |
| 36 | +import org.junit.jupiter.params.provider.MethodSource; |
| 37 | + |
| 38 | +import org.springframework.core.io.ClassPathResource; |
| 39 | + |
| 40 | +import static org.assertj.core.api.Assertions.assertThat; |
| 41 | + |
| 42 | +/** |
| 43 | + * Gradle settings compliance tests. |
| 44 | + * |
| 45 | + * @author Sijun Yang |
| 46 | + */ |
| 47 | +class GradleSettingsComplianceTests extends AbstractComplianceTests { |
| 48 | + |
| 49 | + private static final String BOOT_VERSION = "3.5.0"; |
| 50 | + |
| 51 | + static Stream<Arguments> parameters() { |
| 52 | + return Stream.of(new JavaLanguage(), new GroovyLanguage(), new KotlinLanguage()) |
| 53 | + .flatMap((language) -> Stream.of( |
| 54 | + Arguments.of(language, BuildSystem.forId(GradleBuildSystem.ID), "settings.gradle"), |
| 55 | + Arguments.of(language, |
| 56 | + BuildSystem.forIdAndDialect(GradleBuildSystem.ID, GradleBuildSystem.DIALECT_KOTLIN), |
| 57 | + "settings.gradle.kts"))); |
| 58 | + } |
| 59 | + |
| 60 | + @ParameterizedTest |
| 61 | + @MethodSource("parameters") |
| 62 | + void defaultProjectSettings(Language language, BuildSystem build, String fileName) { |
| 63 | + ProjectStructure project = generateProject(language, build, BOOT_VERSION); |
| 64 | + String path = "project/gradle/" + getAssertFileName(fileName); |
| 65 | + assertThat(project).textFile(fileName).as("Resource " + path).hasSameContentAs(new ClassPathResource(path)); |
| 66 | + } |
| 67 | + |
| 68 | + @ParameterizedTest |
| 69 | + @MethodSource("parameters") |
| 70 | + void customArtifactId(Language language, BuildSystem build, String fileName) { |
| 71 | + ProjectStructure project = generateProject(language, build, BOOT_VERSION, |
| 72 | + (description) -> description.setArtifactId("my-project")); |
| 73 | + String path = "project/gradle/custom-artifact-id-" + getAssertFileName(fileName); |
| 74 | + assertThat(project).textFile(fileName).as("Resource " + path).hasSameContentAs(new ClassPathResource(path)); |
| 75 | + } |
| 76 | + |
| 77 | + @ParameterizedTest |
| 78 | + @MethodSource("parameters") |
| 79 | + void pluginRepository(Language language, BuildSystem build, String fileName) { |
| 80 | + ProjectStructure project = generateProject(language, build, BOOT_VERSION, (description) -> { |
| 81 | + }, (context) -> context.registerBean(BuildCustomizer.class, |
| 82 | + () -> (gradleBuild) -> gradleBuild.pluginRepositories() |
| 83 | + .add(MavenRepository.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone")))); |
| 84 | + String path = "project/gradle/plugin-repository-" + getAssertFileName(fileName); |
| 85 | + assertThat(project).textFile(fileName).as("Resource " + path).hasSameContentAs(new ClassPathResource(path)); |
| 86 | + } |
| 87 | + |
| 88 | + @ParameterizedTest |
| 89 | + @MethodSource("parameters") |
| 90 | + void pluginMapping(Language language, BuildSystem build, String fileName) { |
| 91 | + ProjectStructure project = generateProject(language, build, BOOT_VERSION, (description) -> { |
| 92 | + }, (context) -> context.registerBean(BuildCustomizer.class, |
| 93 | + () -> (gradleBuild) -> ((GradleBuildSettings.Builder) gradleBuild.settings()).mapPlugin("com.example", |
| 94 | + Dependency.withCoordinates("com.example", "gradle-plugin") |
| 95 | + .version(VersionReference.ofValue("1.0.0")) |
| 96 | + .build()))); |
| 97 | + String path = "project/gradle/plugin-mapping-" + getAssertFileName(fileName); |
| 98 | + assertThat(project).textFile(fileName).as("Resource " + path).hasSameContentAs(new ClassPathResource(path)); |
| 99 | + } |
| 100 | + |
| 101 | + private String getAssertFileName(String fileName) { |
| 102 | + return fileName + ".gen"; |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments