mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-20 02:29:44 +08:00
Merge pull request #1412 from prithvitewatia
* pr/1412: Polish "Add build assertion support for Gradle with the Kotlin DSL" Add build assertion support for Gradle with the Kotlin DSL Closes gh-1412
This commit is contained in:
@@ -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 <SELF> the type of the concrete assert implementations
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
*/
|
||||||
|
public abstract class GradleBuildAssert<SELF extends GradleBuildAssert<SELF>> extends AbstractTextAssert<SELF> {
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -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 <SELF> the type of the concrete assert implementations
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
*/
|
||||||
|
public class GradleSettingsAssert<SELF extends GradleSettingsAssert<SELF>> extends AbstractTextAssert<SELF> {
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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 java.nio.file.Path;
|
||||||
|
|
||||||
import io.spring.initializr.generator.test.io.AbstractTextAssert;
|
|
||||||
import io.spring.initializr.generator.test.io.TextTestUtils;
|
import io.spring.initializr.generator.test.io.TextTestUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -26,7 +25,7 @@ import io.spring.initializr.generator.test.io.TextTestUtils;
|
|||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
public class GroovyDslGradleBuildAssert extends AbstractTextAssert<GroovyDslGradleBuildAssert> {
|
public class GroovyDslGradleBuildAssert extends GradleBuildAssert<GroovyDslGradleBuildAssert> {
|
||||||
|
|
||||||
public GroovyDslGradleBuildAssert(String content) {
|
public GroovyDslGradleBuildAssert(String content) {
|
||||||
super(content, GroovyDslGradleBuildAssert.class);
|
super(content, GroovyDslGradleBuildAssert.class);
|
||||||
@@ -55,51 +54,4 @@ public class GroovyDslGradleBuildAssert extends AbstractTextAssert<GroovyDslGrad
|
|||||||
return contains(String.format("id '%s'", id));
|
return contains(String.format("id '%s'", id));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Assert {@code build.gradle} uses the specified {@code version}.
|
|
||||||
* @param version the version of the build
|
|
||||||
* @return {@code this} assertion object
|
|
||||||
*/
|
|
||||||
public GroovyDslGradleBuildAssert hasVersion(String version) {
|
|
||||||
return hasProperty("version", version);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Assert {@code build.gradle} uses a source compatibility for the specified java
|
|
||||||
* version.
|
|
||||||
* @param javaVersion the java version
|
|
||||||
* @return {@code this} assertion object
|
|
||||||
*/
|
|
||||||
public GroovyDslGradleBuildAssert hasSourceCompatibility(String javaVersion) {
|
|
||||||
return hasProperty("sourceCompatibility", javaVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Assert {@code build.gradle} 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 GroovyDslGradleBuildAssert hasProperty(String name, String value) {
|
|
||||||
return contains(String.format("%s = '%s'", name, value));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Assert {@code build.gradle} contains only the specified properties.
|
|
||||||
* @param values the property value pairs
|
|
||||||
* @return this for method chaining.
|
|
||||||
*/
|
|
||||||
public GroovyDslGradleBuildAssert 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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,37 +16,15 @@
|
|||||||
|
|
||||||
package io.spring.initializr.generator.test.buildsystem.gradle;
|
package io.spring.initializr.generator.test.buildsystem.gradle;
|
||||||
|
|
||||||
import io.spring.initializr.generator.test.io.AbstractTextAssert;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple assertions for a gradle settings using the Groovy DSL.
|
* Simple assertions for a gradle settings using the Groovy DSL.
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
public class GroovyDslGradleSettingsAssert extends AbstractTextAssert<GroovyDslGradleSettingsAssert> {
|
public class GroovyDslGradleSettingsAssert extends GradleSettingsAssert<GroovyDslGradleSettingsAssert> {
|
||||||
|
|
||||||
public GroovyDslGradleSettingsAssert(String content) {
|
public GroovyDslGradleSettingsAssert(String content) {
|
||||||
super(content, GroovyDslGradleSettingsAssert.class);
|
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -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<KotlinDslGradleBuildAssert> {
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -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<KotlinDslGradleSettingsAssert> {
|
||||||
|
|
||||||
|
protected KotlinDslGradleSettingsAssert(String content) {
|
||||||
|
super(content, KotlinDslGradleSettingsAssert.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.language.Language;
|
||||||
import io.spring.initializr.generator.test.buildsystem.gradle.GroovyDslGradleBuildAssert;
|
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;
|
import io.spring.initializr.generator.test.buildsystem.maven.MavenBuildAssert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -108,4 +109,24 @@ public abstract class AbstractModuleAssert<SELF extends AbstractModuleAssert<SEL
|
|||||||
return new GroovyDslGradleBuildAssert(this.actual.resolve("build.gradle"));
|
return new GroovyDslGradleBuildAssert(this.actual.resolve("build.gradle"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert the module defines a {@code build.gradle.kts}.
|
||||||
|
* @return {@code this} assertion object
|
||||||
|
*/
|
||||||
|
public SELF hasKotlinDslGradleBuild() {
|
||||||
|
filePaths().contains("build.gradle.kts");
|
||||||
|
return this.myself;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Assert this module has a {@code build.gradle.kts} and return an
|
||||||
|
* {@link KotlinDslGradleBuildAssert assert} for the {@code build.gradle.kts} file of
|
||||||
|
* this module, to allow chaining of gradle-specific assertions from this call.
|
||||||
|
* @return a {@link KotlinDslGradleBuildAssert}
|
||||||
|
*/
|
||||||
|
public KotlinDslGradleBuildAssert kotlinDslGradleBuild() {
|
||||||
|
hasKotlinDslGradleBuild();
|
||||||
|
return new KotlinDslGradleBuildAssert(this.actual.resolve("build.gradle.kts"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
* 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 KotlinDslGradleBuildAssert}
|
||||||
|
*
|
||||||
|
* @author Prithvi singh
|
||||||
|
*/
|
||||||
|
class KotlinDslGradleBuildAssertTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hasPluginWithId() {
|
||||||
|
assertThat(forSampleGradleBuild()).hasPlugin("java");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hasPluginWithIdAndVersion() {
|
||||||
|
assertThat(forSampleGradleBuild()).hasPlugin("com.example", "1.0.0.RELEASE");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hasPluginWrongId() {
|
||||||
|
assertThatExceptionOfType(AssertionError.class)
|
||||||
|
.isThrownBy(() -> 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<KotlinDslGradleBuildAssert> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -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<KotlinDslGradleSettingsAssert> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -188,6 +188,19 @@ class ModuleAssertTests {
|
|||||||
.isThrownBy(() -> assertThat(forDirectory(dir)).hasGroovyDslGradleBuild());
|
.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
|
@Test
|
||||||
void hasGradleWrapper(@TempDir Path dir) throws IOException {
|
void hasGradleWrapper(@TempDir Path dir) throws IOException {
|
||||||
createFiles(dir, "gradlew", "gradlew.bat", "gradle/wrapper/gradle-wrapper.properties",
|
createFiles(dir, "gradlew", "gradlew.bat", "gradle/wrapper/gradle-wrapper.properties",
|
||||||
@@ -221,6 +234,19 @@ class ModuleAssertTests {
|
|||||||
.isThrownBy(() -> assertThat(forDirectory(dir)).groovyDslGradleBuild());
|
.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<ModuleAssert> forDirectory(Path projectDirectory) {
|
private AssertProvider<ModuleAssert> forDirectory(Path projectDirectory) {
|
||||||
return () -> new ModuleAssert(projectDirectory);
|
return () -> new ModuleAssert(projectDirectory);
|
||||||
}
|
}
|
||||||
|
@@ -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}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@@ -0,0 +1 @@
|
|||||||
|
rootProject.name = 'demo'
|
Reference in New Issue
Block a user