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 org.apache.oro.io.AwkFilenameFilter;
19 import org.easymock.EasyMock;
21 import junit.framework.TestCase;
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;
30 private DirectoryMonitor _monitor;
33 protected void setUp() throws Exception {
35 _data = new TestData(this);
37 _listener = EasyMock.createStrictMock(DirectoryMonitor.Listener.class);
38 _monitor = new DirectoryMonitor(_data.getRoot(), new AwkFilenameFilter(
42 public void testEmptyDir() {
43 // Nothing is expected to be called.
44 for (int i = 0; i < 10; i++) {
45 EasyMock.replay(_listener);
47 EasyMock.verify(_listener);
48 EasyMock.reset(_listener);
52 public void testFileCreated() {
53 _listener.fileCreated(_data.getFile(FILE1));
54 EasyMock.replay(_listener);
55 _data.createFile(FILE1, "hello");
57 EasyMock.verify(_listener);
60 public void testFileDeleted() {
61 _data.createFile(FILE1, "hello");
64 EasyMock.reset(_listener);
66 _data.deleteFile(FILE1);
67 _listener.fileDeleted(_data.getFile(FILE1));
68 EasyMock.replay(_listener);
70 EasyMock.verify(_listener);
73 public void testFileChanged() throws InterruptedException {
74 _data.createFile(FILE1, "hello");
76 EasyMock.reset(_listener);
79 _data.deleteFile(FILE1);
80 _data.createFile(FILE1, "bla");
82 _listener.fileChanged(_data.getFile(FILE1));
83 EasyMock.replay(_listener);
85 EasyMock.verify(_listener);
88 public void testFileFilterIsUsed() {
91 _data.createFile("file.xml", "hello");
92 EasyMock.replay(_listener);
94 EasyMock.verify(_listener);
97 public void testDirectoryIsIgnored() {
99 _data.createDir(FILE1);
100 EasyMock.replay(_listener);
102 EasyMock.verify(_listener);
105 public void testExceptionsWIllLeadToRepeatedNotifications() {
107 _data.createFile(FILE1, "hello");
109 _listener.fileCreated(_data.getFile(FILE1));
110 EasyMock.expectLastCall().andThrow(new RuntimeException());
111 EasyMock.replay(_listener);
114 } catch (RuntimeException e) {
115 EasyMock.verify(_listener);
116 EasyMock.reset(_listener);
118 // polling again should lead to the same filecreated call.
119 // this time no exception is thrown.
121 _listener.fileCreated(_data.getFile(FILE1));
122 EasyMock.replay(_listener);
124 EasyMock.verify(_listener);
127 fail(); // should not get here.