This commit is contained in:
Stephane Nicoll 2022-09-14 14:35:17 +02:00
parent 354117da7f
commit e9997d4004
14 changed files with 28 additions and 37 deletions

View File

@ -21,6 +21,8 @@ import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import org.springframework.lang.Nullable;
/**
* {@link ClassLoader} implementation to support
* {@link CompileWithTargetClassAccess @CompileWithTargetClassAccess}.
@ -74,6 +76,7 @@ final class CompileWithTargetClassAccessClassLoader extends ClassLoader {
}
@Override
@Nullable
protected URL findResource(String name) {
return this.testClassLoader.getResource(name);
}

View File

@ -158,7 +158,7 @@ public class DynamicClassLoader extends ClassLoader {
@Override
protected URLConnection openConnection(URL url) throws IOException {
protected URLConnection openConnection(URL url) {
return new ResourceFileConnection(url, this.file);
}
@ -177,11 +177,11 @@ public class DynamicClassLoader extends ClassLoader {
@Override
public void connect() throws IOException {
public void connect() {
}
@Override
public InputStream getInputStream() throws IOException {
public InputStream getInputStream() {
return new ByteArrayInputStream(
this.file.getContent().getBytes(StandardCharsets.UTF_8));
}

View File

@ -38,8 +38,8 @@ public abstract sealed class DynamicFile permits SourceFile, ResourceFile {
protected DynamicFile(String path, String content) {
Assert.hasText(path, "Path must not be empty");
Assert.hasText(content, "Content must not be empty");
Assert.hasText(path, "'path' must not be empty");
Assert.hasText(content, "'content' must not be empty");
this.path = path;
this.content = content;
}

View File

@ -38,8 +38,7 @@ import org.springframework.lang.Nullable;
final class DynamicFiles<F extends DynamicFile> implements Iterable<F> {
private static final DynamicFiles<?> NONE = new DynamicFiles<>(
Collections.emptyMap());
private static final DynamicFiles<?> NONE = new DynamicFiles<>(Collections.emptyMap());
private final Map<String, F> files;

View File

@ -22,7 +22,6 @@ import java.nio.charset.StandardCharsets;
import org.assertj.core.api.AssertProvider;
import org.springframework.core.io.InputStreamSource;
import org.springframework.lang.Nullable;
import org.springframework.util.FileCopyUtils;
/**
@ -32,8 +31,7 @@ import org.springframework.util.FileCopyUtils;
* @author Phillip Webb
* @since 6.0
*/
public final class ResourceFile extends DynamicFile
implements AssertProvider<ResourceFileAssert> {
public final class ResourceFile extends DynamicFile implements AssertProvider<ResourceFileAssert> {
private ResourceFile(String path, String content) {
@ -46,7 +44,7 @@ public final class ResourceFile extends DynamicFile
* {@link CharSequence}.
* @param path the relative path of the file or {@code null} to have the
* path deduced
* @param charSequence a file containing the source contents
* @param charSequence a file containing the file contents
* @return a {@link ResourceFile} instance
*/
public static ResourceFile of(String path, CharSequence charSequence) {
@ -56,13 +54,11 @@ public final class ResourceFile extends DynamicFile
/**
* Factory method to create a new {@link ResourceFile} from the given
* {@link InputStreamSource}.
* @param path the relative path of the file or {@code null} to have the
* path deduced
* @param path the relative path of the file
* @param inputStreamSource the source for the file
* @return a {@link SourceFile} instance
* @return a {@link ResourceFile} instance
*/
public static ResourceFile of(@Nullable String path,
InputStreamSource inputStreamSource) {
public static ResourceFile of(String path, InputStreamSource inputStreamSource) {
return of(path, appendable -> appendable.append(FileCopyUtils.copyToString(
new InputStreamReader(inputStreamSource.getInputStream(), StandardCharsets.UTF_8))));
}
@ -70,10 +66,9 @@ public final class ResourceFile extends DynamicFile
/**
* Factory method to create a new {@link SourceFile} from the given
* {@link WritableContent}.
* @param path the relative path of the file or {@code null} to have the
* path deduced
* @param path the relative path of the file
* @param writableContent the content to write to the file
* @return a {@link SourceFile} instance
* @return a {@link ResourceFile} instance
*/
public static ResourceFile of(String path, WritableContent writableContent) {
return new ResourceFile(path, toString(writableContent));

View File

@ -111,7 +111,7 @@ public final class ResourceFiles implements Iterable<ResourceFile> {
/**
* Get the {@link ResourceFile} with the given
* {@code DynamicFile#getPath() path}.
* {@linkplain DynamicFile#getPath() path}.
* @param path the path to find
* @return a {@link ResourceFile} instance or {@code null}
*/
@ -121,7 +121,7 @@ public final class ResourceFiles implements Iterable<ResourceFile> {
}
/**
* Return the single source file contained in the collection.
* Return the single {@link ResourceFile} contained in the collection.
* @return the single file
* @throws IllegalStateException if the collection doesn't contain exactly
* one file

View File

@ -35,9 +35,8 @@ import org.springframework.util.StringUtils;
/**
* {@link DynamicFile} that holds Java source code and provides
* {@link SourceFileAssert} support. Usually created from an AOT generated type,
* for example:
*
* {@link SourceFileAssert} support. Usually created from an AOT generated
* type, for example:
* <pre class="code">
* SourceFile.of(generatedFile::writeTo)
* </pre>
@ -45,8 +44,7 @@ import org.springframework.util.StringUtils;
* @author Phillip Webb
* @since 6.0
*/
public final class SourceFile extends DynamicFile
implements AssertProvider<SourceFileAssert> {
public final class SourceFile extends DynamicFile implements AssertProvider<SourceFileAssert> {
private final String className;

View File

@ -114,7 +114,7 @@ public final class SourceFiles implements Iterable<SourceFile> {
/**
* Get the {@link SourceFile} with the given
* {@code DynamicFile#getPath() path}.
* {@linkplain DynamicFile#getPath() path}.
* @param path the path to find
* @return a {@link SourceFile} instance or {@code null}
*/

View File

@ -1,7 +1,5 @@
/**
* Support classes for running assertions on generated files.
*
* @author Stephane Nicoll
*/
@NonNullApi
@NonNullFields

View File

@ -107,15 +107,13 @@ class CompiledTests {
ResourceFile.of("META-INF/myfile1", "test1"),
ResourceFile.of("META-INF/myfile2", "test2"));
TestCompiler.forSystem().withResources(resourceFiles).compile(
compiled -> assertThatIllegalStateException().isThrownBy(
() -> compiled.getResourceFile()));
compiled -> assertThatIllegalStateException().isThrownBy(compiled::getResourceFile));
}
@Test
void getResourceFileWhenNoneThrowsException() {
TestCompiler.forSystem().compile(
compiled -> assertThatIllegalStateException().isThrownBy(
() -> compiled.getResourceFile()));
compiled -> assertThatIllegalStateException().isThrownBy(compiled::getResourceFile));
}
@Test

View File

@ -39,7 +39,7 @@ class DynamicJavaFileObjectTests {
}
@Test
void getCharContentReturnsContent() throws Exception {
void getCharContentReturnsContent() {
DynamicJavaFileObject fileObject = new DynamicJavaFileObject(SourceFile.of(CONTENT));
assertThat(fileObject.getCharContent(true)).isEqualTo(CONTENT);
}

View File

@ -206,7 +206,7 @@ class TestCompilerTests {
@Test
@CompileWithTargetClassAccess
void compiledCodeCanAccessExistingPackagePrivateClassIfAnnotated() throws ClassNotFoundException, LinkageError {
void compiledCodeCanAccessExistingPackagePrivateClassIfAnnotated() throws LinkageError {
SourceFiles sourceFiles = SourceFiles.of(SourceFile.of("""
package com.example;

View File

@ -110,7 +110,7 @@ class ReResourceFilesTests {
@Test
void getSingleWhenHasMultipleFilesThrowsException() {
ResourceFiles resourceFiles = ResourceFiles.of(RESOURCE_FILE_1, RESOURCE_FILE_2);
assertThatIllegalStateException().isThrownBy(() -> resourceFiles.getSingle());
assertThatIllegalStateException().isThrownBy(resourceFiles::getSingle);
}
@Test

View File

@ -114,7 +114,7 @@ class SourceFilesTests {
@Test
void getSingleWhenHasMultipleFilesThrowsException() {
SourceFiles sourceFiles = SourceFiles.of(SOURCE_FILE_1, SOURCE_FILE_2);
assertThatIllegalStateException().isThrownBy(() -> sourceFiles.getSingle());
assertThatIllegalStateException().isThrownBy(sourceFiles::getSingle);
}
@Test