X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fio%2FDirectoryMonitorTest.java;h=e1437dcdc2a6c33f8cbaaf0e6924742fa28039df;hb=8de36ff0206c996baf3ee4adc3e2293b12ff5f39;hp=347857da528b1ef04905ab2a630c1c3f122cb5c3;hpb=8557fbe8c7ea4e1cbcbf10d3c4e8c60c9c1e312b;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 347857da..e1437dcd 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,110 +15,104 @@ */ package org.wamblee.io; -import static org.mockito.Mockito.*; import junit.framework.TestCase; import org.apache.oro.io.AwkFilenameFilter; -import org.wamblee.test.ResettableMock; +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 ResettableMock _listener; - - private DirectoryMonitor _monitor; - - @Override - protected void setUp() throws Exception { - super.setUp(); - _data = new TestData(this); - _data.clean(); - _listener = new ResettableMock( - DirectoryMonitor.Listener.class); - _monitor = new DirectoryMonitor(_data.getRoot(), new AwkFilenameFilter( - REGEX), _listener.getProxy()); - } - - public void testEmptyDir() { - // Nothing is expected to be called. - for (int i = 0; i < 10; i++) { - _monitor.poll(); - verifyNoMoreInteractions(_listener.getMock()); - } - } - - public void testFileCreated() { - _data.createFile(FILE1, "hello"); - _monitor.poll(); - verify(_listener.getMock()).fileCreated(_data.getFile(FILE1)); - } - - public void testFileDeleted() { - _data.createFile(FILE1, "hello"); - _monitor.poll(); - _listener.reset(); - - _data.deleteFile(FILE1); - _monitor.poll(); - - verify(_listener.getMock()).fileDeleted(_data.getFile(FILE1)); - verifyNoMoreInteractions(_listener.getMock()); - } - - public void testFileChanged() throws InterruptedException { - _data.createFile(FILE1, "hello"); - _monitor.poll(); - _listener.reset(); - - Thread.sleep(2000); - _data.deleteFile(FILE1); - _data.createFile(FILE1, "bla"); - - _monitor.poll(); - verify(_listener.getMock()).fileChanged(_data.getFile(FILE1)); - verifyNoMoreInteractions(_listener.getMock()); - } - - public void testFileFilterIsUsed() { - _monitor.poll(); - - _data.createFile("file.xml", "hello"); - _monitor.poll(); - verifyNoMoreInteractions(_listener.getMock()); - } - - public void testDirectoryIsIgnored() { - _monitor.poll(); - _data.createDir(FILE1); - _monitor.poll(); - verifyNoMoreInteractions(_listener.getMock()); - } - - public void testExceptionsWIllLeadToRepeatedNotifications() { - _monitor.poll(); - _data.createFile(FILE1, "hello"); - - stubVoid(_listener.getMock()).toThrow(new RuntimeException()).on(). - fileCreated(_data.getFile(FILE1)); - - try { - _monitor.poll(); - } catch (RuntimeException e) { - _listener.reset(); - - // polling again should lead to the same filecreated call. - // this time no exception is thrown. - - _monitor.poll(); - verify(_listener.getMock()).fileCreated(_data.getFile(FILE1)); - verifyNoMoreInteractions(_listener.getMock()); - 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. + } }