diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/DelayingDecoratorTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/DelayingDecoratorTest.java index c784cbd2cbafb0506898e50503859492dc431480..407b9d831d0fb09b03ed867e04f1195701f4057d 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/DelayingDecoratorTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/DelayingDecoratorTest.java @@ -40,19 +40,22 @@ public class DelayingDecoratorTest private IEventFeed decorator; + private IEventFilter filter; + private static final long INTERVAL = 500; @BeforeMethod public void fixture() { - clock = new MockClock(System.currentTimeMillis()); + clock = new MockClock(); context = new Mockery(); provider = context.mock(IEventFeed.class); + filter = context.mock(IEventFilter.class); decorator = new DelayingDecorator(INTERVAL, provider, clock); } @Test - public void eventsAreDelayed() throws Exception + public void callsToDecoratedEventFeedAreDelayedByTheGivenInterval() throws Exception { context.checking(new Expectations() { @@ -60,32 +63,21 @@ public class DelayingDecoratorTest List<String> events = new ArrayList<String>(); events.add("event-1"); events.add("event-2"); - exactly(2).of(provider).getNewEvents(with(Matchers.eventFilterAccepting("event"))); + exactly(2).of(provider).getNewEvents(filter); will(returnValue(events)); } }); - assertThat(decorator.getNewEvents(withName("event")).size(), is(2)); + long firstTime = System.currentTimeMillis(); + clock.setTime(firstTime); + assertThat(decorator.getNewEvents(filter).size(), is(2)); - clock.setTime(clock.getTime() + INTERVAL - 1); - assertThat(decorator.getNewEvents(withName("event")).size(), is(0)); + clock.setTime(firstTime + INTERVAL); + assertThat(decorator.getNewEvents(filter).size(), is(0)); - clock.setTime(clock.getTime() + INTERVAL); - assertThat(decorator.getNewEvents(withName("event")).size(), is(2)); + clock.setTime(firstTime + INTERVAL + 1); + assertThat(decorator.getNewEvents(filter).size(), is(2)); context.assertIsSatisfied(); } - - private IEventFilter withName(final String name) - { - return new IEventFilter() - { - - @Override - public boolean accepts(String value) - { - return value.startsWith(name); - } - }; - } } diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/FileSystemBasedEventProviderTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/FileSystemBasedEventProviderTest.java index d08babc687642aa272ae5ee9929db1950e8f520b..37e17b7556c0405fb799bc1f96ffbce44280f5a7 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/FileSystemBasedEventProviderTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/FileSystemBasedEventProviderTest.java @@ -23,6 +23,8 @@ import java.io.File; import java.util.List; import java.util.UUID; +import org.jmock.Expectations; +import org.jmock.Mockery; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -35,31 +37,54 @@ public class FileSystemBasedEventProviderTest private IEventFeed provider; + private IEventFilter filter; + + private Mockery context; + @BeforeMethod public void fixture() { controlDir = new File("/tmp/" + UUID.randomUUID().toString()); controlDir.mkdir(); provider = new ControlDirectoryEventFeed(controlDir); + + context = new Mockery(); + filter = context.mock(IEventFilter.class); + } @Test public void eventsAreReturnedOnlyOnce() throws Exception { + context.checking(new Expectations() + { + { + allowing(filter).accepts("event"); + will(returnValue(true)); + } + }); createEvent("event"); - assertThat(provider.getNewEvents(eventFilterAccepting("event")).isEmpty(), is(false)); - assertThat(provider.getNewEvents(eventFilterAccepting("event")).isEmpty(), is(true)); + assertThat(provider.getNewEvents(filter).isEmpty(), is(false)); + assertThat(provider.getNewEvents(filter).isEmpty(), is(true)); } @Test public void eventsAreReturnedInCorrectOrder() throws Exception { + context.checking(new Expectations() + { + { + allowing(filter).accepts(with(any(String.class))); + will(returnValue(true)); + } + }); + createEvent("this_event", 1000); createEvent("that_event", 10000); createEvent("this_event2", 100000); - List<String> events = provider.getNewEvents(allPassingEventFilter()); + List<String> events = provider.getNewEvents(filter); assertThat(events.size(), is(3)); assertThat(events.get(0), is("this_event")); @@ -70,10 +95,18 @@ public class FileSystemBasedEventProviderTest @Test public void filesAreCleaned() throws Exception { + context.checking(new Expectations() + { + { + allowing(filter).accepts("event"); + will(returnValue(true)); + } + }); + File event = new File(controlDir, "event"); event.createNewFile(); - provider.getNewEvents(eventFilterAccepting("event")); + provider.getNewEvents(filter); assertThat(event.exists(), is(false)); } @@ -81,41 +114,23 @@ public class FileSystemBasedEventProviderTest @Test public void unrelatedFilesAreNotCleaned() throws Exception { + context.checking(new Expectations() + { + { + allowing(filter).accepts("other_event"); + will(returnValue(false)); + } + }); + File event = new File(controlDir, "other_event"); event.createNewFile(); - List<String> events = provider.getNewEvents(eventFilterAccepting("event")); + List<String> events = provider.getNewEvents(filter); assertThat(events.isEmpty(), is(true)); assertThat(event.exists(), is(true)); } - private IEventFilter eventFilterAccepting(final String event) - { - return new IEventFilter() - { - - @Override - public boolean accepts(String value) - { - return event.equals(value); - } - }; - } - - private IEventFilter allPassingEventFilter() - { - return new IEventFilter() - { - - @Override - public boolean accepts(String value) - { - return true; - } - }; - } - private void createEvent(String event) throws Exception { createEvent(event, System.currentTimeMillis()); @@ -127,5 +142,4 @@ public class FileSystemBasedEventProviderTest f.createNewFile(); f.setLastModified(timestamp); } - } diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/Matchers.java b/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/Matchers.java deleted file mode 100644 index 7e8f9a7b339d08753ad3dad684da552cb37414c2..0000000000000000000000000000000000000000 --- a/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/Matchers.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2013 ETH Zuerich, CISD - * - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.systemsx.cisd.common.filesystem.control; - -import java.util.UUID; - -import org.hamcrest.Description; -import org.hamcrest.TypeSafeMatcher; - -/** - * @author anttil - */ -public class Matchers -{ - static TypeSafeMatcher<IEventFilter> eventFilterAccepting(final String value) - { - return new TypeSafeMatcher<IEventFilter>() - { - - @Override - public void describeTo(Description description) - { - description.appendText("IEventFilter accepting '" + value + "-*'"); - } - - @Override - public boolean matchesSafely(IEventFilter filter) - { - return filter.accepts(value + "-" + UUID.randomUUID().toString()); - } - }; - } -} diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/MockClock.java b/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/MockClock.java index c7061feb34293a96eb597ba4c21df9595bd0af14..40ac668f71b1bc581fb549a21e860b7b577ae15d 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/MockClock.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/MockClock.java @@ -24,9 +24,9 @@ public class MockClock implements IClock private long time; - public MockClock(long time) + public MockClock() { - this.time = time; + this.time = System.currentTimeMillis(); } public void setTime(long time) diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/ParameterMapTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/ParameterMapTest.java index 16008453bf5252c1f83cf89d699730de5ff00955..5e7d7196c3312a102a5a5088779688eafd0946f7 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/ParameterMapTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/filesystem/control/ParameterMapTest.java @@ -22,7 +22,10 @@ import static org.hamcrest.MatcherAssert.assertThat; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.UUID; +import org.hamcrest.Description; +import org.hamcrest.TypeSafeMatcher; import org.jmock.Expectations; import org.jmock.Mockery; import org.testng.annotations.BeforeMethod; @@ -53,7 +56,7 @@ public class ParameterMapTest context.checking(new Expectations() { { - allowing(eventProvider).getNewEvents(with(Matchers.eventFilterAccepting("parameter"))); + allowing(eventProvider).getNewEvents(with(eventFilterAccepting("parameter"))); will(returnValue(new ArrayList<String>())); } }); @@ -74,7 +77,7 @@ public class ParameterMapTest context.checking(new Expectations() { { - allowing(eventProvider).getNewEvents(with(Matchers.eventFilterAccepting("parameter"))); + allowing(eventProvider).getNewEvents(with(eventFilterAccepting("parameter"))); will(returnValue(getUpdateOn("parameter"))); } }); @@ -88,7 +91,7 @@ public class ParameterMapTest context.checking(new Expectations() { { - allowing(eventProvider).getNewEvents(with(Matchers.eventFilterAccepting("parameter"))); + allowing(eventProvider).getNewEvents(with(eventFilterAccepting("parameter"))); will(returnValue(Arrays.asList("parameter-"))); } }); @@ -102,7 +105,7 @@ public class ParameterMapTest context.checking(new Expectations() { { - allowing(eventProvider).getNewEvents(with(Matchers.eventFilterAccepting("parameter"))); + allowing(eventProvider).getNewEvents(with(eventFilterAccepting("parameter"))); will(returnValue(getUpdateOn("parameter"))); } }); @@ -147,4 +150,23 @@ public class ParameterMapTest }; } + + static TypeSafeMatcher<IEventFilter> eventFilterAccepting(final String value) + { + return new TypeSafeMatcher<IEventFilter>() + { + + @Override + public void describeTo(Description description) + { + description.appendText("IEventFilter accepting '" + value + "-*'"); + } + + @Override + public boolean matchesSafely(IEventFilter filter) + { + return filter.accepts(value + "-" + UUID.randomUUID().toString()); + } + }; + } }