11package com .github .stickerifier .stickerify .junit ;
22
3- import com .github .stickerifier .stickerify .logger .StructuredLogger ;
43import org .junit .jupiter .api .extension .AfterAllCallback ;
54import org .junit .jupiter .api .extension .ExtensionContext ;
6- import org .slf4j .event .Level ;
75
86import java .io .IOException ;
97import java .nio .file .Files ;
1513 */
1614public class TempFilesCleanupExtension implements AfterAllCallback {
1715
18- private static final StructuredLogger LOGGER = new StructuredLogger (TempFilesCleanupExtension .class );
19-
2016 @ Override
2117 public void afterAll (ExtensionContext context ) throws IOException {
2218 deleteTempFiles ();
@@ -26,23 +22,17 @@ private void deleteTempFiles() throws IOException {
2622 var tempFolder = System .getProperty ("java.io.tmpdir" );
2723
2824 try (var files = Files .list (Path .of (tempFolder ))) {
29- files .filter (this ::stickerifyFiles ).forEach (this ::deleteFile );
25+ for (var file : files .toList ()) {
26+ if (isStickerifyFile (file )) {
27+ Files .delete (file );
28+ }
29+ }
3030 }
3131 }
3232
33- private boolean stickerifyFiles (Path path ) {
33+ private boolean isStickerifyFile (Path path ) {
3434 var fileName = path .getFileName ().toString ();
3535
3636 return Files .isRegularFile (path ) && (fileName .startsWith ("Stickerify-" ) || fileName .startsWith ("OriginalFile-" ));
3737 }
38-
39- private void deleteFile (Path path ) {
40- try {
41- Files .delete (path );
42-
43- LOGGER .at (Level .TRACE ).addKeyValue ("file_name" , path .getFileName ()).log ("The file has been deleted" );
44- } catch (IOException e ) {
45- LOGGER .at (Level .WARN ).setCause (e ).addKeyValue ("file_name" , path .getFileName ()).log ("The file could not be deleted from the system" );
46- }
47- }
4838}
0 commit comments