(no commit message)
[utils] / support / general / src / test / java / org / wamblee / io / DirectoryMonitorTest.java
1 package org.wamblee.io;
2
3 import org.apache.oro.io.AwkFilenameFilter;
4 import org.easymock.EasyMock;
5
6 import junit.framework.TestCase;
7
8 public class DirectoryMonitorTest extends TestCase {
9
10         private static final String REGEX = "^.*\\.txt$";
11         private static final String FILE1 = "file1.txt";
12
13         private TestData _data;
14         private DirectoryMonitor.Listener _listener;
15         private DirectoryMonitor _monitor;
16
17         @Override
18         protected void setUp() throws Exception {
19                 super.setUp();
20                 _data = new TestData(this);
21                 _data.clean();
22                 _listener = EasyMock.createStrictMock(DirectoryMonitor.Listener.class);
23                 _monitor = new DirectoryMonitor(_data.getRoot(), new AwkFilenameFilter(
24                                 REGEX), _listener);
25         }
26
27         public void testEmptyDir() {
28                 // Nothing is expected to be called.
29                 for (int i = 0; i < 10; i++) {
30                         EasyMock.replay(_listener);
31                         _monitor.poll();
32                         EasyMock.verify(_listener);
33                         EasyMock.reset(_listener);
34                 }
35         }
36
37         public void testFileCreated() {
38                 _listener.fileCreated(_data.getFile(FILE1));
39                 EasyMock.replay(_listener);
40                 _data.createFile(FILE1, "hello");
41                 _monitor.poll();
42                 EasyMock.verify(_listener);
43         }
44
45         public void testFileDeleted() {
46                 _data.createFile(FILE1, "hello");
47                 _monitor.poll();
48
49                 EasyMock.reset(_listener);
50
51                 _data.deleteFile(FILE1);
52                 _listener.fileDeleted(_data.getFile(FILE1));
53                 EasyMock.replay(_listener);
54                 _monitor.poll();
55                 EasyMock.verify(_listener);
56         }
57         
58         public void testFileChanged() throws InterruptedException { 
59                 _data.createFile(FILE1, "hello");
60                 _monitor.poll();
61                 EasyMock.reset(_listener);
62                 
63                 Thread.sleep(2000);
64                 _data.deleteFile(FILE1);
65                 _data.createFile(FILE1, "bla");
66                 
67                 _listener.fileChanged(_data.getFile(FILE1));
68                 EasyMock.replay(_listener);
69                 _monitor.poll();
70                 EasyMock.verify(_listener);
71         }
72         
73         public void testFileFilterIsUsed() {
74                 _monitor.poll(); 
75                 
76                 _data.createFile("file.xml", "hello");
77                 EasyMock.replay(_listener); 
78                 _monitor.poll();
79                 EasyMock.verify(_listener);
80         }
81         
82         public void testDirectoryIsIgnored() { 
83                 _monitor.poll();
84                 _data.createDir(FILE1);
85                 EasyMock.replay(_listener); 
86                 _monitor.poll();
87                 EasyMock.verify(_listener);
88         }
89         
90         public void testExceptionsWIllLeadToRepeatedNotifications() { 
91                 _monitor.poll();
92                 _data.createFile(FILE1, "hello");
93                 
94                 _listener.fileCreated(_data.getFile(FILE1));
95                 EasyMock.expectLastCall().andThrow(new RuntimeException());
96                 EasyMock.replay(_listener);
97                 try { 
98                     _monitor.poll();
99                 } catch (RuntimeException e) { 
100                         EasyMock.verify(_listener);
101                         EasyMock.reset(_listener);
102                         
103                         // polling again should lead to the same filecreated call. 
104                         // this time no exception is thrown. 
105                         
106                         _listener.fileCreated(_data.getFile(FILE1));
107                         EasyMock.replay(_listener);
108                         _monitor.poll();
109                         EasyMock.verify(_listener);
110                         return; 
111                 }
112                 fail(); // should not get here. 
113                 
114         
115         }
116 }