Skip to content

Commit f11021d

Browse files
author
Niall Pemberton
committed
IO-196 FilesystemObserverTestCase failures - revert r1002909 to test case and change how FilesystemMonitor compares files for changes to see if this resolves the original test failures
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1005676 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3f124c6 commit f11021d

3 files changed

Lines changed: 43 additions & 60 deletions

File tree

src/main/java/org/apache/commons/io/monitor/FilesystemEntry.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class FilesystemEntry implements Serializable {
4343

4444
private final FilesystemEntry parent;
4545
private FilesystemEntry[] children;
46-
private File file;
46+
private final File file;
4747
private String name;
4848
private boolean exists;
4949
private boolean directory;
@@ -79,7 +79,7 @@ public FilesystemEntry(FilesystemEntry parent, File file) {
7979
* This implementation refreshes the <code>name</code>, <code>exists</code>
8080
* <code>directory</code> and <code>lastModified</code> properties.
8181
*/
82-
public void refresh() {
82+
public void refresh(File file) {
8383
name = file.getName();
8484
exists = file.exists();
8585
if (exists) {
@@ -109,7 +109,7 @@ public FilesystemEntry newChildInstance(File file) {
109109
*
110110
* @return whether the file has changed or not
111111
*/
112-
public boolean hasChanged() {
112+
public boolean hasChanged(File file) {
113113
return (lastModified != file.lastModified());
114114
}
115115

@@ -160,15 +160,6 @@ public File getFile() {
160160
return file;
161161
}
162162

163-
/**
164-
* Set the file being monitored.
165-
*
166-
* @param file the file being monitored
167-
*/
168-
public void setFile(File file) {
169-
this.file = file;
170-
}
171-
172163
/**
173164
* Return the file name.
174165
*

src/main/java/org/apache/commons/io/monitor/FilesystemObserver.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public Iterable<FilesystemListener> getListeners() {
304304
* @throws Exception if an error occurs
305305
*/
306306
public void initialize() throws Exception {
307-
rootEntry.refresh();
307+
rootEntry.refresh(rootEntry.getFile());
308308
File[] files = listFiles(rootEntry.getFile());
309309
FilesystemEntry[] children = files.length > 0 ? new FilesystemEntry[files.length] : EMPTY_ENTRIES;
310310
for (int i = 0; i < files.length; i++) {
@@ -389,7 +389,7 @@ private void checkAndNotify(FilesystemEntry parent, FilesystemEntry[] previous,
389389
*/
390390
private FilesystemEntry createFileEntry(FilesystemEntry parent, File file) {
391391
FilesystemEntry entry = parent.newChildInstance(file);
392-
entry.refresh();
392+
entry.refresh(file);
393393
File[] files = listFiles(file);
394394
FilesystemEntry[] children = files.length > 0 ? new FilesystemEntry[files.length] : EMPTY_ENTRIES;
395395
for (int i = 0; i < files.length; i++) {
@@ -425,17 +425,16 @@ private void doCreate(FilesystemEntry entry) {
425425
* @param file The current file
426426
*/
427427
private void doMatch(FilesystemEntry entry, File file) {
428-
if (entry.hasChanged()) {
428+
if (entry.hasChanged(file)) {
429429
for (FilesystemListener listener : listeners) {
430430
if (entry.isDirectory()) {
431-
listener.onDirectoryChange(entry.getFile());
431+
listener.onDirectoryChange(file);
432432
} else {
433-
listener.onFileChange(entry.getFile());
433+
listener.onFileChange(file);
434434
}
435435
}
436-
entry.refresh();
436+
entry.refresh(file);
437437
}
438-
entry.setFile(file);
439438
}
440439

441440
/**

src/test/java/org/apache/commons/io/monitor/FilesystemObserverTestCase.java

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ public void testDirectory() {
114114
File testDirBFile1 = touch(new File(testDirB, "B-file1.java"));
115115

116116
checkAndNotify();
117-
checkDirectoryCounts("B", 3, 0, 0);
118-
checkFileCounts("B", 4, 0, 0);
117+
checkCollectionSizes("B", 3, 0, 0, 4, 0, 0);
119118
assertTrue("B testDirA", listener.getCreatedDirectories().contains(testDirA));
120119
assertTrue("B testDirB", listener.getCreatedDirectories().contains(testDirB));
121120
assertTrue("B testDirC", listener.getCreatedDirectories().contains(testDirC));
@@ -131,16 +130,14 @@ public void testDirectory() {
131130
testDirAFile4 = touch(testDirAFile4);
132131
FileUtils.deleteDirectory(testDirB);
133132
checkAndNotify();
134-
checkDirectoryCounts("D", 0, 0, 1);
135-
checkFileCounts("D", 0, 1, 1);
133+
checkCollectionSizes("D", 0, 0, 1, 0, 1, 1);
136134
assertTrue("D testDirB", listener.getDeletedDirectories().contains(testDirB));
137135
assertTrue("D testDirAFile4", listener.getChangedFiles().contains(testDirAFile4));
138136
assertTrue("D testDirBFile1", listener.getDeletedFiles().contains(testDirBFile1));
139137

140138
FileUtils.deleteDirectory(testDir);
141139
checkAndNotify();
142-
checkDirectoryCounts("E", 0, 0, 2);
143-
checkFileCounts("E", 0, 0, 3);
140+
checkCollectionSizes("E", 0, 0, 2, 0, 0, 3);
144141
assertTrue("E testDirA", listener.getDeletedDirectories().contains(testDirA));
145142
assertTrue("E testDirAFile1", listener.getDeletedFiles().contains(testDirAFile1));
146143
assertFalse("E testDirAFile2", listener.getDeletedFiles().contains(testDirAFile2));
@@ -177,8 +174,7 @@ public void testFileCreate() {
177174
File testDirAFile5 = new File(testDirA, "A-file5.java");
178175

179176
checkAndNotify();
180-
checkDirectoryCounts("B", 1, 0, 0);
181-
checkFileCounts("B", 2, 0, 0);
177+
checkCollectionSizes("B", 1, 0, 0, 2, 0, 0);
182178
assertFalse("B testDirAFile1", listener.getCreatedFiles().contains(testDirAFile1));
183179
assertTrue("B testDirAFile2", listener.getCreatedFiles().contains(testDirAFile2));
184180
assertFalse("B testDirAFile3", listener.getCreatedFiles().contains(testDirAFile3));
@@ -196,22 +192,25 @@ public void testFileCreate() {
196192

197193
// Create file with name < first entry
198194
testDirAFile1 = touch(testDirAFile1);
195+
testDirA = touch(testDirA);
199196
checkAndNotify();
200-
checkFileCounts("D", 1, 0, 0);
197+
checkCollectionSizes("D", 0, 1, 0, 1, 0, 0);
201198
assertTrue("D testDirAFile1 exists", testDirAFile1.exists());
202199
assertTrue("D testDirAFile1", listener.getCreatedFiles().contains(testDirAFile1));
203200

204201
// Create file with name between 2 entries
205202
testDirAFile3 = touch(testDirAFile3);
203+
testDirA = touch(testDirA);
206204
checkAndNotify();
207-
checkFileCounts("E", 1, 0, 0);
205+
checkCollectionSizes("E", 0, 1, 0, 1, 0, 0);
208206
assertTrue("E testDirAFile3 exists", testDirAFile3.exists());
209207
assertTrue("E testDirAFile3", listener.getCreatedFiles().contains(testDirAFile3));
210208

211209
// Create file with name > last entry
212210
testDirAFile5 = touch(testDirAFile5);
211+
testDirA = touch(testDirA);
213212
checkAndNotify();
214-
checkFileCounts("F", 1, 0, 0);
213+
checkCollectionSizes("F", 0, 1, 0, 1, 0, 0);
215214
assertTrue("F testDirAFile5 exists", testDirAFile5.exists());
216215
assertTrue("F testDirAFile5", listener.getCreatedFiles().contains(testDirAFile5));
217216
} catch (Exception e) {
@@ -237,8 +236,7 @@ public void testFileUpdate() {
237236
File testDirAFile5 = touch(new File(testDirA, "A-file5.java"));
238237

239238
checkAndNotify();
240-
checkDirectoryCounts("B", 1, 0, 0);
241-
checkFileCounts("B", 5, 0, 0);
239+
checkCollectionSizes("B", 1, 0, 0, 5, 0, 0);
242240
assertTrue("B testDirAFile1", listener.getCreatedFiles().contains(testDirAFile1));
243241
assertTrue("B testDirAFile2", listener.getCreatedFiles().contains(testDirAFile2));
244242
assertTrue("B testDirAFile3", listener.getCreatedFiles().contains(testDirAFile3));
@@ -256,20 +254,23 @@ public void testFileUpdate() {
256254

257255
// Update first entry
258256
testDirAFile1 = touch(testDirAFile1);
257+
testDirA = touch(testDirA);
259258
checkAndNotify();
260-
checkFileCounts("D", 0, 1, 0);
259+
checkCollectionSizes("D", 0, 1, 0, 0, 1, 0);
261260
assertTrue("D testDirAFile1", listener.getChangedFiles().contains(testDirAFile1));
262261

263262
// Update file with name between 2 entries
264263
testDirAFile3 = touch(testDirAFile3);
264+
testDirA = touch(testDirA);
265265
checkAndNotify();
266-
checkFileCounts("E", 0, 1, 0);
266+
checkCollectionSizes("E", 0, 1, 0, 0, 1, 0);
267267
assertTrue("E testDirAFile3", listener.getChangedFiles().contains(testDirAFile3));
268268

269269
// Update last entry
270270
testDirAFile5 = touch(testDirAFile5);
271+
testDirA = touch(testDirA);
271272
checkAndNotify();
272-
checkFileCounts("F", 0, 1, 0);
273+
checkCollectionSizes("F", 0, 1, 0, 0, 1, 0);
273274
assertTrue("F testDirAFile5", listener.getChangedFiles().contains(testDirAFile5));
274275
} catch (Exception e) {
275276
fail("Threw " + e);
@@ -300,8 +301,7 @@ public void testFileDelete() {
300301
assertTrue("B testDirAFile5 exists", testDirAFile5.exists());
301302

302303
checkAndNotify();
303-
checkDirectoryCounts("B", 1, 0, 0);
304-
checkFileCounts("B", 5, 0, 0);
304+
checkCollectionSizes("B", 1, 0, 0, 5, 0, 0);
305305
assertTrue("B testDirAFile1", listener.getCreatedFiles().contains(testDirAFile1));
306306
assertTrue("B testDirAFile2", listener.getCreatedFiles().contains(testDirAFile2));
307307
assertTrue("B testDirAFile3", listener.getCreatedFiles().contains(testDirAFile3));
@@ -313,22 +313,25 @@ public void testFileDelete() {
313313

314314
// Delete first entry
315315
FileUtils.deleteQuietly(testDirAFile1);
316+
testDirA = touch(testDirA);
316317
checkAndNotify();
317-
checkFileCounts("D", 0, 0, 1);
318+
checkCollectionSizes("D", 0, 1, 0, 0, 0, 1);
318319
assertFalse("D testDirAFile1 exists", testDirAFile1.exists());
319320
assertTrue("D testDirAFile1", listener.getDeletedFiles().contains(testDirAFile1));
320321

321322
// Delete file with name between 2 entries
322323
FileUtils.deleteQuietly(testDirAFile3);
324+
testDirA = touch(testDirA);
323325
checkAndNotify();
324-
checkFileCounts("E", 0, 0, 1);
326+
checkCollectionSizes("E", 0, 1, 0, 0, 0, 1);
325327
assertFalse("E testDirAFile3 exists", testDirAFile3.exists());
326328
assertTrue("E testDirAFile3", listener.getDeletedFiles().contains(testDirAFile3));
327329

328330
// Delete last entry
329331
FileUtils.deleteQuietly(testDirAFile5);
332+
testDirA = touch(testDirA);
330333
checkAndNotify();
331-
checkFileCounts("F", 0, 0, 1);
334+
checkCollectionSizes("F", 0, 1, 0, 0, 0, 1);
332335
assertFalse("F testDirAFile5 exists", testDirAFile5.exists());
333336
assertTrue("F testDirAFile5", listener.getDeletedFiles().contains(testDirAFile5));
334337

@@ -360,8 +363,7 @@ public void testObserveSingleFile() {
360363
assertTrue("B testDirAFile2 exists", testDirAFile2.exists());
361364
assertTrue("B testDirAFile3 exists", testDirAFile3.exists());
362365
checkAndNotify();
363-
checkDirectoryCounts("C", 0, 0, 0);
364-
checkFileCounts("C", 1, 0, 0);
366+
checkCollectionSizes("C", 0, 0, 0, 1, 0, 0);
365367
assertTrue("C created", listener.getCreatedFiles().contains(testDirAFile1));
366368
assertFalse("C created", listener.getCreatedFiles().contains(testDirAFile2));
367369
assertFalse("C created", listener.getCreatedFiles().contains(testDirAFile3));
@@ -371,8 +373,7 @@ public void testObserveSingleFile() {
371373
testDirAFile2 = touch(testDirAFile2);
372374
testDirAFile3 = touch(testDirAFile3);
373375
checkAndNotify();
374-
checkDirectoryCounts("D", 0, 0, 0);
375-
checkFileCounts("D", 0, 1, 0);
376+
checkCollectionSizes("D", 0, 0, 0, 0, 1, 0);
376377
assertTrue("D changed", listener.getChangedFiles().contains(testDirAFile1));
377378
assertFalse("D changed", listener.getChangedFiles().contains(testDirAFile2));
378379
assertFalse("D changed", listener.getChangedFiles().contains(testDirAFile3));
@@ -385,8 +386,7 @@ public void testObserveSingleFile() {
385386
assertFalse("E testDirAFile2 exists", testDirAFile2.exists());
386387
assertFalse("E testDirAFile3 exists", testDirAFile3.exists());
387388
checkAndNotify();
388-
checkDirectoryCounts("E", 0, 0, 0);
389-
checkFileCounts("E", 0, 0, 1);
389+
checkCollectionSizes("E", 0, 0, 0, 0, 0, 1);
390390
assertTrue("E deleted", listener.getDeletedFiles().contains(testDirAFile1));
391391
assertFalse("E deleted", listener.getDeletedFiles().contains(testDirAFile2));
392392
assertFalse("E deleted", listener.getDeletedFiles().contains(testDirAFile3));
@@ -409,29 +409,22 @@ protected void checkAndNotify() throws Exception {
409409
* Check all the Collections are empty
410410
*/
411411
private void checkCollectionsEmpty(String label) {
412-
checkDirectoryCounts("EMPTY-" + label, 0, 0, 0);
413-
checkFileCounts("EMPTY-" + label, 0, 0, 0);
412+
checkCollectionSizes("EMPTY-" + label, 0, 0, 0, 0, 0, 0);
414413
}
415414

416415
/**
417-
* Check all the Directory Collections have the expected sizes.
416+
* Check all the Collections have the expected sizes.
418417
*/
419-
private void checkDirectoryCounts(String label, int dirCreate, int dirChange, int dirDelete) {
418+
private void checkCollectionSizes(String label, int dirCreate, int dirChange, int dirDelete, int fileCreate, int fileChange, int fileDelete) {
420419
label = label + "[" + listener.getCreatedDirectories().size() +
421420
" " + listener.getChangedDirectories().size() +
422-
" " + listener.getDeletedDirectories().size() + "]";
421+
" " + listener.getDeletedDirectories().size() +
422+
" " + listener.getCreatedFiles().size() +
423+
" " + listener.getChangedFiles().size() +
424+
" " + listener.getDeletedFiles().size() + "]";
423425
assertEquals(label + ": No. of directories created", dirCreate, listener.getCreatedDirectories().size());
424426
assertEquals(label + ": No. of directories changed", dirChange, listener.getChangedDirectories().size());
425427
assertEquals(label + ": No. of directories deleted", dirDelete, listener.getDeletedDirectories().size());
426-
}
427-
428-
/**
429-
* Check all the File Collections have the expected sizes.
430-
*/
431-
private void checkFileCounts(String label, int fileCreate, int fileChange, int fileDelete) {
432-
label = label + "[" + listener.getCreatedFiles().size() +
433-
" " + listener.getChangedFiles().size() +
434-
" " + listener.getDeletedFiles().size() + "]";
435428
assertEquals(label + ": No. of files created", fileCreate, listener.getCreatedFiles().size());
436429
assertEquals(label + ": No. of files changed", fileChange, listener.getChangedFiles().size());
437430
assertEquals(label + ": No. of files deleted", fileDelete, listener.getDeletedFiles().size());

0 commit comments

Comments
 (0)