Skip to content

Commit b6d568c

Browse files
committed
Update FileUtilsTest.java
1 parent bda068e commit b6d568c

1 file changed

Lines changed: 46 additions & 73 deletions

File tree

microsphere-java-core/src/test/java/io/microsphere/io/FileUtilsTest.java

Lines changed: 46 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import org.junit.jupiter.api.Test;
66

77
import java.io.File;
8+
import java.io.FileOutputStream;
89
import java.io.IOException;
910
import java.net.URL;
11+
import java.util.concurrent.ExecutorService;
12+
import java.util.concurrent.atomic.AtomicInteger;
1013

14+
import static io.microsphere.concurrent.CustomizedThreadFactory.newThreadFactory;
1115
import static io.microsphere.io.FileUtils.cleanDirectory;
1216
import static io.microsphere.io.FileUtils.deleteDirectory;
1317
import static io.microsphere.io.FileUtils.forceDelete;
@@ -21,6 +25,8 @@
2125
import static io.microsphere.util.StringUtils.EMPTY_STRING;
2226
import static io.microsphere.util.SystemUtils.IS_OS_WINDOWS;
2327
import static io.microsphere.util.SystemUtils.JAVA_IO_TMPDIR;
28+
import static java.util.concurrent.Executors.newFixedThreadPool;
29+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2430
import static org.junit.jupiter.api.Assertions.assertEquals;
2531
import static org.junit.jupiter.api.Assertions.assertFalse;
2632
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -147,7 +153,6 @@ public void testDeleteDirectoryOnIOException() throws Exception {
147153
// while (true) {
148154
// try {
149155
// deleteDirectory(testDir);
150-
// sleep(500);
151156
// } catch (IOException e) {
152157
// exception = e;
153158
// running.set(false);
@@ -222,79 +227,47 @@ public void testForceDeleteOnFileNotExists() {
222227

223228
@Test
224229
public void testForceDeleteOnIOException() throws Exception {
230+
File testFile = createRandomTempFile();
231+
232+
ExecutorService executor = newFixedThreadPool(3, newThreadFactory("testForceDelete-", true));
233+
234+
// status : 0 -> init
235+
// status : 1 -> writing
236+
// status : 2 -> deleting
237+
AtomicInteger status = new AtomicInteger(0);
238+
239+
executor.submit(() -> {
240+
synchronized (testFile) {
241+
try (FileOutputStream outputStream = new FileOutputStream(testFile, true)) {
242+
outputStream.write('a');
243+
status.set(1);
244+
// wait for notification
245+
testFile.wait();
246+
}
247+
}
248+
return null;
249+
});
225250

226-
// if (IS_OS_WINDOWS) {
227-
// File testFile = createRandomTempFile();
228-
//
229-
// ExecutorService executor = newFixedThreadPool(3);
230-
//
231-
// // status : 0 -> init
232-
// // status : 1 -> writing
233-
// // status : 2 -> deleting
234-
// AtomicInteger status = new AtomicInteger(0);
235-
//
236-
// executor.submit(() -> {
237-
// synchronized (testFile) {
238-
// try (FileOutputStream outputStream = new FileOutputStream(testFile, true)) {
239-
// outputStream.write('a');
240-
// status.set(1);
241-
// // wait for notification
242-
// testFile.wait();
243-
// }
244-
// }
245-
// return null;
246-
// });
247-
//
248-
// executor.submit(() -> {
249-
// while (status.get() != 1) {
250-
// }
251-
// assertThrows(IOException.class, () -> forceDelete(testFile));
252-
// status.set(2);
253-
// return null;
254-
// });
255-
//
256-
// executor.submit(() -> {
257-
// while (status.get() != 2) {
258-
// }
259-
// synchronized (testFile) {
260-
// testFile.notify();
261-
// }
262-
// return null;
263-
// });
264-
//
265-
// executor.awaitTermination(100, MILLISECONDS);
266-
//
267-
// executor.shutdown();
268-
// return;
269-
// }
270-
//
271-
// File root = new File(USER_HOME);
272-
// Path readOnlyFilePath = walkFileTree(root.toPath(), new SimpleFileVisitor<Path>() {
273-
// @Override
274-
// public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
275-
// if (!attrs.isRegularFile()) {
276-
// return CONTINUE;
277-
// }
278-
// if (attrs instanceof DosFileAttributes) {
279-
// DosFileAttributes dosFileAttributes = (DosFileAttributes) attrs;
280-
// if (dosFileAttributes.isReadOnly()) {
281-
// return TERMINATE;
282-
// }
283-
// } else if (attrs instanceof PosixFileAttributes) {
284-
// PosixFileAttributes posixFileAttributes = (PosixFileAttributes) attrs;
285-
// Set<PosixFilePermission> permissions = posixFileAttributes.permissions();
286-
// if (!permissions.contains(OWNER_WRITE)) {
287-
// return TERMINATE;
288-
//
289-
// }
290-
// }
291-
// return super.visitFile(file, attrs);
292-
// }
293-
// });
294-
//
295-
// if (exists(readOnlyFilePath)) {
296-
// assertThrows(IOException.class, () -> forceDelete(readOnlyFilePath.toFile()));
297-
// }
251+
executor.submit(() -> {
252+
while (status.get() != 1) {
253+
}
254+
assertThrows(IOException.class, () -> forceDelete(testFile));
255+
status.set(2);
256+
return null;
257+
});
258+
259+
executor.submit(() -> {
260+
while (status.get() != 2) {
261+
}
262+
synchronized (testFile) {
263+
testFile.notify();
264+
}
265+
return null;
266+
});
267+
268+
executor.awaitTermination(100, MILLISECONDS);
269+
270+
executor.shutdown();
298271
}
299272

300273
@Test

0 commit comments

Comments
 (0)