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 junit.framework.TestCase;
20 import org.apache.oro.io.AwkFilenameFilter;
21 import static org.mockito.Mockito.*;
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;
31 protected void setUp() throws Exception {
33 data = new TestData(this);
35 listener = mock(DirectoryMonitor.Listener.class);
36 monitor = new DirectoryMonitor(data.getRoot(), new AwkFilenameFilter(
40 public void testEmptyDir() {
41 // Nothing is expected to be called.
42 for (int i = 0; i < 10; i++) {
44 verifyNoMoreInteractions(listener);
48 public void testFileCreated() {
49 data.createFile(FILE1, "hello");
51 verify(listener).fileCreated(data.getFile(FILE1));
54 public void testFileDeleted() {
55 data.createFile(FILE1, "hello");
59 data.deleteFile(FILE1);
62 verify(listener).fileDeleted(data.getFile(FILE1));
63 verifyNoMoreInteractions(listener);
66 public void testFileChanged() throws InterruptedException {
67 data.createFile(FILE1, "hello");
72 data.deleteFile(FILE1);
73 data.createFile(FILE1, "bla");
76 verify(listener).fileChanged(data.getFile(FILE1));
77 verifyNoMoreInteractions(listener);
80 public void testFileFilterIsUsed() {
83 data.createFile("file.xml", "hello");
85 verifyNoMoreInteractions(listener);
88 public void testDirectoryIsIgnored() {
90 data.createDir(FILE1);
92 verifyNoMoreInteractions(listener);
95 public void testExceptionsWIllLeadToRepeatedNotifications() {
97 data.createFile(FILE1, "hello");
99 stubVoid(listener).toThrow(new RuntimeException()).on().fileCreated(
100 data.getFile(FILE1));
104 } catch (RuntimeException e) {
107 // polling again should lead to the same filecreated call.
108 // this time no exception is thrown.
110 verify(listener).fileCreated(data.getFile(FILE1));
111 verifyNoMoreInteractions(listener);
116 fail(); // should not get here.