Skip to content

Commit

Permalink
Merge pull request #1412 from prithvitewatia
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
snicoll committed Jun 12, 2023
2 parents 6ca2dcc + c99c8c0 commit 7f1208a
Show file tree
Hide file tree
Showing 12 changed files with 467 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -18,15 +18,14 @@

import java.nio.file.Path;

import io.spring.initializr.generator.test.io.AbstractTextAssert;
import io.spring.initializr.generator.test.io.TextTestUtils;

/**
* Simple assertions for a gradle build using the Groovy DSL.
*
* @author Stephane Nicoll
*/
public class GroovyDslGradleBuildAssert extends AbstractTextAssert<GroovyDslGradleBuildAssert> {
public class GroovyDslGradleBuildAssert extends GradleBuildAssert<GroovyDslGradleBuildAssert> {

public GroovyDslGradleBuildAssert(String content) {
super(content, GroovyDslGradleBuildAssert.class);
Expand Down Expand Up @@ -55,51 +54,4 @@ public GroovyDslGradleBuildAssert hasPlugin(String 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());
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,37 +16,15 @@

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.
*
* @author Stephane Nicoll
*/
public class GroovyDslGradleSettingsAssert extends AbstractTextAssert<GroovyDslGradleSettingsAssert> {
public class GroovyDslGradleSettingsAssert extends GradleSettingsAssert<GroovyDslGradleSettingsAssert> {

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));
}

}
Original file line number Diff line number Diff line change
@@ -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));
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,6 +20,7 @@

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;

/**
Expand Down Expand Up @@ -108,4 +109,24 @@ public GroovyDslGradleBuildAssert groovyDslGradleBuild() {
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"));
}

}
Loading

0 comments on commit 7f1208a

Please sign in to comment.