diff --git a/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/GradleBuildAssert.java b/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/GradleBuildAssert.java new file mode 100644 index 00000000..0f1765e0 --- /dev/null +++ b/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/GradleBuildAssert.java @@ -0,0 +1,80 @@ +/* + * Copyright 2012-2023 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 + * + * https://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 io.spring.initializr.generator.test.buildsystem.gradle; + +import io.spring.initializr.generator.test.io.AbstractTextAssert; + +/** + * Base class for Gradle build assertions. + * + * @param the type of the concrete assert implementations + * @author Stephane Nicoll + */ +public abstract class GradleBuildAssert> extends AbstractTextAssert { + + protected GradleBuildAssert(String content, Class selfType) { + super(content, selfType); + } + + /** + * Assert the Gradle {@code build} uses the specified {@code version}. + * @param version the version of the build + * @return {@code this} assertion object + */ + public SELF hasVersion(String version) { + return hasProperty("version", version); + } + + /** + * Assert the Gradle {@code build} uses a source compatibility for the specified java + * version. + * @param javaVersion the java version + * @return {@code this} assertion object + */ + public SELF hasSourceCompatibility(String javaVersion) { + return hasProperty("sourceCompatibility", javaVersion); + } + + /** + * Assert the Gradle {@code build} defines a top-level property with the specified + * name and value. + * @param name the name of the property + * @param value the value + * @return {@code this} assertion object + */ + public SELF hasProperty(String name, String value) { + return contains(String.format("%s = '%s'", name, value)); + } + + /** + * Assert the Gradle {@code build} contains only the specified properties. + * @param values the property value pairs + * @return {@code this} assertion object + */ + public SELF containsOnlyExtProperties(String... values) { + StringBuilder builder = new StringBuilder(String.format("ext {%n")); + if (values.length % 2 == 1) { + throw new IllegalArgumentException("Size must be even, it is a set of property=value pairs"); + } + for (int i = 0; i < values.length; i += 2) { + builder.append(String.format("\tset('%s', \"%s\")%n", values[i], values[i + 1])); + } + builder.append("}"); + return contains(builder.toString()); + } + +} diff --git a/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/GradleSettingsAssert.java b/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/GradleSettingsAssert.java new file mode 100644 index 00000000..8a3c04cb --- /dev/null +++ b/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/GradleSettingsAssert.java @@ -0,0 +1,53 @@ +/* + * Copyright 2012-2023 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 + * + * https://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 io.spring.initializr.generator.test.buildsystem.gradle; + +import io.spring.initializr.generator.test.io.AbstractTextAssert; + +/** + * Base class for Gradle settings assertions. + * + * @param the type of the concrete assert implementations + * @author Stephane Nicoll + */ +public class GradleSettingsAssert> extends AbstractTextAssert { + + protected GradleSettingsAssert(String actual, Class selfType) { + super(actual, selfType); + } + + /** + * Assert the Gradle {@code settings} defines the specified project name. + * @param name the name of the project + * @return {@code this} assertion object + */ + public SELF hasProjectName(String name) { + return hasProperty("rootProject.name", name); + } + + /** + * Assert the Gradle {@code settings} defines a property with the specified name and + * value. + * @param name the name of the property + * @param value the value + * @return {@code this} assertion object + */ + public SELF hasProperty(String name, String value) { + return contains(String.format("%s = '%s", name, value)); + } + +} diff --git a/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/GroovyDslGradleBuildAssert.java b/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/GroovyDslGradleBuildAssert.java index de4b8d5d..1aa619b3 100644 --- a/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/GroovyDslGradleBuildAssert.java +++ b/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/GroovyDslGradleBuildAssert.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2023 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. @@ -18,7 +18,6 @@ package io.spring.initializr.generator.test.buildsystem.gradle; import java.nio.file.Path; -import io.spring.initializr.generator.test.io.AbstractTextAssert; import io.spring.initializr.generator.test.io.TextTestUtils; /** @@ -26,7 +25,7 @@ import io.spring.initializr.generator.test.io.TextTestUtils; * * @author Stephane Nicoll */ -public class GroovyDslGradleBuildAssert extends AbstractTextAssert { +public class GroovyDslGradleBuildAssert extends GradleBuildAssert { public GroovyDslGradleBuildAssert(String content) { super(content, GroovyDslGradleBuildAssert.class); @@ -55,51 +54,4 @@ public class GroovyDslGradleBuildAssert extends AbstractTextAssert { +public class GroovyDslGradleSettingsAssert extends GradleSettingsAssert { public GroovyDslGradleSettingsAssert(String content) { super(content, GroovyDslGradleSettingsAssert.class); } - /** - * Assert {@code settings.gradle} defines the specified project name. - * @param name the name of the project - * @return {@code this} assertion object - */ - public GroovyDslGradleSettingsAssert hasProjectName(String name) { - return hasProperty("rootProject.name", name); - } - - /** - * Assert {@code settings.gradle} defines a property with the specified name and - * value. - * @param name the name of the property - * @param value the value - * @return {@code this} assertion object - */ - public GroovyDslGradleSettingsAssert hasProperty(String name, String value) { - return contains(String.format("%s = '%s", name, value)); - } - } diff --git a/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/KotlinDslGradleBuildAssert.java b/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/KotlinDslGradleBuildAssert.java new file mode 100644 index 00000000..7c9915e3 --- /dev/null +++ b/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/KotlinDslGradleBuildAssert.java @@ -0,0 +1,57 @@ +/* + * Copyright 2012-2023 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 + * + * https://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 io.spring.initializr.generator.test.buildsystem.gradle; + +import java.nio.file.Path; + +import io.spring.initializr.generator.test.io.TextTestUtils; + +/** + * Simple assertions for a kotlin build using the kotlin DSL. + * + * @author Prithvi singh + */ +public class KotlinDslGradleBuildAssert extends GradleBuildAssert { + + public KotlinDslGradleBuildAssert(String content) { + super(content, KotlinDslGradleBuildAssert.class); + } + + public KotlinDslGradleBuildAssert(Path buildGradleFile) { + this(TextTestUtils.readContent(buildGradleFile)); + } + + /** + * Assert {@code build.gradle.kts} defines a plugin with the specified id and version. + * @param id the id of the plugin + * @param version the version of the plugin + * @return {@code this} assertion object + */ + public KotlinDslGradleBuildAssert hasPlugin(String id, String version) { + return contains(String.format("id('%s') version '%s'", id, version)); + } + + /** + * Assert {@code build.gradle.kts} defines a plugin with the specified id. + * @param id the id of the plugin + * @return {@code this} assertion object + */ + public KotlinDslGradleBuildAssert hasPlugin(String id) { + return contains(String.format("id('%s')", id)); + } + +} diff --git a/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/KotlinDslGradleSettingsAssert.java b/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/KotlinDslGradleSettingsAssert.java new file mode 100644 index 00000000..308cb482 --- /dev/null +++ b/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/buildsystem/gradle/KotlinDslGradleSettingsAssert.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2023 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 + * + * https://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 io.spring.initializr.generator.test.buildsystem.gradle; + +/** + * Simple assertions for a gradle settings using the Kotlin DSL. + * + * @author Prithvi singh + */ +public class KotlinDslGradleSettingsAssert extends GradleSettingsAssert { + + protected KotlinDslGradleSettingsAssert(String content) { + super(content, KotlinDslGradleSettingsAssert.class); + } + +} diff --git a/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/project/AbstractModuleAssert.java b/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/project/AbstractModuleAssert.java index 15208da4..7de97a4d 100644 --- a/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/project/AbstractModuleAssert.java +++ b/initializr-generator-test/src/main/java/io/spring/initializr/generator/test/project/AbstractModuleAssert.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2023 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. @@ -20,6 +20,7 @@ import java.nio.file.Path; import io.spring.initializr.generator.language.Language; import io.spring.initializr.generator.test.buildsystem.gradle.GroovyDslGradleBuildAssert; +import io.spring.initializr.generator.test.buildsystem.gradle.KotlinDslGradleBuildAssert; import io.spring.initializr.generator.test.buildsystem.maven.MavenBuildAssert; /** @@ -108,4 +109,24 @@ public abstract class AbstractModuleAssert assertThat(forSampleGradleBuild()).hasPlugin("com.another", "1.0.0.RELEASE")); + } + + @Test + void hasPluginWrongValue() { + assertThatExceptionOfType(AssertionError.class) + .isThrownBy(() -> assertThat(forSampleGradleBuild()).hasPlugin("com.example", "2.0.0.RELEASE")); + } + + @Test + void hasVersion() { + assertThat(forSampleGradleBuild()).hasVersion("0.0.1-SNAPSHOT"); + } + + @Test + void hasVersionWithWrongValue() { + assertThatExceptionOfType(AssertionError.class) + .isThrownBy(() -> assertThat(forSampleGradleBuild()).hasVersion("0.0.3-SNAPSHOT")); + } + + @Test + void hasSourceCompatibility() { + assertThat(forSampleGradleBuild()).hasSourceCompatibility("1.8"); + } + + @Test + void hasSourceCompatibilityWithWrongValue() { + assertThatExceptionOfType(AssertionError.class) + .isThrownBy(() -> assertThat(forSampleGradleBuild()).hasSourceCompatibility("11")); + } + + @Test + void containsOnlyExtProperties() { + assertThat(forSampleGradleBuild()).containsOnlyExtProperties("acmeVersion", "Brussels.SR2"); + } + + @Test + void containsOnlyExtPropertiesWithExtraValue() { + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThat(forSampleGradleBuild()) + .containsOnlyExtProperties("acmeVersion", "Brussels.SR2", "wrong", "1.0.0")); + } + + private AssertProvider forSampleGradleBuild() { + String path = "project/build/gradle/sample-build.gradle.kts"; + try (InputStream in = new ClassPathResource(path).getInputStream()) { + String content = StreamUtils.copyToString(in, StandardCharsets.UTF_8); + return () -> new KotlinDslGradleBuildAssert(content); + } + catch (IOException ex) { + throw new IllegalStateException("No content found at " + path, ex); + } + } + +} diff --git a/initializr-generator-test/src/test/java/io/spring/initializr/generator/test/buildsystem/gradle/KotlinDslGradleSettingsAssertTests.java b/initializr-generator-test/src/test/java/io/spring/initializr/generator/test/buildsystem/gradle/KotlinDslGradleSettingsAssertTests.java new file mode 100644 index 00000000..ab6b1f6e --- /dev/null +++ b/initializr-generator-test/src/test/java/io/spring/initializr/generator/test/buildsystem/gradle/KotlinDslGradleSettingsAssertTests.java @@ -0,0 +1,61 @@ +/* + * Copyright 2012-2023 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 + * + * https://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 io.spring.initializr.generator.test.buildsystem.gradle; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +import org.assertj.core.api.AssertProvider; +import org.junit.jupiter.api.Test; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.util.StreamUtils; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests for {@link KotlinDslGradleSettingsAssert} + * + * @author Prithvi singh + */ +class KotlinDslGradleSettingsAssertTests { + + @Test + void hasProjectName() { + assertThat(forSampleGradleSettings()).hasProjectName("demo"); + } + + @Test + void hasProjectNameWithWrongValue() { + assertThatExceptionOfType(AssertionError.class) + .isThrownBy(() -> assertThat(forSampleGradleSettings()).hasProjectName("another-project")); + } + + private AssertProvider forSampleGradleSettings() { + String path = "project/build/gradle/sample-settings.gradle.kts"; + try (InputStream in = new ClassPathResource(path).getInputStream()) { + String content = StreamUtils.copyToString(in, StandardCharsets.UTF_8); + return () -> new KotlinDslGradleSettingsAssert(content); + } + catch (IOException ex) { + throw new IllegalStateException("No content found at " + path, ex); + } + } + +} diff --git a/initializr-generator-test/src/test/java/io/spring/initializr/generator/test/project/ModuleAssertTests.java b/initializr-generator-test/src/test/java/io/spring/initializr/generator/test/project/ModuleAssertTests.java index a907d205..5b72e63f 100644 --- a/initializr-generator-test/src/test/java/io/spring/initializr/generator/test/project/ModuleAssertTests.java +++ b/initializr-generator-test/src/test/java/io/spring/initializr/generator/test/project/ModuleAssertTests.java @@ -188,6 +188,19 @@ class ModuleAssertTests { .isThrownBy(() -> assertThat(forDirectory(dir)).hasGroovyDslGradleBuild()); } + @Test + void hasKotlinDslGradleBuild(@TempDir Path dir) throws IOException { + createFiles(dir, "build.gradle.kts"); + assertThat(forDirectory(dir)).hasKotlinDslGradleBuild(); + } + + @Test + void hasKotlinDslGradleBuildWithMissingBuildFile(@TempDir Path dir) throws IOException { + createFiles(dir, "build.gradle.wrongkts"); + assertThatExceptionOfType(AssertionError.class) + .isThrownBy(() -> assertThat(forDirectory(dir)).hasKotlinDslGradleBuild()); + } + @Test void hasGradleWrapper(@TempDir Path dir) throws IOException { createFiles(dir, "gradlew", "gradlew.bat", "gradle/wrapper/gradle-wrapper.properties", @@ -221,6 +234,19 @@ class ModuleAssertTests { .isThrownBy(() -> assertThat(forDirectory(dir)).groovyDslGradleBuild()); } + @Test + void kotlinDslGradleBuild(@TempDir Path dir) throws IOException { + createFileFrom(new ClassPathResource("project/build/gradle/sample-build.gradle.kts"), + dir.resolve("build.gradle.kts")); + assertThat(forDirectory(dir)).kotlinDslGradleBuild().hasVersion("0.0.1-SNAPSHOT").hasSourceCompatibility("1.8"); + } + + @Test + void kotlinDslGradleBuildWithMissingBuildFile(@TempDir Path dir) { + assertThatExceptionOfType(AssertionError.class) + .isThrownBy(() -> assertThat(forDirectory(dir)).kotlinDslGradleBuild()); + } + private AssertProvider forDirectory(Path projectDirectory) { return () -> new ModuleAssert(projectDirectory); } diff --git a/initializr-generator-test/src/test/resources/project/build/gradle/sample-build.gradle.kts b/initializr-generator-test/src/test/resources/project/build/gradle/sample-build.gradle.kts new file mode 100644 index 00000000..97ac3467 --- /dev/null +++ b/initializr-generator-test/src/test/resources/project/build/gradle/sample-build.gradle.kts @@ -0,0 +1,28 @@ +plugins { + id('com.example') version '1.0.0.RELEASE' + id('java') +} + +group = 'com.example' +version = '0.0.1-SNAPSHOT' +sourceCompatibility = '1.8' + +repositories { + mavenCentral() +} + +ext { + set('acmeVersion', "Brussels.SR2") +} + +dependencies { + implementation 'com.example.acme:library' + testImplementation 'com.example.acme:library-test' +} + +dependencyManagement { + imports { + mavenBom "com.example.acme:library-bom:${acmeVersion}" + } +} + diff --git a/initializr-generator-test/src/test/resources/project/build/gradle/sample-settings.gradle.kts b/initializr-generator-test/src/test/resources/project/build/gradle/sample-settings.gradle.kts new file mode 100644 index 00000000..3d1932fe --- /dev/null +++ b/initializr-generator-test/src/test/resources/project/build/gradle/sample-settings.gradle.kts @@ -0,0 +1 @@ +rootProject.name = 'demo' \ No newline at end of file