Replace our TestCompiler with Spring Framework's version
Replace the last use of our `TestCompiler` with Spring Framework's version. See gh-31266
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package org.springframework.boot.loader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.ArrayList;
|
||||
@@ -33,8 +34,11 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.loader.archive.Archive;
|
||||
import org.springframework.boot.loader.archive.ExplodedArchive;
|
||||
import org.springframework.boot.loader.archive.JarFileArchive;
|
||||
import org.springframework.boot.testsupport.compiler.TestCompiler;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.test.tools.SourceFile;
|
||||
import org.springframework.core.test.tools.TestCompiler;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.function.ThrowingConsumer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -101,24 +105,26 @@ class JarLauncherTests extends AbstractExecutableArchiveLauncherTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("removal")
|
||||
void explodedJarDefinedPackagesIncludeManifestAttributes() throws Exception {
|
||||
void explodedJarDefinedPackagesIncludeManifestAttributes() {
|
||||
Manifest manifest = new Manifest();
|
||||
Attributes attributes = manifest.getMainAttributes();
|
||||
attributes.put(Name.MANIFEST_VERSION, "1.0");
|
||||
attributes.put(Name.IMPLEMENTATION_TITLE, "test");
|
||||
File explodedRoot = explode(
|
||||
createJarArchive("archive.jar", manifest, "BOOT-INF", true, Collections.emptyList()));
|
||||
TestCompiler compiler = new TestCompiler(new File(explodedRoot, "BOOT-INF/classes"));
|
||||
File source = new File(this.tempDir, "explodedsample/ExampleClass.java");
|
||||
source.getParentFile().mkdirs();
|
||||
FileCopyUtils.copy(new File("src/test/resources/explodedsample/ExampleClass.txt"), source);
|
||||
compiler.getTask(Collections.singleton(source)).call();
|
||||
JarLauncher launcher = new JarLauncher(new ExplodedArchive(explodedRoot, true));
|
||||
Iterator<Archive> archives = launcher.getClassPathArchivesIterator();
|
||||
URLClassLoader classLoader = (URLClassLoader) launcher.createClassLoader(archives);
|
||||
Class<?> loaded = classLoader.loadClass("explodedsample.ExampleClass");
|
||||
assertThat(loaded.getPackage().getImplementationTitle()).isEqualTo("test");
|
||||
SourceFile sourceFile = SourceFile.of("explodedsample/ExampleClass.java",
|
||||
new ClassPathResource("explodedsample/ExampleClass.txt"));
|
||||
TestCompiler.forSystem().compile(sourceFile, ThrowingConsumer.of((compiled) -> {
|
||||
File explodedRoot = explode(
|
||||
createJarArchive("archive.jar", manifest, "BOOT-INF", true, Collections.emptyList()));
|
||||
File target = new File(explodedRoot, "BOOT-INF/classes/explodedsample/ExampleClass.class");
|
||||
target.getParentFile().mkdirs();
|
||||
FileCopyUtils.copy(compiled.getClassLoader().getResourceAsStream("explodedsample/ExampleClass.class"),
|
||||
new FileOutputStream(target));
|
||||
JarLauncher launcher = new JarLauncher(new ExplodedArchive(explodedRoot, true));
|
||||
Iterator<Archive> archives = launcher.getClassPathArchivesIterator();
|
||||
URLClassLoader classLoader = (URLClassLoader) launcher.createClassLoader(archives);
|
||||
Class<?> loaded = classLoader.loadClass("explodedsample.ExampleClass");
|
||||
assertThat(loaded.getPackage().getImplementationTitle()).isEqualTo("test");
|
||||
}));
|
||||
}
|
||||
|
||||
protected final URL[] getExpectedFileUrls(File explodedRoot) {
|
||||
|
||||
@@ -1,130 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2022 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 org.springframework.boot.testsupport.compiler;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.annotation.processing.Processor;
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.JavaCompiler.CompilationTask;
|
||||
import javax.tools.JavaFileObject;
|
||||
import javax.tools.StandardJavaFileManager;
|
||||
import javax.tools.StandardLocation;
|
||||
import javax.tools.ToolProvider;
|
||||
|
||||
/**
|
||||
* Wrapper to make the {@link JavaCompiler} easier to use in tests.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @since 1.5.0
|
||||
* @deprecated since 3.0.0 in favor of
|
||||
* {@link org.springframework.core.test.tools.TestCompiler}
|
||||
*/
|
||||
@Deprecated(since = "3.0.0", forRemoval = true)
|
||||
public class TestCompiler {
|
||||
|
||||
/**
|
||||
* The default source directory.
|
||||
*/
|
||||
public static final File SOURCE_DIRECTORY = new File("src/test/java");
|
||||
|
||||
private final JavaCompiler compiler;
|
||||
|
||||
private final StandardJavaFileManager fileManager;
|
||||
|
||||
private final File outputLocation;
|
||||
|
||||
public TestCompiler(File outputLocation) throws IOException {
|
||||
this(ToolProvider.getSystemJavaCompiler(), outputLocation);
|
||||
}
|
||||
|
||||
public TestCompiler(JavaCompiler compiler, File outputLocation) throws IOException {
|
||||
this.compiler = compiler;
|
||||
this.fileManager = compiler.getStandardFileManager(null, null, null);
|
||||
this.outputLocation = outputLocation;
|
||||
this.outputLocation.mkdirs();
|
||||
Iterable<? extends File> temp = Collections.singletonList(this.outputLocation);
|
||||
this.fileManager.setLocation(StandardLocation.CLASS_OUTPUT, temp);
|
||||
this.fileManager.setLocation(StandardLocation.SOURCE_OUTPUT, temp);
|
||||
}
|
||||
|
||||
public TestCompilationTask getTask(Collection<File> sourceFiles) {
|
||||
Iterable<? extends JavaFileObject> javaFileObjects = this.fileManager.getJavaFileObjectsFromFiles(sourceFiles);
|
||||
return getTask(javaFileObjects);
|
||||
}
|
||||
|
||||
public TestCompilationTask getTask(Class<?>... types) {
|
||||
Iterable<? extends JavaFileObject> javaFileObjects = getJavaFileObjects(types);
|
||||
return getTask(javaFileObjects);
|
||||
}
|
||||
|
||||
private TestCompilationTask getTask(Iterable<? extends JavaFileObject> javaFileObjects) {
|
||||
return new TestCompilationTask(
|
||||
this.compiler.getTask(null, this.fileManager, null, null, null, javaFileObjects));
|
||||
}
|
||||
|
||||
public File getOutputLocation() {
|
||||
return this.outputLocation;
|
||||
}
|
||||
|
||||
private Iterable<? extends JavaFileObject> getJavaFileObjects(Class<?>... types) {
|
||||
File[] files = new File[types.length];
|
||||
for (int i = 0; i < types.length; i++) {
|
||||
files[i] = getFile(types[i]);
|
||||
}
|
||||
return this.fileManager.getJavaFileObjects(files);
|
||||
}
|
||||
|
||||
protected File getFile(Class<?> type) {
|
||||
return new File(getSourceDirectory(), sourcePathFor(type));
|
||||
}
|
||||
|
||||
public static String sourcePathFor(Class<?> type) {
|
||||
return type.getName().replace('.', '/') + ".java";
|
||||
}
|
||||
|
||||
protected File getSourceDirectory() {
|
||||
return SOURCE_DIRECTORY;
|
||||
}
|
||||
|
||||
/**
|
||||
* A compilation task.
|
||||
*/
|
||||
public static class TestCompilationTask {
|
||||
|
||||
private final CompilationTask task;
|
||||
|
||||
public TestCompilationTask(CompilationTask task) {
|
||||
this.task = task;
|
||||
}
|
||||
|
||||
public void call(Processor... processors) {
|
||||
this.task.setProcessors(Arrays.asList(processors));
|
||||
if (!this.task.call()) {
|
||||
throw new IllegalStateException("Compilation failed");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Utilities to work with the Java compiler at test time.
|
||||
*/
|
||||
package org.springframework.boot.testsupport.compiler;
|
||||
Reference in New Issue
Block a user