2 * Copyright 2007 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 import org.apache.oro.io.AwkFilenameFilter;
23 public class DirectoryMonitorTest extends TestCase {
25 private static final String REGEX = "^.*\\.txt$";
26 private static final String FILE1 = "file1.txt";
28 private TestData _data;
29 private DirectoryMonitor.Listener _listener;
31 private DirectoryMonitor _monitor;
34 protected void setUp() throws Exception {
36 _data = new TestData(this);
38 _listener = mock(DirectoryMonitor.Listener.class);
39 _monitor = new DirectoryMonitor(_data.getRoot(), new AwkFilenameFilter(
43 public void testEmptyDir() {
44 // Nothing is expected to be called.
45 for (int i = 0; i < 10; i++) {
47 verifyNoMoreInteractions(_listener);
51 public void testFileCreated() {
52 _data.createFile(FILE1, "hello");
54 verify(_listener).fileCreated(_data.getFile(FILE1));
57 public void testFileDeleted() {
58 _data.createFile(FILE1, "hello");
62 _data.deleteFile(FILE1);
65 verify(_listener).fileDeleted(_data.getFile(FILE1));
66 verifyNoMoreInteractions(_listener);
69 public void testFileChanged() throws InterruptedException {
70 _data.createFile(FILE1, "hello");
75 _data.deleteFile(FILE1);
76 _data.createFile(FILE1, "bla");
79 verify(_listener).fileChanged(_data.getFile(FILE1));
80 verifyNoMoreInteractions(_listener);
83 public void testFileFilterIsUsed() {
86 _data.createFile("file.xml", "hello");
88 verifyNoMoreInteractions(_listener);
91 public void testDirectoryIsIgnored() {
93 _data.createDir(FILE1);
95 verifyNoMoreInteractions(_listener);
98 public void testExceptionsWIllLeadToRepeatedNotifications() {
100 _data.createFile(FILE1, "hello");
102 stubVoid(_listener).toThrow(new RuntimeException()).on().
103 fileCreated(_data.getFile(FILE1));
107 } catch (RuntimeException e) {
110 // polling again should lead to the same filecreated call.
111 // this time no exception is thrown.
114 verify(_listener).fileCreated(_data.getFile(FILE1));
115 verifyNoMoreInteractions(_listener);
118 fail(); // should not get here.