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