|
27 | 27 | import javax.annotation.Nullable; |
28 | 28 | import java.io.File; |
29 | 29 | import java.io.IOException; |
| 30 | +import java.nio.file.Files; |
| 31 | +import java.nio.file.Path; |
30 | 32 | import java.util.ArrayList; |
31 | 33 | import java.util.Arrays; |
| 34 | +import java.util.Comparator; |
32 | 35 | import java.util.HashMap; |
33 | 36 | import java.util.List; |
34 | 37 | import java.util.Map; |
35 | 38 | import java.util.Random; |
36 | 39 | import java.util.concurrent.CountDownLatch; |
37 | 40 | import java.util.concurrent.TimeUnit; |
| 41 | +import java.util.stream.Stream; |
38 | 42 |
|
39 | | -import static org.junit.Assert.assertArrayEquals; |
40 | 43 | import static org.junit.Assert.assertEquals; |
41 | 44 | import static org.junit.Assert.assertTrue; |
| 45 | +import static org.junit.Assert.fail; |
42 | 46 |
|
43 | 47 | public abstract class AbstractObjectBoxTest { |
44 | 48 |
|
@@ -152,28 +156,28 @@ public void tearDown() { |
152 | 156 | logError("Could not clean up test", e); |
153 | 157 | } |
154 | 158 | } |
155 | | - deleteAllFiles(); |
| 159 | + deleteAllFiles(boxStoreDir); |
156 | 160 | } |
157 | 161 |
|
158 | | - protected void deleteAllFiles() { |
| 162 | + protected void deleteAllFiles(@Nullable File boxStoreDir) { |
159 | 163 | 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"); |
163 | 177 | } |
164 | | - delete(boxStoreDir); |
165 | 178 | } |
166 | 179 | } |
167 | 180 |
|
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 | | - |
177 | 181 | protected void log(String text) { |
178 | 182 | System.out.println(text); |
179 | 183 | } |
|
0 commit comments