1 package org.wamblee.io;
3 import org.apache.oro.io.AwkFilenameFilter;
4 import org.easymock.EasyMock;
6 import junit.framework.TestCase;
8 public class DirectoryMonitorTest extends TestCase {
10 private static final String REGEX = "^.*\\.txt$";
11 private static final String FILE1 = "file1.txt";
13 private TestData _data;
14 private DirectoryMonitor.Listener _listener;
15 private DirectoryMonitor _monitor;
18 protected void setUp() throws Exception {
20 _data = new TestData(this);
22 _listener = EasyMock.createStrictMock(DirectoryMonitor.Listener.class);
23 _monitor = new DirectoryMonitor(_data.getRoot(), new AwkFilenameFilter(
27 public void testEmptyDir() {
28 // Nothing is expected to be called.
29 for (int i = 0; i < 10; i++) {
30 EasyMock.replay(_listener);
32 EasyMock.verify(_listener);
33 EasyMock.reset(_listener);
37 public void testFileCreated() {
38 _listener.fileCreated(_data.getFile(FILE1));
39 EasyMock.replay(_listener);
40 _data.createFile(FILE1, "hello");
42 EasyMock.verify(_listener);
45 public void testFileDeleted() {
46 _data.createFile(FILE1, "hello");
49 EasyMock.reset(_listener);
51 _data.deleteFile(FILE1);
52 _listener.fileDeleted(_data.getFile(FILE1));
53 EasyMock.replay(_listener);
55 EasyMock.verify(_listener);
58 public void testFileChanged() throws InterruptedException {
59 _data.createFile(FILE1, "hello");
61 EasyMock.reset(_listener);
64 _data.deleteFile(FILE1);
65 _data.createFile(FILE1, "bla");
67 _listener.fileChanged(_data.getFile(FILE1));
68 EasyMock.replay(_listener);
70 EasyMock.verify(_listener);
73 public void testFileFilterIsUsed() {
76 _data.createFile("file.xml", "hello");
77 EasyMock.replay(_listener);
79 EasyMock.verify(_listener);
82 public void testDirectoryIsIgnored() {
84 _data.createDir(FILE1);
85 EasyMock.replay(_listener);
87 EasyMock.verify(_listener);
90 public void testExceptionsWIllLeadToRepeatedNotifications() {
92 _data.createFile(FILE1, "hello");
94 _listener.fileCreated(_data.getFile(FILE1));
95 EasyMock.expectLastCall().andThrow(new RuntimeException());
96 EasyMock.replay(_listener);
99 } catch (RuntimeException e) {
100 EasyMock.verify(_listener);
101 EasyMock.reset(_listener);
103 // polling again should lead to the same filecreated call.
104 // this time no exception is thrown.
106 _listener.fileCreated(_data.getFile(FILE1));
107 EasyMock.replay(_listener);
109 EasyMock.verify(_listener);
112 fail(); // should not get here.