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 junit.framework.TestCase;
20 import org.apache.oro.io.AwkFilenameFilter;
21 import static org.mockito.Mockito.*;
22 public class DirectoryMonitorTest extends TestCase {
23 private static final String REGEX = "^.*\\.txt$";
24 private static final String FILE1 = "file1.txt";
25 private TestData data;
26 private DirectoryMonitor.Listener listener;
27 private DirectoryMonitor monitor;
30 protected void setUp() throws Exception {
32 data = new TestData(this);
34 listener = mock(DirectoryMonitor.Listener.class);
35 monitor = new DirectoryMonitor(data.getRoot(),
36 new AwkFilenameFilter(REGEX), listener);
39 public void testEmptyDir() {
40 // Nothing is expected to be called.
41 for (int i = 0; i < 10; i++) {
43 verifyNoMoreInteractions(listener);
47 public void testFileCreated() {
48 data.createFile(FILE1, "hello");
50 verify(listener).fileCreated(data.getFile(FILE1));
53 public void testFileDeleted() {
54 data.createFile(FILE1, "hello");
58 data.deleteFile(FILE1);
61 verify(listener).fileDeleted(data.getFile(FILE1));
62 verifyNoMoreInteractions(listener);
65 public void testFileChanged() throws InterruptedException {
66 data.createFile(FILE1, "hello");
71 data.deleteFile(FILE1);
72 data.createFile(FILE1, "bla");
75 verify(listener).fileChanged(data.getFile(FILE1));
76 verifyNoMoreInteractions(listener);
79 public void testFileFilterIsUsed() {
82 data.createFile("file.xml", "hello");
84 verifyNoMoreInteractions(listener);
87 public void testDirectoryIsIgnored() {
89 data.createDir(FILE1);
91 verifyNoMoreInteractions(listener);
94 public void testExceptionsWIllLeadToRepeatedNotifications() {
96 data.createFile(FILE1, "hello");
98 stubVoid(listener).toThrow(new RuntimeException()).on()
99 .fileCreated(data.getFile(FILE1));
103 } catch (RuntimeException e) {
106 // polling again should lead to the same filecreated call.
107 // this time no exception is thrown.
109 verify(listener).fileCreated(data.getFile(FILE1));
110 verifyNoMoreInteractions(listener);
115 fail(); // should not get here.