2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.io;
18 import static org.mockito.Mockito.*;
19 import junit.framework.TestCase;
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;
29 protected void setUp() throws Exception {
31 data = new TestData(this);
33 listener = mock(DirectoryMonitor.Listener.class);
34 monitor = new DirectoryMonitor(data.getRoot(), new RegexFilenameFilter(
38 public void testEmptyDir() {
39 // Nothing is expected to be called.
40 for (int i = 0; i < 10; i++) {
42 verifyNoMoreInteractions(listener);
46 public void testFileCreated() {
47 data.createFile(FILE1, "hello");
49 verify(listener).fileCreated(data.getFile(FILE1));
52 public void testFileDeleted() {
53 data.createFile(FILE1, "hello");
57 data.deleteFile(FILE1);
60 verify(listener).fileDeleted(data.getFile(FILE1));
61 verifyNoMoreInteractions(listener);
64 public void testFileChanged() throws InterruptedException {
65 data.createFile(FILE1, "hello");
70 data.deleteFile(FILE1);
71 data.createFile(FILE1, "bla");
74 verify(listener).fileChanged(data.getFile(FILE1));
75 verifyNoMoreInteractions(listener);
78 public void testFileModificationTimeChanged() throws InterruptedException {
79 data.createFile(FILE1, "hello");
82 data.deleteFile(FILE1);
84 data.createFile(FILE1, "hello");
87 verify(listener).fileChanged(data.getFile(FILE1));
88 verifyNoMoreInteractions(listener);
91 public void testFileFilterIsUsed() {
94 data.createFile("file.xml", "hello");
96 verifyNoMoreInteractions(listener);
99 public void testDirectoryIsIgnored() {
101 data.createDir(FILE1);
103 verifyNoMoreInteractions(listener);
106 public void testExceptionsWIllLeadToRepeatedNotifications() {
108 data.createFile(FILE1, "hello");
110 stubVoid(listener).toThrow(new RuntimeException()).on()
111 .fileCreated(data.getFile(FILE1));
115 } catch (RuntimeException e) {
118 // polling again should lead to the same filecreated call.
119 // this time no exception is thrown.
121 verify(listener).fileCreated(data.getFile(FILE1));
122 verifyNoMoreInteractions(listener);
127 fail(); // should not get here.