Minimize and centralize assumptions about build output

Closes gh-15471
This commit is contained in:
Andy Wilkinson
2018-12-14 17:50:20 +00:00
parent 7d0a7a65c8
commit 61d04db0d7
57 changed files with 778 additions and 337 deletions

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2012-2018 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.testsupport;
import java.io.File;
import java.net.URISyntaxException;
/**
* Provides access to build output locations in a build system and IDE agnostic manner.
*
* @author Andy Wilkinson
*/
public class BuildOutput {
private final Class<?> testClass;
public BuildOutput(Class<?> testClass) {
this.testClass = testClass;
}
/**
* Returns the location into which test classes have been built.
* @return test classes location
*/
public File getTestClassesLocation() {
try {
File location = new File(this.testClass.getProtectionDomain().getCodeSource()
.getLocation().toURI());
if (location.getPath().endsWith(path("target", "test-classes"))) {
return location;
}
throw new IllegalStateException(
"Unexpected test classes location '" + location + "'");
}
catch (URISyntaxException ex) {
throw new IllegalStateException("Invalid test class code source location",
ex);
}
}
/**
* Returns the location into which test resources have been built.
* @return test resources location
*/
public File getTestResourcesLocation() {
File testClassesLocation = getTestClassesLocation();
if (testClassesLocation.getPath().endsWith(path("target", "test-classes"))) {
return testClassesLocation;
}
throw new IllegalStateException(
"Cannot determine test resources location from classes location '"
+ testClassesLocation + "'");
}
/**
* Returns the root location into which build output is written.
* @return root location
*/
public File getRootLocation() {
return getTestClassesLocation().getParentFile();
}
private String path(String... components) {
return File.separator + String.join(File.separator, components);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@@ -16,7 +16,6 @@
package org.springframework.boot.testsupport.context;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
@@ -25,6 +24,7 @@ import org.junit.Test;
import org.springframework.asm.Opcodes;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.boot.testsupport.BuildOutput;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
@@ -44,6 +44,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public abstract class AbstractConfigurationClassTests {
private final BuildOutput buildOutput = new BuildOutput(getClass());
private ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
@Test
@@ -83,7 +85,7 @@ public abstract class AbstractConfigurationClassTests {
private boolean isTestClass(Resource resource) throws IOException {
return resource.getFile().getAbsolutePath()
.contains("target" + File.separator + "test-classes");
.startsWith(this.buildOutput.getTestClassesLocation().getAbsolutePath());
}
private boolean isPublic(MethodMetadata methodMetadata) {