e0048c08ad22679e9e3548f90795d62c74ee1f65
[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 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 }