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;
22 import org.wamblee.test.ResettableMock;
24 public class DirectoryMonitorTest extends TestCase {
26 private static final String REGEX = "^.*\\.txt$";
27 private static final String FILE1 = "file1.txt";
29 private TestData _data;
30 private ResettableMock<DirectoryMonitor.Listener> _listener;
32 private DirectoryMonitor _monitor;
35 protected void setUp() throws Exception {
37 _data = new TestData(this);
39 _listener = new ResettableMock<DirectoryMonitor.Listener>(
40 DirectoryMonitor.Listener.class);
41 _monitor = new DirectoryMonitor(_data.getRoot(), new AwkFilenameFilter(
42 REGEX), _listener.getProxy());
45 public void testEmptyDir() {
46 // Nothing is expected to be called.
47 for (int i = 0; i < 10; i++) {
49 verifyNoMoreInteractions(_listener.getMock());
53 public void testFileCreated() {
54 _data.createFile(FILE1, "hello");
56 verify(_listener.getMock()).fileCreated(_data.getFile(FILE1));
59 public void testFileDeleted() {
60 _data.createFile(FILE1, "hello");
64 _data.deleteFile(FILE1);
67 verify(_listener.getMock()).fileDeleted(_data.getFile(FILE1));
68 verifyNoMoreInteractions(_listener.getMock());
71 public void testFileChanged() throws InterruptedException {
72 _data.createFile(FILE1, "hello");
77 _data.deleteFile(FILE1);
78 _data.createFile(FILE1, "bla");
81 verify(_listener.getMock()).fileChanged(_data.getFile(FILE1));
82 verifyNoMoreInteractions(_listener.getMock());
85 public void testFileFilterIsUsed() {
88 _data.createFile("file.xml", "hello");
90 verifyNoMoreInteractions(_listener.getMock());
93 public void testDirectoryIsIgnored() {
95 _data.createDir(FILE1);
97 verifyNoMoreInteractions(_listener.getMock());
100 public void testExceptionsWIllLeadToRepeatedNotifications() {
102 _data.createFile(FILE1, "hello");
104 stubVoid(_listener.getMock()).toThrow(new RuntimeException()).on().
105 fileCreated(_data.getFile(FILE1));
109 } catch (RuntimeException e) {
112 // polling again should lead to the same filecreated call.
113 // this time no exception is thrown.
116 verify(_listener.getMock()).fileCreated(_data.getFile(FILE1));
117 verifyNoMoreInteractions(_listener.getMock());
120 fail(); // should not get here.