mirror of
https://gitee.com/dcren/initializr.git
synced 2026-02-25 21:22:58 +08:00
Polish "Add build assertion support for Gradle with the Kotlin DSL"
See 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,34 +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));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 Prithvi singh
|
* @author Prithvi singh
|
||||||
*/
|
*/
|
||||||
public class KotlinDslGradleBuildAssert extends AbstractTextAssert<KotlinDslGradleBuildAssert> {
|
public class KotlinDslGradleBuildAssert extends GradleBuildAssert<KotlinDslGradleBuildAssert> {
|
||||||
|
|
||||||
public KotlinDslGradleBuildAssert(String content) {
|
public KotlinDslGradleBuildAssert(String content) {
|
||||||
super(content, KotlinDslGradleBuildAssert.class);
|
super(content, KotlinDslGradleBuildAssert.class);
|
||||||
@@ -55,34 +54,4 @@ public class KotlinDslGradleBuildAssert extends AbstractTextAssert<KotlinDslGrad
|
|||||||
return contains(String.format("id('%s')", id));
|
return contains(String.format("id('%s')", id));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Assert {@code build.gradle.kts} uses the specified {@code version}.
|
|
||||||
* @param version the version of the build
|
|
||||||
* @return {@code this} assertion object
|
|
||||||
*/
|
|
||||||
public KotlinDslGradleBuildAssert hasVersion(String version) {
|
|
||||||
return hasProperty("version", version);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Assert {@code build.gradle.kts} uses a source compatibility for the specified java
|
|
||||||
* version.
|
|
||||||
* @param javaVersion the java version
|
|
||||||
* @return {@code this} assertion object
|
|
||||||
*/
|
|
||||||
public KotlinDslGradleBuildAssert hasSourceCompatibility(String javaVersion) {
|
|
||||||
return hasProperty("sourceCompatibility", javaVersion);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Assert {@code build.gradle.kts} 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 KotlinDslGradleBuildAssert 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.
|
||||||
@@ -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 Kotlin DSL.
|
* Simple assertions for a gradle settings using the Kotlin DSL.
|
||||||
*
|
*
|
||||||
* @author Prithvi singh
|
* @author Prithvi singh
|
||||||
*/
|
*/
|
||||||
public class KotlinDslGradleSettingsAssert extends AbstractTextAssert<KotlinDslGradleSettingsAssert> {
|
public class KotlinDslGradleSettingsAssert extends GradleSettingsAssert<KotlinDslGradleSettingsAssert> {
|
||||||
|
|
||||||
protected KotlinDslGradleSettingsAssert(String content) {
|
protected KotlinDslGradleSettingsAssert(String content) {
|
||||||
super(content, KotlinDslGradleSettingsAssert.class);
|
super(content, KotlinDslGradleSettingsAssert.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Assert {@code settings.gradle.kts} defines the specified project name.
|
|
||||||
* @param name the name of the project
|
|
||||||
* @return {@code this} assertion object
|
|
||||||
*/
|
|
||||||
public KotlinDslGradleSettingsAssert hasProjectName(String name) {
|
|
||||||
return hasProperty("rootProject.name", name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Assert {@code settings.gradle.kts} 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 KotlinDslGradleSettingsAssert hasProperty(String name, String value) {
|
|
||||||
return contains(String.format("%s = '%s", name, value));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ import org.springframework.util.StreamUtils;
|
|||||||
*
|
*
|
||||||
* @param <SELF> the type of the concrete assert implementations
|
* @param <SELF> the type of the concrete assert implementations
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Prithvi singh
|
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractTextAssert<SELF extends AbstractStringAssert<SELF>> extends AbstractStringAssert<SELF> {
|
public abstract class AbstractTextAssert<SELF extends AbstractStringAssert<SELF>> extends AbstractStringAssert<SELF> {
|
||||||
|
|
||||||
@@ -85,22 +84,4 @@ public abstract class AbstractTextAssert<SELF extends AbstractStringAssert<SELF>
|
|||||||
return new ListAssert<>(TextTestUtils.readAllLines(this.actual));
|
return new ListAssert<>(TextTestUtils.readAllLines(this.actual));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Assert {@code build.gradle} or {@code build.gradle.kts} contains only the specified
|
|
||||||
* properties.
|
|
||||||
* @param values the property value pairs
|
|
||||||
* @return this for method chaining.
|
|
||||||
*/
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.
|
||||||
|
|||||||
Reference in New Issue
Block a user