12db916e78a9ea8615b6c4e1b7cc8d4b8b6bbfb1
[utils] /
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 static org.mockito.Mockito.*;
19 import junit.framework.TestCase;
20
21 import org.apache.oro.io.AwkFilenameFilter;
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
31         private DirectoryMonitor _monitor;
32
33         @Override
34         protected void setUp() throws Exception {
35                 super.setUp();
36                 _data = new TestData(this);
37                 _data.clean();
38                 _listener = mock(DirectoryMonitor.Listener.class);
39                 _monitor = new DirectoryMonitor(_data.getRoot(), new AwkFilenameFilter(
40                                 REGEX), _listener);
41         }
42
43         public void testEmptyDir() {
44                 // Nothing is expected to be called.
45                 for (int i = 0; i < 10; i++) {
46                         _monitor.poll();
47                         verifyNoMoreInteractions(_listener);
48                 }
49         }
50
51         public void testFileCreated() {
52                 _data.createFile(FILE1, "hello");
53                 _monitor.poll();
54                 verify(_listener).fileCreated(_data.getFile(FILE1));
55         }
56
57         public void testFileDeleted() {
58                 _data.createFile(FILE1, "hello");
59                 _monitor.poll();
60                 reset(_listener);
61
62                 _data.deleteFile(FILE1);
63                 _monitor.poll();
64
65                 verify(_listener).fileDeleted(_data.getFile(FILE1));
66                 verifyNoMoreInteractions(_listener);
67         }
68
69         public void testFileChanged() throws InterruptedException {
70                 _data.createFile(FILE1, "hello");
71                 _monitor.poll();
72                 reset(_listener);
73
74                 Thread.sleep(2000);
75                 _data.deleteFile(FILE1);
76                 _data.createFile(FILE1, "bla");
77
78                 _monitor.poll();
79                 verify(_listener).fileChanged(_data.getFile(FILE1));
80                 verifyNoMoreInteractions(_listener);
81         }
82
83         public void testFileFilterIsUsed() {
84                 _monitor.poll();
85
86                 _data.createFile("file.xml", "hello");
87                 _monitor.poll();
88                 verifyNoMoreInteractions(_listener);
89         }
90
91         public void testDirectoryIsIgnored() {
92                 _monitor.poll();
93                 _data.createDir(FILE1);
94                 _monitor.poll();
95                 verifyNoMoreInteractions(_listener);
96         }
97
98         public void testExceptionsWIllLeadToRepeatedNotifications() { 
99                 _monitor.poll();
100                 _data.createFile(FILE1, "hello");
101                 
102                 stubVoid(_listener).toThrow(new RuntimeException()).on().
103                  fileCreated(_data.getFile(FILE1));
104                 
105                 try { 
106                     _monitor.poll();
107                 } catch (RuntimeException e) { 
108                         reset(_listener); 
109                         
110                         // polling again should lead to the same filecreated call.
111                         // this time no exception is thrown.
112                         
113                         _monitor.poll();
114                         verify(_listener).fileCreated(_data.getFile(FILE1));
115                         verifyNoMoreInteractions(_listener);
116                         return; 
117                 }
118                 fail(); // should not get here.
119                 
120         
121         }
122 }