835c696305fcd35825c03b70b3b55a412c34077f
[utils] / support / general / src / test / java / org / wamblee / io / DirectoryMonitorTest.java
1 /*
2  * Copyright 2007 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.io;
17
18 import org.apache.oro.io.AwkFilenameFilter;
19 import org.easymock.EasyMock;
20
21 import junit.framework.TestCase;
22
23 public class DirectoryMonitorTest extends TestCase {
24
25         private static final String REGEX = "^.*\\.txt$";
26         private static final String FILE1 = "file1.txt";
27
28         private TestData _data;
29         private DirectoryMonitor.Listener _listener;
30         private DirectoryMonitor _monitor;
31
32         @Override
33         protected void setUp() throws Exception {
34                 super.setUp();
35                 _data = new TestData(this);
36                 _data.clean();
37                 _listener = EasyMock.createStrictMock(DirectoryMonitor.Listener.class);
38                 _monitor = new DirectoryMonitor(_data.getRoot(), new AwkFilenameFilter(
39                                 REGEX), _listener);
40         }
41
42         public void testEmptyDir() {
43                 // Nothing is expected to be called.
44                 for (int i = 0; i < 10; i++) {
45                         EasyMock.replay(_listener);
46                         _monitor.poll();
47                         EasyMock.verify(_listener);
48                         EasyMock.reset(_listener);
49                 }
50         }
51
52         public void testFileCreated() {
53                 _listener.fileCreated(_data.getFile(FILE1));
54                 EasyMock.replay(_listener);
55                 _data.createFile(FILE1, "hello");
56                 _monitor.poll();
57                 EasyMock.verify(_listener);
58         }
59
60         public void testFileDeleted() {
61                 _data.createFile(FILE1, "hello");
62                 _monitor.poll();
63
64                 EasyMock.reset(_listener);
65
66                 _data.deleteFile(FILE1);
67                 _listener.fileDeleted(_data.getFile(FILE1));
68                 EasyMock.replay(_listener);
69                 _monitor.poll();
70                 EasyMock.verify(_listener);
71         }
72         
73         public void testFileChanged() throws InterruptedException { 
74                 _data.createFile(FILE1, "hello");
75                 _monitor.poll();
76                 EasyMock.reset(_listener);
77                 
78                 Thread.sleep(2000);
79                 _data.deleteFile(FILE1);
80                 _data.createFile(FILE1, "bla");
81                 
82                 _listener.fileChanged(_data.getFile(FILE1));
83                 EasyMock.replay(_listener);
84                 _monitor.poll();
85                 EasyMock.verify(_listener);
86         }
87         
88         public void testFileFilterIsUsed() {
89                 _monitor.poll(); 
90                 
91                 _data.createFile("file.xml", "hello");
92                 EasyMock.replay(_listener); 
93                 _monitor.poll();
94                 EasyMock.verify(_listener);
95         }
96         
97         public void testDirectoryIsIgnored() { 
98                 _monitor.poll();
99                 _data.createDir(FILE1);
100                 EasyMock.replay(_listener); 
101                 _monitor.poll();
102                 EasyMock.verify(_listener);
103         }
104         
105         public void testExceptionsWIllLeadToRepeatedNotifications() { 
106                 _monitor.poll();
107                 _data.createFile(FILE1, "hello");
108                 
109                 _listener.fileCreated(_data.getFile(FILE1));
110                 EasyMock.expectLastCall().andThrow(new RuntimeException());
111                 EasyMock.replay(_listener);
112                 try { 
113                     _monitor.poll();
114                 } catch (RuntimeException e) { 
115                         EasyMock.verify(_listener);
116                         EasyMock.reset(_listener);
117                         
118                         // polling again should lead to the same filecreated call. 
119                         // this time no exception is thrown. 
120                         
121                         _listener.fileCreated(_data.getFile(FILE1));
122                         EasyMock.replay(_listener);
123                         _monitor.poll();
124                         EasyMock.verify(_listener);
125                         return; 
126                 }
127                 fail(); // should not get here. 
128                 
129         
130         }
131 }