Skip to content

Commit 05c9b72

Browse files
committed
WIP3
added all tests. See also https://bugs.openjdk.org/browse/JDK-8202759
1 parent a5ff718 commit 05c9b72

2 files changed

Lines changed: 85 additions & 135 deletions

File tree

metafacture-io/src/main/java/org/metafacture/io/DirectoryListener.java

Lines changed: 30 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -85,29 +85,19 @@ public void process(final String directory) {
8585
}
8686
System.out.println("Directory is:" + directory);
8787

88-
/* try {
89-
dir.register(
90-
watcher,
91-
ENTRY_CREATE);
92-
}
93-
catch (IOException e) {
94-
throw new RuntimeException(e);
95-
}*/
96-
9788
new Thread("DirectoryListener") {
89+
WatchKey key;
9890
public void run() {
9991

100-
10192
while (true) {
102-
WatchKey key;
10393
try {
10494
key = watcher.take();
10595
} catch (InterruptedException x) {
10696
return;
10797
}
10898
Path dir = keys.get(key);
10999
if (dir == null) {
110-
System.err.println("WatchKey not recognized!!");
100+
System.err.println("WatchKey not recognized!");
111101
continue;
112102
}
113103

@@ -116,41 +106,41 @@ public void run() {
116106
if (event.kind() == OVERFLOW) {
117107
throw new OpenFailed("Overflow event occurred on directory " + directory);
118108
}
119-
System.out.println(
120-
"Event kind:" + event.kind()
109+
System.out.println("Event kind:" + event.kind()
121110
+ ". File affected: " + event.context() + ".");
122111

123112
@SuppressWarnings("unchecked")
124-
Path path = ((WatchEvent<Path>) event).context();
125-
String pathName = dir.resolve(path).toAbsolutePath().toString();
126-
127-
try {
128-
if (Files.isDirectory(Path.of(pathName), LinkOption.NOFOLLOW_LINKS)) {
129-
registerAll(Path.of(pathName));
113+
Path fileName = ((WatchEvent<Path>) event).context();
114+
Path absolutePath = dir.resolve(fileName);
115+
System.out.println("PATH: "+ absolutePath.toString());
116+
if (Files.isDirectory(absolutePath, LinkOption.NOFOLLOW_LINKS)) {
117+
try {
118+
registerAll(absolutePath);
130119
}
120+
catch (IOException e) {
121+
throw new OpenFailed("IOException event occurred on directory " + directory, e);
122+
}
123+
} else {
124+
if (fileName.toString().equals(TRIGGER_SHUTDOWN_FILENAME)) {
125+
//getReceiver().closeStream();
126+
System.out.print(".... closing");
127+
closeStream();
128+
System.out.print(".... ret");
129+
return;
130+
}
131+
System.out.println("Pathname ist:" + absolutePath.toString() +" going to process super!");
132+
getReceiver().process(absolutePath.toString());
131133
}
132-
catch (IOException e) {
133-
// ignore to keep sample readable
134-
}
135-
136-
System.out.println("1=" + pathName);
137-
/* pathName = dir.relativize(path).getFileName().toString();
138-
System.out.println("2="+pathName);*/
139-
System.out.println("3=" + pathName);
140-
pathName = dir.resolve(path).toAbsolutePath().toString();
141-
System.out.println("4=" + pathName);
142-
143-
if (pathName.equals(TRIGGER_SHUTDOWN_FILENAME)) {
144-
//getReceiver().closeStream();
145-
System.out.print(".... closing");
146-
closeStream();
147-
System.out.print(".... ret");
148-
return;
134+
}
135+
// reset key and remove from set if directory no longer accessible
136+
boolean valid = key.reset();
137+
if (!valid) {
138+
keys.remove(key);
139+
// all directories are inaccessible
140+
if (keys.isEmpty()) {
141+
break;
149142
}
150-
System.out.println("Pathname ist:" + pathName);
151-
getReceiver().process(pathName);
152143
}
153-
key.reset();
154144
}
155145
}
156146
}.start();
@@ -192,68 +182,4 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
192182
});
193183
}
194184

195-
196-
197-
/**
198-
* Process all events for keys queued to the watcher
199-
*/
200-
void processEvents() {
201-
for (;;) {
202-
203-
// wait for key to be signalled
204-
WatchKey key;
205-
try {
206-
key = watcher.take();
207-
} catch (InterruptedException x) {
208-
return;
209-
}
210-
211-
Path dir = keys.get(key);
212-
if (dir == null) {
213-
System.err.println("WatchKey not recognized!!");
214-
continue;
215-
}
216-
217-
for (WatchEvent<?> event: key.pollEvents()) {
218-
WatchEvent.Kind kind = event.kind();
219-
220-
// TBD - provide example of how OVERFLOW event is handled
221-
if (kind == OVERFLOW) {
222-
continue;
223-
}
224-
225-
// Context for directory entry event is the file name of entry
226-
WatchEvent<Path> ev = cast(event);
227-
Path name = ev.context();
228-
Path child = dir.resolve(name);
229-
230-
// print out event
231-
System.out.format("%s: %s\n", event.kind().name(), child);
232-
233-
// if directory is created, and watching recursively, then
234-
// register it and its sub-directories
235-
if (kind == ENTRY_CREATE) {
236-
try {
237-
if (Files.isDirectory(child, LinkOption.NOFOLLOW_LINKS)) {
238-
registerAll(child);
239-
}
240-
} catch (IOException x) {
241-
// ignore to keep sample readable
242-
}
243-
}
244-
}
245-
246-
// reset key and remove from set if directory no longer accessible
247-
boolean valid = key.reset();
248-
if (!valid) {
249-
keys.remove(key);
250-
251-
// all directories are inaccessible
252-
if (keys.isEmpty()) {
253-
break;
254-
}
255-
}
256-
}
257-
}
258-
259185
}

