diff --git a/spring-boot-tools/spring-boot-gradle-plugin/build.gradle b/spring-boot-tools/spring-boot-gradle-plugin/build.gradle index 5f3c795b5a..01b42fef76 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/build.gradle +++ b/spring-boot-tools/spring-boot-gradle-plugin/build.gradle @@ -37,6 +37,7 @@ dependencies { compile localGroovy() compile gradleApi() testCompile gradleTestKit() + testCompile 'org.apache.commons:commons-compress:1.13' } jar { diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests.java new file mode 100644 index 0000000000..6a9dd6c1f7 --- /dev/null +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests.java @@ -0,0 +1,156 @@ +/* + * Copyright 2012-2017 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 + * + * http://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 org.springframework.boot.gradle.plugin; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.gradle.testkit.runner.TaskOutcome; +import org.junit.Rule; +import org.junit.Test; + +import org.springframework.boot.gradle.testkit.GradleBuild; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration tests for {@link ApplicationPluginAction}. + * + * @author Andy Wilkinson + */ +public class ApplicationPluginActionIntegrationTests { + + @Rule + public GradleBuild gradleBuild = new GradleBuild(); + + @Test + public void noBootDistributionWithoutApplicationPluginApplied() { + assertThat(this.gradleBuild.build("distributionExists", "-PdistributionName=boot") + .getOutput()).contains("boot exists = false"); + } + + @Test + public void applyingApplicationPluginCreatesBootDistribution() { + assertThat(this.gradleBuild.build("distributionExists", "-PdistributionName=boot", + "-PapplyApplicationPlugin").getOutput()).contains("boot exists = true"); + } + + @Test + public void noBootStartScriptsTaskWithoutApplicationPluginApplied() { + assertThat(this.gradleBuild.build("taskExists", "-PtaskName=bootStartScripts") + .getOutput()).contains("bootStartScripts exists = false"); + } + + @Test + public void applyingApplictionPluginCreatesBootStartScriptsTask() { + assertThat(this.gradleBuild.build("taskExists", "-PtaskName=bootStartScripts", + "-PapplyApplicationPlugin").getOutput()) + .contains("bootStartScripts exists = true"); + } + + @Test + public void zipDistributionForJarCanBeBuilt() throws IOException { + assertThat( + this.gradleBuild.build("bootDistZip").task(":bootDistZip").getOutcome()) + .isEqualTo(TaskOutcome.SUCCESS); + String name = this.gradleBuild.getProjectDir().getName(); + File distribution = new File(this.gradleBuild.getProjectDir(), + "build/distributions/" + name + "-boot.zip"); + assertThat(distribution).isFile(); + assertThat(zipEntryNames(distribution)).containsExactlyInAnyOrder(name + "-boot/", + name + "-boot/lib/", name + "-boot/lib/" + name + ".jar", + name + "-boot/bin/", name + "-boot/bin/" + name, + name + "-boot/bin/" + name + ".bat"); + } + + @Test + public void tarDistributionForJarCanBeBuilt() throws IOException { + assertThat( + this.gradleBuild.build("bootDistTar").task(":bootDistTar").getOutcome()) + .isEqualTo(TaskOutcome.SUCCESS); + String name = this.gradleBuild.getProjectDir().getName(); + File distribution = new File(this.gradleBuild.getProjectDir(), + "build/distributions/" + name + "-boot.tar"); + assertThat(distribution).isFile(); + assertThat(tarEntryNames(distribution)).containsExactlyInAnyOrder(name + "-boot/", + name + "-boot/lib/", name + "-boot/lib/" + name + ".jar", + name + "-boot/bin/", name + "-boot/bin/" + name, + name + "-boot/bin/" + name + ".bat"); + } + + @Test + public void zipDistributionForWarCanBeBuilt() throws IOException { + assertThat( + this.gradleBuild.build("bootDistZip").task(":bootDistZip").getOutcome()) + .isEqualTo(TaskOutcome.SUCCESS); + String name = this.gradleBuild.getProjectDir().getName(); + File distribution = new File(this.gradleBuild.getProjectDir(), + "build/distributions/" + name + "-boot.zip"); + assertThat(distribution).isFile(); + assertThat(zipEntryNames(distribution)).containsExactlyInAnyOrder(name + "-boot/", + name + "-boot/lib/", name + "-boot/lib/" + name + ".war", + name + "-boot/bin/", name + "-boot/bin/" + name, + name + "-boot/bin/" + name + ".bat"); + } + + @Test + public void tarDistributionForWarCanBeBuilt() throws IOException { + assertThat( + this.gradleBuild.build("bootDistTar").task(":bootDistTar").getOutcome()) + .isEqualTo(TaskOutcome.SUCCESS); + String name = this.gradleBuild.getProjectDir().getName(); + File distribution = new File(this.gradleBuild.getProjectDir(), + "build/distributions/" + name + "-boot.tar"); + assertThat(distribution).isFile(); + assertThat(tarEntryNames(distribution)).containsExactlyInAnyOrder(name + "-boot/", + name + "-boot/lib/", name + "-boot/lib/" + name + ".war", + name + "-boot/bin/", name + "-boot/bin/" + name, + name + "-boot/bin/" + name + ".bat"); + } + + private List zipEntryNames(File distribution) throws IOException { + List entryNames = new ArrayList<>(); + try (ZipFile zipFile = new ZipFile(distribution)) { + Enumeration entries = zipFile.entries(); + while (entries.hasMoreElements()) { + entryNames.add(entries.nextElement().getName()); + } + } + return entryNames; + } + + private List tarEntryNames(File distribution) throws IOException { + List entryNames = new ArrayList<>(); + try (TarArchiveInputStream input = new TarArchiveInputStream( + new FileInputStream(distribution))) { + TarArchiveEntry entry; + while ((entry = input.getNextTarEntry()) != null) { + entryNames.add(entry.getName()); + } + } + return entryNames; + } + +} diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests-tarDistributionForJarCanBeBuilt.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests-tarDistributionForJarCanBeBuilt.gradle new file mode 100644 index 0000000000..ed26697d0e --- /dev/null +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests-tarDistributionForJarCanBeBuilt.gradle @@ -0,0 +1,13 @@ +buildscript { + dependencies { + classpath files(pluginClasspath.split(',')) + } +} + +apply plugin: 'org.springframework.boot' +apply plugin: 'application' +apply plugin: 'java' + +bootJar { + mainClass = 'com.example.ExampleApplication' +} diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests-tarDistributionForWarCanBeBuilt.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests-tarDistributionForWarCanBeBuilt.gradle new file mode 100644 index 0000000000..aa4a2a7633 --- /dev/null +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests-tarDistributionForWarCanBeBuilt.gradle @@ -0,0 +1,13 @@ +buildscript { + dependencies { + classpath files(pluginClasspath.split(',')) + } +} + +apply plugin: 'org.springframework.boot' +apply plugin: 'application' +apply plugin: 'war' + +bootWar { + mainClass = 'com.example.ExampleApplication' +} diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests-zipDistributionForJarCanBeBuilt.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests-zipDistributionForJarCanBeBuilt.gradle new file mode 100644 index 0000000000..ed26697d0e --- /dev/null +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests-zipDistributionForJarCanBeBuilt.gradle @@ -0,0 +1,13 @@ +buildscript { + dependencies { + classpath files(pluginClasspath.split(',')) + } +} + +apply plugin: 'org.springframework.boot' +apply plugin: 'application' +apply plugin: 'java' + +bootJar { + mainClass = 'com.example.ExampleApplication' +} diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests-zipDistributionForWarCanBeBuilt.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests-zipDistributionForWarCanBeBuilt.gradle new file mode 100644 index 0000000000..aa4a2a7633 --- /dev/null +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests-zipDistributionForWarCanBeBuilt.gradle @@ -0,0 +1,13 @@ +buildscript { + dependencies { + classpath files(pluginClasspath.split(',')) + } +} + +apply plugin: 'org.springframework.boot' +apply plugin: 'application' +apply plugin: 'war' + +bootWar { + mainClass = 'com.example.ExampleApplication' +} diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests.gradle b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests.gradle new file mode 100644 index 0000000000..18509a038c --- /dev/null +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/resources/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests.gradle @@ -0,0 +1,33 @@ +buildscript { + dependencies { + classpath files(pluginClasspath.split(',')) + } +} + +apply plugin: 'org.springframework.boot' + +if (project.hasProperty('applyApplicationPlugin')) { + apply plugin: 'application' +} + +task('taskExists') { + doFirst { + println "$taskName exists = ${tasks.findByName(taskName) != null}" + } +} + +task('distributionExists') { + doFirst { + boolean found = project.hasProperty('distributions') && + distributions.findByName(distributionName) != null + println "$distributionName exists = $found" + } +} + +task('javaCompileEncoding') { + doFirst { + tasks.withType(JavaCompile) { + println "$name = ${options.encoding}" + } + } +}