19
19
import java .io .ByteArrayOutputStream ;
20
20
import java .io .IOException ;
21
21
import java .nio .charset .StandardCharsets ;
22
+ import java .util .concurrent .atomic .AtomicBoolean ;
22
23
23
24
import javax .lang .model .element .Modifier ;
24
25
25
26
import org .assertj .core .api .AbstractStringAssert ;
26
27
import org .junit .jupiter .api .Test ;
27
28
29
+ import org .springframework .aot .generate .GeneratedFiles .FileHandler ;
28
30
import org .springframework .aot .generate .GeneratedFiles .Kind ;
29
31
import org .springframework .core .io .ByteArrayResource ;
30
32
import org .springframework .core .io .InputStreamSource ;
37
39
38
40
import static org .assertj .core .api .Assertions .assertThat ;
39
41
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
42
+ import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
40
43
41
44
/**
42
45
* Tests for {@link GeneratedFiles}.
@@ -157,18 +160,80 @@ void addFileWithConsumedAppendableAddsFile() throws IOException {
157
160
assertThatFileAdded (Kind .SOURCE , "com/example/HelloWorld.java" ).isEqualTo ("{}" );
158
161
}
159
162
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 )
161
221
throws IOException {
162
222
return this .generatedFiles .assertThatFileAdded (kind , path );
163
223
}
164
224
225
+
165
226
static class TestGeneratedFiles extends GeneratedFiles {
166
227
167
228
private Kind kind ;
168
229
169
230
private String path ;
170
231
171
- private final TestFileHandler fileHandler = new TestFileHandler ();
232
+ private TestFileHandler fileHandler = new TestFileHandler ();
233
+
234
+ void setFileHandler (TestFileHandler fileHandler ) {
235
+ this .fileHandler = fileHandler ;
236
+ }
172
237
173
238
@ Override
174
239
public void handleFile (Kind kind , String path , ThrowingConsumer <FileHandler > handler ) {
@@ -177,29 +242,51 @@ public void handleFile(Kind kind, String path, ThrowingConsumer<FileHandler> han
177
242
handler .accept (this .fileHandler );
178
243
}
179
244
180
- AbstractStringAssert <?> assertThatFileAdded (Kind kind , String path )
245
+ GeneratedFileAssert assertThatFileAdded (Kind kind , String path )
181
246
throws IOException {
182
247
assertThat (this .kind ).as ("kind" ).isEqualTo (kind );
183
248
assertThat (this .path ).as ("path" ).isEqualTo (path );
184
249
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 ;
188
261
}
189
262
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 ;
191
273
192
- @ Nullable
193
- private InputStreamSource content ;
274
+ @ Nullable
275
+ private Boolean override ;
194
276
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
+ }
198
285
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 ;
203
290
}
204
291
}
205
292
0 commit comments