(no commit message)
[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 testFileFilterIsUsed() {
79         monitor.poll();
80
81         data.createFile("file.xml", "hello");
82         monitor.poll();
83         verifyNoMoreInteractions(listener);
84     }
85
86     public void testDirectoryIsIgnored() {
87         monitor.poll();
88         data.createDir(FILE1);
89         monitor.poll();
90         verifyNoMoreInteractions(listener);
91     }
92
93     public void testExceptionsWIllLeadToRepeatedNotifications() {
94         monitor.poll();
95         data.createFile(FILE1, "hello");
96
97         stubVoid(listener).toThrow(new RuntimeException()).on().fileCreated(
98             data.getFile(FILE1));
99
100         try {
101             monitor.poll();
102         } catch (RuntimeException e) {
103             reset(listener);
104
105             // polling again should lead to the same filecreated call.
106             // this time no exception is thrown.
107             monitor.poll();
108             verify(listener).fileCreated(data.getFile(FILE1));
109             verifyNoMoreInteractions(listener);
110
111             return;
112         }
113
114         fail(); // should not get here.
115     }
116 }