Skip to content

Commit 63df2e2

Browse files
committed
Update StandardFileWatchServiceTest.java
1 parent c447125 commit 63df2e2

1 file changed

Lines changed: 85 additions & 36 deletions

File tree

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

Lines changed: 85 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.microsphere.io;
1818

1919
import io.microsphere.AbstractTestCase;
20+
import io.microsphere.io.event.DefaultFileChangedListener;
2021
import io.microsphere.io.event.FileChangedEvent;
2122
import io.microsphere.io.event.FileChangedListener;
2223
import io.microsphere.io.event.LoggingFileChangedListener;
@@ -31,8 +32,13 @@
3132
import java.util.concurrent.CountDownLatch;
3233
import java.util.concurrent.ExecutorService;
3334
import java.util.concurrent.Future;
35+
import java.util.concurrent.atomic.AtomicReference;
3436

37+
import static io.microsphere.concurrent.ExecutorUtils.shutdown;
38+
import static io.microsphere.io.FileUtils.deleteDirectory;
3539
import static io.microsphere.io.FileUtils.forceDelete;
40+
import static io.microsphere.io.event.FileChangedEvent.Kind.CREATED;
41+
import static io.microsphere.io.event.FileChangedEvent.Kind.DELETED;
3642
import static io.microsphere.io.event.FileChangedEvent.Kind.values;
3743
import static io.microsphere.util.ClassLoaderUtils.getResource;
3844
import static io.microsphere.util.ExceptionUtils.wrap;
@@ -53,61 +59,104 @@
5359
*/
5460
public class StandardFileWatchServiceTest extends AbstractTestCase {
5561

56-
private static final String TEST_FILE_LOCATION = "test.txt";
57-
58-
private StandardFileWatchService fileWatchService;
59-
60-
private File sourceFile;
61-
62-
private File targetFile;
63-
64-
private CountDownLatch countDownLatch;
62+
private File testDir;
6563

6664
private ExecutorService executor;
6765

6866
@BeforeEach
6967
public void init() throws Exception {
70-
StandardFileWatchService fileWatchService = new StandardFileWatchService(commonPool());
71-
URL resource = getResource(this.getClass().getClassLoader(), TEST_FILE_LOCATION);
72-
String resourceFilePath = resource.getFile();
73-
this.sourceFile = new File(resourceFilePath);
74-
File targetDir = createRandomTempDirectory();
75-
76-
this.fileWatchService = fileWatchService;
77-
this.targetFile = new File(targetDir, this.sourceFile.getName());
78-
this.countDownLatch = new CountDownLatch(3);
68+
this.testDir = createRandomTempDirectory();
7969
this.executor = newSingleThreadExecutor();
80-
81-
fileWatchService.watch(targetFile, new MyFileChangedListener(this.countDownLatch), values());
82-
fileWatchService.watch(targetFile, new LoggingFileChangedListener());
83-
fileWatchService.watch(targetFile, new FileChangedListener() {
84-
});
85-
fileWatchService.start();
86-
87-
assertThrows(IllegalStateException.class, fileWatchService::start);
8870
}
8971

9072
@AfterEach
9173
public void destroy() throws Exception {
92-
this.fileWatchService.stop();
93-
this.executor.shutdown();
74+
shutdown(this.executor);
75+
deleteDirectory(this.testDir);
9476
}
9577

9678
@Test
97-
public void test() throws Exception {
98-
// create file
99-
Path sourcePath = this.sourceFile.toPath();
100-
Path targetFilePath = this.targetFile.toPath();
101-
copy(sourcePath, targetFilePath, REPLACE_EXISTING);
79+
public void testFile() throws Exception {
80+
URL resource = getResource(super.classLoader, "test.txt");
81+
String resourceFilePath = resource.getFile();
82+
File sourceFile = new File(resourceFilePath);
83+
84+
File targetFile = new File(this.testDir, sourceFile.getName());
85+
86+
CountDownLatch countDownLatch = new CountDownLatch(3);
87+
88+
try (StandardFileWatchService fileWatchService = new StandardFileWatchService(commonPool())) {
89+
// watch file and attach listeners
90+
fileWatchService.watch(targetFile, new MyFileChangedListener(targetFile, countDownLatch), values());
91+
fileWatchService.watch(targetFile, new LoggingFileChangedListener());
92+
fileWatchService.watch(targetFile, new DefaultFileChangedListener());
10293

103-
countDownLatch.await();
94+
// start StandardFileWatchService
95+
fileWatchService.start();
96+
// start again
97+
assertThrows(IllegalStateException.class, fileWatchService::start);
98+
99+
// copy file
100+
Path sourcePath = sourceFile.toPath();
101+
Path targetFilePath = targetFile.toPath();
102+
copy(sourcePath, targetFilePath, REPLACE_EXISTING);
103+
104+
// await for completion
105+
countDownLatch.await();
106+
}
107+
}
108+
109+
@Test
110+
public void testDirectory() throws Exception {
111+
112+
AtomicReference<File> fileReference = new AtomicReference<>();
113+
114+
try (StandardFileWatchService fileWatchService = new StandardFileWatchService()) {
115+
116+
fileWatchService.watch(this.testDir, new FileChangedListener() {
117+
@Override
118+
public void onFileCreated(FileChangedEvent event) {
119+
File file = event.getFile();
120+
fileReference.set(file);
121+
log("The file[path: '{}'] is created", file);
122+
}
123+
124+
@Override
125+
public void onFileDeleted(FileChangedEvent event) {
126+
File file = event.getFile();
127+
fileReference.set(file);
128+
log("The file[path: '{}'] is deleted", file);
129+
}
130+
}, CREATED, DELETED);
131+
132+
fileWatchService.start();
133+
134+
assertThrows(IllegalStateException.class, fileWatchService::start);
135+
136+
// create a test file
137+
File testFile = createRandomFile(testDir);
138+
139+
while (!testFile.equals(fileReference.get())) {
140+
// spin
141+
}
142+
143+
// delete the test file
144+
testFile.delete();
145+
146+
while (fileReference.get() == null) {
147+
// spin
148+
}
149+
}
104150
}
105151

106152
private class MyFileChangedListener implements FileChangedListener {
107153

108-
private CountDownLatch countDownLatch;
154+
private final File targetFile;
155+
156+
private final CountDownLatch countDownLatch;
109157

110-
public MyFileChangedListener(CountDownLatch countDownLatch) {
158+
public MyFileChangedListener(File targetFile, CountDownLatch countDownLatch) {
159+
this.targetFile = targetFile;
111160
this.countDownLatch = countDownLatch;
112161
}
113162

0 commit comments

Comments
 (0)