X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fio%2FDirectoryMonitorTest.java;h=e0048c08ad22679e9e3548f90795d62c74ee1f65;hb=0d8d8f24656e585ee75558cfd6a4c661f8f14985;hp=835c696305fcd35825c03b70b3b55a412c34077f;hpb=6f8bb575523e672b9f8797e543f7c59d15db7253;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 835c6963..e0048c08 100644 --- a/support/general/src/test/java/org/wamblee/io/DirectoryMonitorTest.java +++ b/support/general/src/test/java/org/wamblee/io/DirectoryMonitorTest.java @@ -12,119 +12,110 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - */ + */ package org.wamblee.io; -import org.apache.oro.io.AwkFilenameFilter; -import org.easymock.EasyMock; - +import static org.mockito.Mockito.*; import junit.framework.TestCase; +import org.apache.oro.io.AwkFilenameFilter; + 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; + 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 = EasyMock.createStrictMock(DirectoryMonitor.Listener.class); - _monitor = new DirectoryMonitor(_data.getRoot(), new AwkFilenameFilter( - REGEX), _listener); + 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++) { - EasyMock.replay(_listener); - _monitor.poll(); - EasyMock.verify(_listener); - EasyMock.reset(_listener); + monitor.poll(); + verifyNoMoreInteractions(listener); } } public void testFileCreated() { - _listener.fileCreated(_data.getFile(FILE1)); - EasyMock.replay(_listener); - _data.createFile(FILE1, "hello"); - _monitor.poll(); - EasyMock.verify(_listener); + data.createFile(FILE1, "hello"); + monitor.poll(); + verify(listener).fileCreated(data.getFile(FILE1)); } public void testFileDeleted() { - _data.createFile(FILE1, "hello"); - _monitor.poll(); + data.createFile(FILE1, "hello"); + monitor.poll(); + reset(listener); - EasyMock.reset(_listener); + data.deleteFile(FILE1); + monitor.poll(); - _data.deleteFile(FILE1); - _listener.fileDeleted(_data.getFile(FILE1)); - EasyMock.replay(_listener); - _monitor.poll(); - EasyMock.verify(_listener); + verify(listener).fileDeleted(data.getFile(FILE1)); + verifyNoMoreInteractions(listener); } - - public void testFileChanged() throws InterruptedException { - _data.createFile(FILE1, "hello"); - _monitor.poll(); - EasyMock.reset(_listener); - + + public void testFileChanged() throws InterruptedException { + data.createFile(FILE1, "hello"); + monitor.poll(); + reset(listener); + Thread.sleep(2000); - _data.deleteFile(FILE1); - _data.createFile(FILE1, "bla"); - - _listener.fileChanged(_data.getFile(FILE1)); - EasyMock.replay(_listener); - _monitor.poll(); - EasyMock.verify(_listener); + 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"); - EasyMock.replay(_listener); - _monitor.poll(); - EasyMock.verify(_listener); + monitor.poll(); + + data.createFile("file.xml", "hello"); + monitor.poll(); + verifyNoMoreInteractions(listener); } - - public void testDirectoryIsIgnored() { - _monitor.poll(); - _data.createDir(FILE1); - EasyMock.replay(_listener); - _monitor.poll(); - EasyMock.verify(_listener); + + public void testDirectoryIsIgnored() { + monitor.poll(); + data.createDir(FILE1); + monitor.poll(); + verifyNoMoreInteractions(listener); } - + public void testExceptionsWIllLeadToRepeatedNotifications() { - _monitor.poll(); - _data.createFile(FILE1, "hello"); + monitor.poll(); + data.createFile(FILE1, "hello"); + + stubVoid(listener).toThrow(new RuntimeException()).on(). + fileCreated(data.getFile(FILE1)); - _listener.fileCreated(_data.getFile(FILE1)); - EasyMock.expectLastCall().andThrow(new RuntimeException()); - EasyMock.replay(_listener); try { - _monitor.poll(); + monitor.poll(); } catch (RuntimeException e) { - EasyMock.verify(_listener); - EasyMock.reset(_listener); + reset(listener); - // polling again should lead to the same filecreated call. - // this time no exception is thrown. + // polling again should lead to the same filecreated call. + // this time no exception is thrown. - _listener.fileCreated(_data.getFile(FILE1)); - EasyMock.replay(_listener); - _monitor.poll(); - EasyMock.verify(_listener); + monitor.poll(); + verify(listener).fileCreated(data.getFile(FILE1)); + verifyNoMoreInteractions(listener); return; } - fail(); // should not get here. + fail(); // should not get here. }