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