X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fio%2FDirectoryMonitorTest.java;h=ac5fbf707ca56a325e9ee62113a8089c74542c32;hb=ddd261f331280640c5b53c7128230b629ebcd268;hp=e0048c08ad22679e9e3548f90795d62c74ee1f65;hpb=92e23e5ecf9614f2ab770a8cdedc0b21ddf1e127;p=utils diff --git a/support/general/src/test/java/org/wamblee/io/DirectoryMonitorTest.java b/support/general/src/test/java/org/wamblee/io/DirectoryMonitorTest.java index e0048c08..ac5fbf70 100644 --- a/support/general/src/test/java/org/wamblee/io/DirectoryMonitorTest.java +++ b/support/general/src/test/java/org/wamblee/io/DirectoryMonitorTest.java @@ -1,12 +1,12 @@ /* * Copyright 2007 the original author or authors. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,108 +15,103 @@ */ package org.wamblee.io; -import static org.mockito.Mockito.*; import junit.framework.TestCase; import org.apache.oro.io.AwkFilenameFilter; - +import static org.mockito.Mockito.*; public class DirectoryMonitorTest extends TestCase { - - private static final String REGEX = "^.*\\.txt$"; - private static final String FILE1 = "file1.txt"; - - private TestData data; - private DirectoryMonitor.Listener listener; - - private DirectoryMonitor monitor; - - @Override - protected void setUp() throws Exception { - super.setUp(); - data = new TestData(this); - data.clean(); - listener = mock(DirectoryMonitor.Listener.class); - monitor = new DirectoryMonitor(data.getRoot(), new AwkFilenameFilter( - REGEX), listener); - } - - public void testEmptyDir() { - // Nothing is expected to be called. - for (int i = 0; i < 10; i++) { - monitor.poll(); - verifyNoMoreInteractions(listener); - } - } - - public void testFileCreated() { - data.createFile(FILE1, "hello"); - monitor.poll(); - verify(listener).fileCreated(data.getFile(FILE1)); - } - - public void testFileDeleted() { - data.createFile(FILE1, "hello"); - monitor.poll(); - reset(listener); - - data.deleteFile(FILE1); - monitor.poll(); - - verify(listener).fileDeleted(data.getFile(FILE1)); - verifyNoMoreInteractions(listener); - } - - public void testFileChanged() throws InterruptedException { - data.createFile(FILE1, "hello"); - monitor.poll(); - reset(listener); - - Thread.sleep(2000); - data.deleteFile(FILE1); - data.createFile(FILE1, "bla"); - - monitor.poll(); - verify(listener).fileChanged(data.getFile(FILE1)); - verifyNoMoreInteractions(listener); - } - - public void testFileFilterIsUsed() { - monitor.poll(); - - data.createFile("file.xml", "hello"); - monitor.poll(); - verifyNoMoreInteractions(listener); - } - - public void testDirectoryIsIgnored() { - monitor.poll(); - data.createDir(FILE1); - monitor.poll(); - verifyNoMoreInteractions(listener); - } - - public void testExceptionsWIllLeadToRepeatedNotifications() { - monitor.poll(); - data.createFile(FILE1, "hello"); - - stubVoid(listener).toThrow(new RuntimeException()).on(). - fileCreated(data.getFile(FILE1)); - - try { - monitor.poll(); - } catch (RuntimeException e) { - reset(listener); - - // polling again should lead to the same filecreated call. - // this time no exception is thrown. - - monitor.poll(); - verify(listener).fileCreated(data.getFile(FILE1)); - verifyNoMoreInteractions(listener); - return; - } - fail(); // should not get here. - - - } + private static final String REGEX = "^.*\\.txt$"; + private static final String FILE1 = "file1.txt"; + private TestData data; + private DirectoryMonitor.Listener listener; + private DirectoryMonitor monitor; + + @Override + protected void setUp() throws Exception { + super.setUp(); + data = new TestData(this); + data.clean(); + listener = mock(DirectoryMonitor.Listener.class); + monitor = new DirectoryMonitor(data.getRoot(), + new AwkFilenameFilter(REGEX), listener); + } + + public void testEmptyDir() { + // Nothing is expected to be called. + for (int i = 0; i < 10; i++) { + monitor.poll(); + verifyNoMoreInteractions(listener); + } + } + + public void testFileCreated() { + data.createFile(FILE1, "hello"); + monitor.poll(); + verify(listener).fileCreated(data.getFile(FILE1)); + } + + public void testFileDeleted() { + data.createFile(FILE1, "hello"); + monitor.poll(); + reset(listener); + + data.deleteFile(FILE1); + monitor.poll(); + + verify(listener).fileDeleted(data.getFile(FILE1)); + verifyNoMoreInteractions(listener); + } + + public void testFileChanged() throws InterruptedException { + data.createFile(FILE1, "hello"); + monitor.poll(); + reset(listener); + + Thread.sleep(2000); + data.deleteFile(FILE1); + data.createFile(FILE1, "bla"); + + monitor.poll(); + verify(listener).fileChanged(data.getFile(FILE1)); + verifyNoMoreInteractions(listener); + } + + public void testFileFilterIsUsed() { + monitor.poll(); + + data.createFile("file.xml", "hello"); + monitor.poll(); + verifyNoMoreInteractions(listener); + } + + public void testDirectoryIsIgnored() { + monitor.poll(); + data.createDir(FILE1); + monitor.poll(); + verifyNoMoreInteractions(listener); + } + + public void testExceptionsWIllLeadToRepeatedNotifications() { + monitor.poll(); + data.createFile(FILE1, "hello"); + + stubVoid(listener).toThrow(new RuntimeException()).on() + .fileCreated(data.getFile(FILE1)); + + try { + monitor.poll(); + } catch (RuntimeException e) { + reset(listener); + + // polling again should lead to the same filecreated call. + // this time no exception is thrown. + monitor.poll(); + verify(listener).fileCreated(data.getFile(FILE1)); + verifyNoMoreInteractions(listener); + + return; + } + + fail(); // should not get here. + } }