Add GeneratedFiles interface and support classes
Add a `GeneratedFiles` interface that can be used to add generated source, class and resource files. An in-memory implementation is provided for testing and a filesystem implementation is provided to actually save the files to disk. See gh-28414
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright 2002-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.aot.generate;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import org.springframework.aot.generate.GeneratedFiles.Kind;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests for {@link FileSystemGeneratedFiles}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class FileSystemGeneratedFilesTests {
|
||||
|
||||
@TempDir
|
||||
Path root;
|
||||
|
||||
@Test
|
||||
void addFilesCopiesToFileSystem() {
|
||||
FileSystemGeneratedFiles generatedFiles = new FileSystemGeneratedFiles(this.root);
|
||||
generatedFiles.addSourceFile("com.example.Test", "{}");
|
||||
generatedFiles.addResourceFile("META-INF/test", "test");
|
||||
generatedFiles.addClassFile("com/example/TestProxy.class",
|
||||
new ByteArrayResource("!".getBytes(StandardCharsets.UTF_8)));
|
||||
assertThat(this.root.resolve("sources/com/example/Test.java")).content()
|
||||
.isEqualTo("{}");
|
||||
assertThat(this.root.resolve("resources/META-INF/test")).content()
|
||||
.isEqualTo("test");
|
||||
assertThat(this.root.resolve("classes/com/example/TestProxy.class")).content()
|
||||
.isEqualTo("!");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addFilesWithCustomRootsCopiesToFileSystem() {
|
||||
FileSystemGeneratedFiles generatedFiles = new FileSystemGeneratedFiles(
|
||||
kind -> this.root.resolve("the-" + kind));
|
||||
generatedFiles.addSourceFile("com.example.Test", "{}");
|
||||
generatedFiles.addResourceFile("META-INF/test", "test");
|
||||
generatedFiles.addClassFile("com/example/TestProxy.class",
|
||||
new ByteArrayResource("!".getBytes(StandardCharsets.UTF_8)));
|
||||
assertThat(this.root.resolve("the-SOURCE/com/example/Test.java")).content()
|
||||
.isEqualTo("{}");
|
||||
assertThat(this.root.resolve("the-RESOURCE/META-INF/test")).content()
|
||||
.isEqualTo("test");
|
||||
assertThat(this.root.resolve("the-CLASS/com/example/TestProxy.class")).content()
|
||||
.isEqualTo("!");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenRootIsNullThrowsException() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new FileSystemGeneratedFiles((Path) null))
|
||||
.withMessage("'root' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenRootsIsNullThrowsException() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(
|
||||
() -> new FileSystemGeneratedFiles((Function<Kind, Path>) null))
|
||||
.withMessage("'roots' must not be null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createWhenRootsResultsInNullThrowsException() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(
|
||||
() -> new FileSystemGeneratedFiles(kind -> (kind != Kind.CLASS)
|
||||
? this.root.resolve(kind.toString()) : null))
|
||||
.withMessage("'roots' must return a value for all file kinds");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addFileWhenPathIsOutsideOfRootThrowsException() {
|
||||
FileSystemGeneratedFiles generatedFiles = new FileSystemGeneratedFiles(this.root);
|
||||
assertPathMustBeRelative(generatedFiles, "/test");
|
||||
assertPathMustBeRelative(generatedFiles, "../test");
|
||||
assertPathMustBeRelative(generatedFiles, "test/../../test");
|
||||
}
|
||||
|
||||
private void assertPathMustBeRelative(FileSystemGeneratedFiles generatedFiles,
|
||||
String path) {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> generatedFiles.addResourceFile(path, "test"))
|
||||
.withMessage("'path' must be relative");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Copyright 2002-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.aot.generate;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import javax.lang.model.element.Modifier;
|
||||
|
||||
import org.assertj.core.api.AbstractStringAssert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.aot.generate.GeneratedFiles.Kind;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.InputStreamSource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.javapoet.JavaFile;
|
||||
import org.springframework.javapoet.MethodSpec;
|
||||
import org.springframework.javapoet.TypeSpec;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests for {@link GeneratedFiles}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class GeneratedFilesTests {
|
||||
|
||||
private final TestGeneratedFiles generatedFiles = new TestGeneratedFiles();
|
||||
|
||||
@Test
|
||||
void addSourceFileWithJavaFileAddsFile() throws Exception {
|
||||
MethodSpec main = MethodSpec.methodBuilder("main")
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.STATIC).returns(void.class)
|
||||
.addParameter(String[].class, "args")
|
||||
.addStatement("$T.out.println($S)", System.class, "Hello, World!")
|
||||
.build();
|
||||
TypeSpec helloWorld = TypeSpec.classBuilder("HelloWorld")
|
||||
.addModifiers(Modifier.PUBLIC, Modifier.FINAL).addMethod(main).build();
|
||||
JavaFile javaFile = JavaFile.builder("com.example", helloWorld).build();
|
||||
this.generatedFiles.addSourceFile(javaFile);
|
||||
assertThatFileAdded(Kind.SOURCE, "com/example/HelloWorld.java")
|
||||
.contains("Hello, World!");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addSourceFileWithCharSequenceAddsFile() throws Exception {
|
||||
this.generatedFiles.addSourceFile("com.example.HelloWorld", "{}");
|
||||
assertThatFileAdded(Kind.SOURCE, "com/example/HelloWorld.java").isEqualTo("{}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addSourceFileWithCharSequenceWhenClassNameIsEmptyThrowsException() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> this.generatedFiles.addSourceFile("", "{}"))
|
||||
.withMessage("'className' must not be empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addSourceFileWithCharSequenceWhenClassNameIsInvalidThrowsException() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> this.generatedFiles
|
||||
.addSourceFile("com/example/HelloWorld.java", "{}"))
|
||||
.withMessage("'className' must be a valid identifier");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addSourceFileWithConsumedAppendableAddsFile() throws Exception {
|
||||
this.generatedFiles.addSourceFile("com.example.HelloWorld",
|
||||
appendable -> appendable.append("{}"));
|
||||
assertThatFileAdded(Kind.SOURCE, "com/example/HelloWorld.java").isEqualTo("{}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addSourceFileWithInputStreamSourceAddsFile() throws Exception {
|
||||
Resource resource = new ByteArrayResource("{}".getBytes(StandardCharsets.UTF_8));
|
||||
this.generatedFiles.addSourceFile("com.example.HelloWorld", resource);
|
||||
assertThatFileAdded(Kind.SOURCE, "com/example/HelloWorld.java").isEqualTo("{}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addResourceFileWithCharSequenceAddsFile() throws Exception {
|
||||
this.generatedFiles.addResourceFile("META-INF/file", "test");
|
||||
assertThatFileAdded(Kind.RESOURCE, "META-INF/file").isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addResourceFileWithConsumedAppendableAddsFile() throws Exception {
|
||||
this.generatedFiles.addResourceFile("META-INF/file",
|
||||
appendable -> appendable.append("test"));
|
||||
assertThatFileAdded(Kind.RESOURCE, "META-INF/file").isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addResourceFileWithInputStreamSourceAddsFile() throws IOException {
|
||||
Resource resource = new ByteArrayResource(
|
||||
"test".getBytes(StandardCharsets.UTF_8));
|
||||
this.generatedFiles.addResourceFile("META-INF/file", resource);
|
||||
assertThatFileAdded(Kind.RESOURCE, "META-INF/file").isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addClassFileWithInputStreamSourceAddsFile() throws IOException {
|
||||
Resource resource = new ByteArrayResource(
|
||||
"test".getBytes(StandardCharsets.UTF_8));
|
||||
this.generatedFiles.addClassFile("com/example/HelloWorld.class", resource);
|
||||
assertThatFileAdded(Kind.CLASS, "com/example/HelloWorld.class").isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addFileWithCharSequenceAddsFile() throws Exception {
|
||||
this.generatedFiles.addFile(Kind.RESOURCE, "META-INF/file", "test");
|
||||
assertThatFileAdded(Kind.RESOURCE, "META-INF/file").isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addFileWithConsumedAppendableAddsFile() throws IOException {
|
||||
this.generatedFiles.addFile(Kind.SOURCE, "com/example/HelloWorld.java",
|
||||
appendable -> appendable.append("{}"));
|
||||
assertThatFileAdded(Kind.SOURCE, "com/example/HelloWorld.java").isEqualTo("{}");
|
||||
}
|
||||
|
||||
private AbstractStringAssert<?> assertThatFileAdded(Kind kind, String path)
|
||||
throws IOException {
|
||||
return this.generatedFiles.assertThatFileAdded(kind, path);
|
||||
}
|
||||
|
||||
static class TestGeneratedFiles implements GeneratedFiles {
|
||||
|
||||
private Kind kind;
|
||||
|
||||
private String path;
|
||||
|
||||
private InputStreamSource content;
|
||||
|
||||
@Override
|
||||
public void addFile(Kind kind, String path, InputStreamSource content) {
|
||||
this.kind = kind;
|
||||
this.path = path;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
AbstractStringAssert<?> assertThatFileAdded(Kind kind, String path)
|
||||
throws IOException {
|
||||
assertThat(this.kind).as("kind").isEqualTo(kind);
|
||||
assertThat(this.path).as("path").isEqualTo(path);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
this.content.getInputStream().transferTo(out);
|
||||
return assertThat(out.toString(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2002-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.aot.generate;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.aot.generate.GeneratedFiles.Kind;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
/**
|
||||
* Tests for {@link InMemoryGeneratedFiles}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
class InMemoryGeneratedFilesTests {
|
||||
|
||||
private final InMemoryGeneratedFiles generatedFiles = new InMemoryGeneratedFiles();
|
||||
|
||||
@Test
|
||||
void addFileAddsInMemoryFile() throws Exception {
|
||||
this.generatedFiles.addResourceFile("META-INF/test", "test");
|
||||
assertThat(this.generatedFiles.getGeneratedFileContent(Kind.RESOURCE,
|
||||
"META-INF/test")).isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addFileWhenFileAlreadyAddedThrowsException() {
|
||||
this.generatedFiles.addResourceFile("META-INF/test", "test");
|
||||
assertThatIllegalStateException().isThrownBy(
|
||||
() -> this.generatedFiles.addResourceFile("META-INF/test", "test"))
|
||||
.withMessage("Path 'META-INF/test' already in use");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getGeneratedFilesReturnsFiles() throws Exception {
|
||||
this.generatedFiles.addResourceFile("META-INF/test1", "test1");
|
||||
this.generatedFiles.addResourceFile("META-INF/test2", "test2");
|
||||
assertThat(this.generatedFiles.getGeneratedFiles(Kind.RESOURCE))
|
||||
.containsKeys("META-INF/test1", "META-INF/test2");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getGeneratedFileContentWhenFileExistsReturnsContent() throws Exception {
|
||||
this.generatedFiles.addResourceFile("META-INF/test", "test");
|
||||
assertThat(this.generatedFiles.getGeneratedFileContent(Kind.RESOURCE,
|
||||
"META-INF/test")).isEqualTo("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getGeneratedFileContentWhenFileIsMissingReturnsNull() throws Exception {
|
||||
this.generatedFiles.addResourceFile("META-INF/test", "test");
|
||||
assertThat(this.generatedFiles.getGeneratedFileContent(Kind.RESOURCE,
|
||||
"META-INF/missing")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getGeneratedFileWhenFileExistsReturnsInputStreamSource() {
|
||||
this.generatedFiles.addResourceFile("META-INF/test", "test");
|
||||
assertThat(this.generatedFiles.getGeneratedFile(Kind.RESOURCE, "META-INF/test"))
|
||||
.isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getGeneratedFileWhenFileIsMissingReturnsNull() {
|
||||
this.generatedFiles.addResourceFile("META-INF/test", "test");
|
||||
assertThat(
|
||||
this.generatedFiles.getGeneratedFile(Kind.RESOURCE, "META-INF/missing"))
|
||||
.isNull();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user