Skip to content

Commit 6554898

Browse files
AbstractObjectBoxTest: use NIO to delete files, expose deleteAllFiles.
1 parent a8e806c commit 6554898

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

tests/objectbox-java-test/src/test/java/io/objectbox/AbstractObjectBoxTest.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@
2727
import javax.annotation.Nullable;
2828
import java.io.File;
2929
import java.io.IOException;
30+
import java.nio.file.Files;
31+
import java.nio.file.Path;
3032
import java.util.ArrayList;
3133
import java.util.Arrays;
34+
import java.util.Comparator;
3235
import java.util.HashMap;
3336
import java.util.List;
3437
import java.util.Map;
3538
import java.util.Random;
3639
import java.util.concurrent.CountDownLatch;
3740
import java.util.concurrent.TimeUnit;
41+
import java.util.stream.Stream;
3842

39-
import static org.junit.Assert.assertArrayEquals;
4043
import static org.junit.Assert.assertEquals;
4144
import static org.junit.Assert.assertTrue;
45+
import static org.junit.Assert.fail;
4246

4347
public abstract class AbstractObjectBoxTest {
4448

@@ -152,28 +156,28 @@ public void tearDown() {
152156
logError("Could not clean up test", e);
153157
}
154158
}
155-
deleteAllFiles();
159+
deleteAllFiles(boxStoreDir);
156160
}
157161

158-
protected void deleteAllFiles() {
162+
protected void deleteAllFiles(@Nullable File boxStoreDir) {
159163
if (boxStoreDir != null && boxStoreDir.exists()) {
160-
File[] files = boxStoreDir.listFiles();
161-
for (File file : files) {
162-
delete(file);
164+
try (Stream<Path> stream = Files.walk(boxStoreDir.toPath())) {
165+
stream.sorted(Comparator.reverseOrder())
166+
.forEach(path -> {
167+
try {
168+
Files.delete(path);
169+
} catch (IOException e) {
170+
logError("Could not delete file", e);
171+
fail("Could not delete file");
172+
}
173+
});
174+
} catch (IOException e) {
175+
logError("Could not delete file", e);
176+
fail("Could not delete file");
163177
}
164-
delete(boxStoreDir);
165178
}
166179
}
167180

168-
private boolean delete(File file) {
169-
boolean deleted = file.delete();
170-
if (!deleted) {
171-
file.deleteOnExit();
172-
logError("Could not delete " + file.getAbsolutePath());
173-
}
174-
return deleted;
175-
}
176-
177181
protected void log(String text) {
178182
System.out.println(text);
179183
}

0 commit comments

Comments
 (0)