metafacture-io/src/test/java/org/metafacture/io/DirectoryListenerTest.java

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@
4040
*
4141
*/
4242
public final class DirectoryListenerTest {
43-
private static final int MAX_SECONDS_WAIT_OF_THREAD = 13 * 1000;
4443

45-
private static final String FILE_NAME = "test";
4644
private static final DirectoryListener dl = new DirectoryListener();
47-
private static String fname;
48-
private String filenameInDirectory;
45+
private static final int MAX_MILLISECONDS_WAITING_OF_THREAD = 3 * 1000;
46+
private static final String FILE_NAME = "test";
47+
private static final String SUBDIRECTORY_NAME = "subdir";
48+
private static String PATH_TO_DIRECTORY;
49+
private static String PATH_TO_SUBDIRECTORY;
50+
4951
@Rule
5052
public MockitoRule mockitoRule = MockitoJUnit.rule();
5153

@@ -57,47 +59,69 @@ public final class DirectoryListenerTest {
5759

5860
@Before
5961
public void setup() {
62+
PATH_TO_DIRECTORY = tempFolder.getRoot() + File.separator;
6063
dl.setReceiver(receiver);
61-
dl.process(tempFolder.getRoot().getPath());
62-
fname = tempFolder.getRoot()+File.separator + FILE_NAME+ "1";
63-
filenameInDirectory =fname+File.separator+FILE_NAME;
64+
dl.process(PATH_TO_DIRECTORY);
65+
PATH_TO_SUBDIRECTORY = PATH_TO_DIRECTORY + SUBDIRECTORY_NAME + File.separator;
6466
}
6567

6668
@Test
6769
public void testFileOccurs() {
68-
createFile(fname);
69-
System.out.println("test if "+filenameInDirectory+ " occured ");
70-
Mockito.verify(receiver, timeout(MAX_SECONDS_WAIT_OF_THREAD)).process(filenameInDirectory);
71-
System.out.println("tested if "+filenameInDirectory+ " occured ");
70+
String pathToTestfile = PATH_TO_DIRECTORY + FILE_NAME;
71+
createFile(pathToTestfile);
72+
Mockito.verify(receiver, timeout(MAX_MILLISECONDS_WAITING_OF_THREAD)).process(pathToTestfile);
73+
}
7274

75+
@Test
76+
public void testFileOccursInSubdirectory() throws InterruptedException {
77+
createDirectory(PATH_TO_SUBDIRECTORY);
78+
String pathToTestfile = PATH_TO_SUBDIRECTORY + FILE_NAME;
79+
Thread.sleep(100); // because of https://bugs.openjdk.org/browse/JDK-8202759
80+
createFile(pathToTestfile);
81+
System.out.println("testFileOccursInSubdirectory if "+pathToTestfile+ " occurs ");
82+
Mockito.verify(receiver, timeout(MAX_MILLISECONDS_WAITING_OF_THREAD)).process(pathToTestfile);
83+
System.out.println("tested if "+pathToTestfile+ " occured ");
7384
}
7485

75-
private void createFile(final String fname) {
76-
System.out.println("Mache File:" + fname);
77-
String directory="dirtest";
78-
Path dir = Path.of(fname);
79-
System.out.println("dir:" + dir.toString());
80-
File testFile = new File( fname+File.separator+FILE_NAME);//dirtest+fname+"/tut");
81-
System.out.println("all File:" + testFile.getAbsolutePath());
82-
try {
83-
Files.createDirectory(dir);
84-
Thread.sleep(12000);
85-
dir = Path.of(fname+"LKNLKNLK");
86-
Files.createDirectory(dir);
87-
testFile.createNewFile();
88-
Thread.sleep(600000);
89-
}
90-
catch (IOException | InterruptedException e) {
91-
throw new RuntimeException(e);
92-
}
86+
@Test
87+
public void testDontProcessDirectoryWithoutFiles() {
88+
String pathToTestfile = SUBDIRECTORY_NAME + FILE_NAME;
89+
createFile(SUBDIRECTORY_NAME);
90+
System.out.println("test if "+pathToTestfile+ " occurs ");
91+
Mockito.verify(receiver, timeout(MAX_MILLISECONDS_WAITING_OF_THREAD).times(0)).process(pathToTestfile);
92+
System.out.println("tested if "+pathToTestfile+ " occured ");
9393
}
9494

9595
@Test
96-
public void testTriggerShutdown() {
96+
public void testTriggerShutdown() throws InterruptedException {
97+
String pathToTestfile = PATH_TO_DIRECTORY + DirectoryListener.TRIGGER_SHUTDOWN_FILENAME;
9798
createFile(tempFolder.getRoot().getPath() + File.separator + DirectoryListener.TRIGGER_SHUTDOWN_FILENAME);
98-
Mockito.verify(receiver, timeout(MAX_SECONDS_WAIT_OF_THREAD)).process(fname);
99+
Mockito.verify(receiver, timeout(MAX_MILLISECONDS_WAITING_OF_THREAD).times(0)).process(PATH_TO_DIRECTORY);
100+
Thread.sleep(100);
99101
Assert.assertTrue(dl.isClosed());
100102
System.out.print(".... closed");
101103
}
102104

105+
private void createFile(final String path) {
106+
System.out.println("Mache File:" + path);
107+
File testFile = new File(path);
108+
try {
109+
testFile.createNewFile();
110+
}
111+
catch (IOException e) {
112+
throw new RuntimeException(e);
113+
}
114+
}
115+
private void createDirectory(final String dir) {
116+
System.out.println("Mache Directory:" + dir);
117+
try {
118+
Files.createDirectory(Path.of(dir));
119+
}
120+
catch (IOException e) {
121+
throw new RuntimeException(e);
122+
}
123+
}
124+
125+
126+
103127
}

0 commit comments

Comments
 (0)