Skip to content

Commit 0ea7af7

Browse files
committed
Polish
See gh-31331
1 parent 8ef74df commit 0ea7af7

File tree

2 files changed

+111
-19
lines changed

2 files changed

+111
-19
lines changed

spring-core/src/main/java/org/springframework/aot/generate/GeneratedFiles.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,14 @@ public void addFile(Kind kind, String path, InputStreamSource content) {
170170
}
171171

172172
/**
173-
* Add a generated file of the specified {@link Kind} with the given
174-
* {@linkplain FileHandler handler}.
175-
* @param kind the kind of file being written
173+
* Handle a generated file of the specified {@link Kind} with the given
174+
* {@linkplain FileHandler handler}. The file handler lets you consume
175+
* the content of the already generated file, if any and provide a way
176+
* to override its content if necessary.
177+
* @param kind the kind of file
176178
* @param path the relative path of the file
177179
* @param handler a consumer of a {@link FileHandler} for the file
180+
* @since 6.2
178181
*/
179182
public abstract void handleFile(Kind kind, String path, ThrowingConsumer<FileHandler> handler);
180183

@@ -235,6 +238,8 @@ public enum Kind {
235238
/**
236239
* Provide access to a particular file and offer convenient method to save
237240
* or override its content.
241+
*
242+
* @since 6.2
238243
*/
239244
public abstract static class FileHandler {
240245

spring-core/src/test/java/org/springframework/aot/generate/GeneratedFilesTests.java

+103-16
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import java.io.ByteArrayOutputStream;
2020
import java.io.IOException;
2121
import java.nio.charset.StandardCharsets;
22+
import java.util.concurrent.atomic.AtomicBoolean;
2223

2324
import javax.lang.model.element.Modifier;
2425

2526
import org.assertj.core.api.AbstractStringAssert;
2627
import org.junit.jupiter.api.Test;
2728

29+
import org.springframework.aot.generate.GeneratedFiles.FileHandler;
2830
import org.springframework.aot.generate.GeneratedFiles.Kind;
2931
import org.springframework.core.io.ByteArrayResource;
3032
import org.springframework.core.io.InputStreamSource;
@@ -37,6 +39,7 @@
3739

3840
import static org.assertj.core.api.Assertions.assertThat;
3941
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
42+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
4043

4144
/**
4245
* Tests for {@link GeneratedFiles}.
@@ -157,18 +160,80 @@ void addFileWithConsumedAppendableAddsFile() throws IOException {
157160
assertThatFileAdded(Kind.SOURCE, "com/example/HelloWorld.java").isEqualTo("{}");
158161
}
159162

160-
private AbstractStringAssert<?> assertThatFileAdded(Kind kind, String path)
163+
@Test
164+
void handleFileWhenFileDoesNotExist() throws IOException {
165+
this.generatedFiles.setFileHandler(new TestFileHandler());
166+
AtomicBoolean called = new AtomicBoolean(false);
167+
this.generatedFiles.handleFile(Kind.RESOURCE, "META-INF/test", handler -> {
168+
called.set(true);
169+
handler.create(createSource("content"));
170+
});
171+
assertThat(called).isTrue();
172+
assertThatFileAdded(Kind.RESOURCE, "META-INF/test").isEqualTo("content").hasOverride(false);
173+
}
174+
175+
@Test
176+
void handleFileWhenFileExistsCanOverride() throws IOException {
177+
this.generatedFiles.setFileHandler(new TestFileHandler(createSource("existing")));
178+
AtomicBoolean called = new AtomicBoolean(false);
179+
this.generatedFiles.handleFile(Kind.RESOURCE, "META-INF/test", handler -> {
180+
called.set(true);
181+
handler.override(createSource("overridden"));
182+
});
183+
assertThat(called).isTrue();
184+
assertThatFileAdded(Kind.RESOURCE, "META-INF/test").isEqualTo("overridden").hasOverride(true);
185+
}
186+
187+
@Test
188+
void handleFileWhenFileExistsCanOverrideUsingExistingContent() throws IOException {
189+
this.generatedFiles.setFileHandler(new TestFileHandler(createSource("existing")));
190+
AtomicBoolean called = new AtomicBoolean(false);
191+
this.generatedFiles.handleFile(Kind.RESOURCE, "META-INF/test", handler -> {
192+
called.set(true);
193+
String existing = readSource(handler.getContent());
194+
handler.override(createSource(existing+"-override"));
195+
});
196+
assertThat(called).isTrue();
197+
assertThatFileAdded(Kind.RESOURCE, "META-INF/test").isEqualTo("existing-override").hasOverride(true);
198+
}
199+
200+
@Test
201+
void handleFileWhenFileExistsFailedToCreate() {
202+
TestFileHandler fileHandler = new TestFileHandler(createSource("existing"));
203+
this.generatedFiles.setFileHandler(fileHandler);
204+
assertThatIllegalStateException()
205+
.isThrownBy(() -> this.generatedFiles.handleFile(Kind.RESOURCE, "META-INF/test", handler ->
206+
handler.create(createSource("should fail"))))
207+
.withMessage("%s already exists".formatted(fileHandler));
208+
}
209+
210+
private static InputStreamSource createSource(String content) {
211+
return new ByteArrayResource(content.getBytes(StandardCharsets.UTF_8));
212+
}
213+
214+
private static String readSource(InputStreamSource content) throws IOException {
215+
ByteArrayOutputStream out = new ByteArrayOutputStream();
216+
content.getInputStream().transferTo(out);
217+
return out.toString(StandardCharsets.UTF_8);
218+
}
219+
220+
private GeneratedFileAssert assertThatFileAdded(Kind kind, String path)
161221
throws IOException {
162222
return this.generatedFiles.assertThatFileAdded(kind, path);
163223
}
164224

225+
165226
static class TestGeneratedFiles extends GeneratedFiles {
166227

167228
private Kind kind;
168229

169230
private String path;
170231

171-
private final TestFileHandler fileHandler = new TestFileHandler();
232+
private TestFileHandler fileHandler = new TestFileHandler();
233+
234+
void setFileHandler(TestFileHandler fileHandler) {
235+
this.fileHandler = fileHandler;
236+
}
172237

173238
@Override
174239
public void handleFile(Kind kind, String path, ThrowingConsumer<FileHandler> handler) {
@@ -177,29 +242,51 @@ public void handleFile(Kind kind, String path, ThrowingConsumer<FileHandler> han
177242
handler.accept(this.fileHandler);
178243
}
179244

180-
AbstractStringAssert<?> assertThatFileAdded(Kind kind, String path)
245+
GeneratedFileAssert assertThatFileAdded(Kind kind, String path)
181246
throws IOException {
182247
assertThat(this.kind).as("kind").isEqualTo(kind);
183248
assertThat(this.path).as("path").isEqualTo(path);
184249
assertThat(this.fileHandler.content).as("content").isNotNull();
185-
ByteArrayOutputStream out = new ByteArrayOutputStream();
186-
this.fileHandler.content.getInputStream().transferTo(out);
187-
return assertThat(out.toString(StandardCharsets.UTF_8));
250+
return new GeneratedFileAssert(this.fileHandler);
251+
}
252+
}
253+
254+
private static class GeneratedFileAssert extends AbstractStringAssert<GeneratedFileAssert> {
255+
256+
private final TestFileHandler fileHandler;
257+
258+
GeneratedFileAssert(TestFileHandler fileHandler) throws IOException {
259+
super(readSource(fileHandler.content), GeneratedFileAssert.class);
260+
this.fileHandler = fileHandler;
188261
}
189262

190-
private static class TestFileHandler extends FileHandler {
263+
public GeneratedFileAssert hasOverride(boolean expected) {
264+
assertThat(this.fileHandler.override).isEqualTo(expected);
265+
return this.myself;
266+
}
267+
}
268+
269+
private static class TestFileHandler extends FileHandler {
270+
271+
@Nullable
272+
private InputStreamSource content;
191273

192-
@Nullable
193-
private InputStreamSource content;
274+
@Nullable
275+
private Boolean override;
194276

195-
TestFileHandler() {
196-
super(false, () -> null);
197-
}
277+
TestFileHandler(@Nullable InputStreamSource content) {
278+
super(content != null, () -> content);
279+
this.content = content;
280+
}
281+
282+
TestFileHandler() {
283+
this(null);
284+
}
198285

199-
@Override
200-
protected void copy(InputStreamSource content, boolean override) {
201-
this.content = content;
202-
}
286+
@Override
287+
protected void copy(InputStreamSource content, boolean override) {
288+
this.content = content;
289+
this.override = override;
203290
}
204291
}
205292

0 commit comments

Comments
 (0)