Rework BootRunTask to be more idiomatic and make fewer assumptions

This commit is contained in:
Andy Wilkinson
2017-03-27 15:06:50 +01:00
parent 6e7e42459b
commit 2b44ad9809
8 changed files with 197 additions and 90 deletions

View File

@@ -0,0 +1,42 @@
/*
* 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 com.example;
import java.net.URL;
import java.net.URLClassLoader;
/**
* Very basic application used for testing {@code BootRun}.
*
* @author Andy Wilkinson
*/
public class BootRunApplication {
protected BootRunApplication() {
}
public static void main(String[] args) {
int i = 1;
for (URL url : ((URLClassLoader) BootRunApplication.class.getClassLoader())
.getURLs()) {
System.out.println(i++ + ". " + url);
}
}
}

View File

@@ -0,0 +1,74 @@
/*
* 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.run;
import java.io.File;
import java.io.IOException;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.util.FileSystemUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link BootRun}.
*
* @author Andy Wilkinson
*/
public class BootRunIntegrationTests {
@Rule
public final GradleBuild gradleBuild = new GradleBuild();
@Test
public void basicExecution() throws IOException {
File output = new File(this.gradleBuild.getProjectDir(),
"src/main/java/com/example");
output.mkdirs();
FileSystemUtils.copyRecursively(new File("src/test/java/com/example"), output);
new File(this.gradleBuild.getProjectDir(), "src/main/resources").mkdirs();
BuildResult result = this.gradleBuild.build("bootRun");
assertThat(result.task(":bootRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("1. " + urlOf("build/classes/main"));
assertThat(result.getOutput()).contains("2. " + urlOf("build/resources/main"));
assertThat(result.getOutput()).doesNotContain(urlOf("src/main/resources"));
}
@Test
public void sourceResourcesCanBeUsed() throws IOException {
File output = new File(this.gradleBuild.getProjectDir(),
"src/main/java/com/example");
output.mkdirs();
FileSystemUtils.copyRecursively(new File("src/test/java/com/example"), output);
BuildResult result = this.gradleBuild.build("bootRun");
assertThat(result.task(":bootRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("1. " + urlOf("src/main/resources"));
assertThat(result.getOutput()).contains("2. " + urlOf("build/classes/main"));
assertThat(result.getOutput()).doesNotContain(urlOf("build/resources/main"));
}
private String urlOf(String path) throws IOException {
return new File(this.gradleBuild.getProjectDir().getCanonicalFile(), path).toURI()
.toURL().toString();
}
}

View File

@@ -39,6 +39,7 @@ import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.xml.sax.InputSource;
import org.springframework.asm.ClassVisitor;
import org.springframework.boot.loader.tools.LaunchScript;
/**
@@ -106,6 +107,7 @@ public class GradleBuild implements TestRule {
return absolutePath("bin") + "," + absolutePath("build/classes/main") + ","
+ absolutePath("build/resources/main") + ","
+ pathOfJarContaining(LaunchScript.class) + ","
+ pathOfJarContaining(ClassVisitor.class) + ","
+ pathOfJarContaining(DependencyManagementPlugin.class);
}