Skip to content

Commit

Permalink
fixed an error in the directory watcher
Browse files Browse the repository at this point in the history
	modified:   src/main/java/example/BundleRegistry.java
	modified:   src/main/java/example/DirectoryWatcher.java
	modified:   src/test/java/example/BundleRegistryTest.java
	modified:   src/test/java/example/DirectoryWatcherTest.java
  • Loading branch information
caspian311 committed Aug 6, 2009
1 parent 391d3e3 commit e1724dc
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/main/java/example/BundleRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void uninstallBundle(File file) throws BundleRegistryException {
}

try {
bundle.stop();
bundle.uninstall();
} catch (BundleException e) {
throw new BundleRegistryException(e);
Expand Down
30 changes: 10 additions & 20 deletions src/main/java/example/DirectoryWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,20 @@ private void findDirectoryDifferences(Map<String, Long> previousLastModifiedMap,

for (int i = 0; i < previousFileList.size(); i++) {
String previousPath = previousFileList.get(i);
if (currentFileList.size() > i) {
String currentPath = currentFileList.get(i);
if (previousPath.equals(currentPath)) {
if (previousLastModifiedMap.get(previousPath).equals(
currentLastModifiedMap.get(currentPath))) {
continue;
} else {
changedDifferences.add(new File(currentPath));
}
} else {
if (currentFileList.contains(previousPath)) {
addedDifferences.add(new File(currentPath));
} else {
deletedDifferences.add(new File(previousPath));
}
}
} else {
if (!currentFileList.contains(previousPath)) {
deletedDifferences.add(new File(previousPath));
} else {
if (!previousLastModifiedMap.get(previousPath).equals(
currentLastModifiedMap.get(previousPath))) {
changedDifferences.add(new File(previousPath));
}
}
}

if (currentFileList.size() > previousFileList.size()) {
for (int i = previousFileList.size(); i < currentFileList.size(); i++) {
addedDifferences.add(new File(currentFileList.get(i)));
for (int i = 0; i < currentFileList.size(); i++) {
String currentPath = currentFileList.get(i);
if (!previousFileList.contains(currentPath)) {
addedDifferences.add(new File(currentPath));
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/example/BundleRegistryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public void bundleRegistryCanUninstallPreviouslyInstalledBundle() throws Excepti
expect(bundleContext.installBundle(eq(fileLocation), (FileInputStream) anyObject()))
.andReturn(bundle);
bundle.start();
bundle.stop();
bundle.uninstall();
replay(bundleContext, bundle);

Expand All @@ -129,6 +130,7 @@ public void ifBundleUninstallThrowsExceptionWrapItAndThrowItAgain() throws Excep
expect(bundleContext.installBundle(eq(fileLocation), (FileInputStream) anyObject()))
.andReturn(bundle);
bundle.start();
bundle.stop();
bundle.uninstall();
String errorMessage = UUID.randomUUID().toString();
expectLastCall().andThrow(new BundleException(errorMessage));
Expand Down
64 changes: 64 additions & 0 deletions src/test/java/example/DirectoryWatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,70 @@ public void whatChangedIsCorrectForFileDeletions() throws Exception {
}
}

@Test
public void whatEventsAreFiredCorrectlyForFileDeletionsWhenThereAreMultipleFiles()
throws Exception {
File tempDir = File.createTempFile(getClass().getName(), null);
tempDir.delete();
try {
tempDir.mkdir();

File file1 = new File(tempDir, "file1");
String file1Path = file1.getAbsolutePath();
FileOutputStream fos = null;
PrintWriter out = null;
try {
fos = new FileOutputStream(file1);
out = new PrintWriter(fos);

out.println("hello world");
out.flush();
} finally {
out.close();
fos.close();
}

File file2 = new File(tempDir, "file2");
try {
fos = new FileOutputStream(file2);
out = new PrintWriter(fos);

out.println("hello world");
out.flush();
} finally {
out.close();
fos.close();
}

DirectoryWatcher watcher = new DirectoryWatcher(tempDir);
MockChangeFileListener fileChangedListener = new MockChangeFileListener(watcher);
MockAddFileListener fileAddedListener = new MockAddFileListener(watcher);
MockDeleteFileListener fileDeletedListener = new MockDeleteFileListener(watcher);
watcher.addFileChangedListener(fileChangedListener);
watcher.addFileChangedListener(fileAddedListener);
watcher.addFileDeletedListener(fileDeletedListener);

watcher.checkForChanges();

assertNull(fileChangedListener.modifiedFiles);
assertNull(fileAddedListener.addedFiles);
assertNull(fileDeletedListener.deletedFiles);

File fileToDelete = new File(file1Path);
FileUtils.forceDelete(fileToDelete);

watcher.checkForChanges();

assertNull(fileChangedListener.modifiedFiles);
assertNull(fileAddedListener.addedFiles);
assertNotNull(fileDeletedListener.deletedFiles);
assertEquals(1, fileDeletedListener.deletedFiles.size());
assertEquals(file1Path, fileDeletedListener.deletedFiles.get(0).getAbsolutePath());
} finally {
FileUtils.deleteDirectory(tempDir);
}
}

private static class MockAddFileListener implements IListener {
private final DirectoryWatcher watcher;
private List<File> addedFiles;
Expand Down

0 comments on commit e1724dc

Please sign in to comment.