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