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