Skip to content
Snippets Groups Projects
Commit bb259c7b authored by anttil's avatar anttil
Browse files

BIS-518 / SP-827: Cleaning up.

SVN: 29681
parent b61909bb
No related merge requests found
...@@ -40,19 +40,22 @@ public class DelayingDecoratorTest ...@@ -40,19 +40,22 @@ public class DelayingDecoratorTest
private IEventFeed decorator; private IEventFeed decorator;
private IEventFilter filter;
private static final long INTERVAL = 500; private static final long INTERVAL = 500;
@BeforeMethod @BeforeMethod
public void fixture() public void fixture()
{ {
clock = new MockClock(System.currentTimeMillis()); clock = new MockClock();
context = new Mockery(); context = new Mockery();
provider = context.mock(IEventFeed.class); provider = context.mock(IEventFeed.class);
filter = context.mock(IEventFilter.class);
decorator = new DelayingDecorator(INTERVAL, provider, clock); decorator = new DelayingDecorator(INTERVAL, provider, clock);
} }
@Test @Test
public void eventsAreDelayed() throws Exception public void callsToDecoratedEventFeedAreDelayedByTheGivenInterval() throws Exception
{ {
context.checking(new Expectations() context.checking(new Expectations()
{ {
...@@ -60,32 +63,21 @@ public class DelayingDecoratorTest ...@@ -60,32 +63,21 @@ public class DelayingDecoratorTest
List<String> events = new ArrayList<String>(); List<String> events = new ArrayList<String>();
events.add("event-1"); events.add("event-1");
events.add("event-2"); events.add("event-2");
exactly(2).of(provider).getNewEvents(with(Matchers.eventFilterAccepting("event"))); exactly(2).of(provider).getNewEvents(filter);
will(returnValue(events)); 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); clock.setTime(firstTime + INTERVAL);
assertThat(decorator.getNewEvents(withName("event")).size(), is(0)); assertThat(decorator.getNewEvents(filter).size(), is(0));
clock.setTime(clock.getTime() + INTERVAL); clock.setTime(firstTime + INTERVAL + 1);
assertThat(decorator.getNewEvents(withName("event")).size(), is(2)); assertThat(decorator.getNewEvents(filter).size(), is(2));
context.assertIsSatisfied(); context.assertIsSatisfied();
} }
private IEventFilter withName(final String name)
{
return new IEventFilter()
{
@Override
public boolean accepts(String value)
{
return value.startsWith(name);
}
};
}
} }
...@@ -23,6 +23,8 @@ import java.io.File; ...@@ -23,6 +23,8 @@ import java.io.File;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
...@@ -35,31 +37,54 @@ public class FileSystemBasedEventProviderTest ...@@ -35,31 +37,54 @@ public class FileSystemBasedEventProviderTest
private IEventFeed provider; private IEventFeed provider;
private IEventFilter filter;
private Mockery context;
@BeforeMethod @BeforeMethod
public void fixture() public void fixture()
{ {
controlDir = new File("/tmp/" + UUID.randomUUID().toString()); controlDir = new File("/tmp/" + UUID.randomUUID().toString());
controlDir.mkdir(); controlDir.mkdir();
provider = new ControlDirectoryEventFeed(controlDir); provider = new ControlDirectoryEventFeed(controlDir);
context = new Mockery();
filter = context.mock(IEventFilter.class);
} }
@Test @Test
public void eventsAreReturnedOnlyOnce() throws Exception public void eventsAreReturnedOnlyOnce() throws Exception
{ {
context.checking(new Expectations()
{
{
allowing(filter).accepts("event");
will(returnValue(true));
}
});
createEvent("event"); createEvent("event");
assertThat(provider.getNewEvents(eventFilterAccepting("event")).isEmpty(), is(false)); assertThat(provider.getNewEvents(filter).isEmpty(), is(false));
assertThat(provider.getNewEvents(eventFilterAccepting("event")).isEmpty(), is(true)); assertThat(provider.getNewEvents(filter).isEmpty(), is(true));
} }
@Test @Test
public void eventsAreReturnedInCorrectOrder() throws Exception public void eventsAreReturnedInCorrectOrder() throws Exception
{ {
context.checking(new Expectations()
{
{
allowing(filter).accepts(with(any(String.class)));
will(returnValue(true));
}
});
createEvent("this_event", 1000); createEvent("this_event", 1000);
createEvent("that_event", 10000); createEvent("that_event", 10000);
createEvent("this_event2", 100000); createEvent("this_event2", 100000);
List<String> events = provider.getNewEvents(allPassingEventFilter()); List<String> events = provider.getNewEvents(filter);
assertThat(events.size(), is(3)); assertThat(events.size(), is(3));
assertThat(events.get(0), is("this_event")); assertThat(events.get(0), is("this_event"));
...@@ -70,10 +95,18 @@ public class FileSystemBasedEventProviderTest ...@@ -70,10 +95,18 @@ public class FileSystemBasedEventProviderTest
@Test @Test
public void filesAreCleaned() throws Exception public void filesAreCleaned() throws Exception
{ {
context.checking(new Expectations()
{
{
allowing(filter).accepts("event");
will(returnValue(true));
}
});
File event = new File(controlDir, "event"); File event = new File(controlDir, "event");
event.createNewFile(); event.createNewFile();
provider.getNewEvents(eventFilterAccepting("event")); provider.getNewEvents(filter);
assertThat(event.exists(), is(false)); assertThat(event.exists(), is(false));
} }
...@@ -81,41 +114,23 @@ public class FileSystemBasedEventProviderTest ...@@ -81,41 +114,23 @@ public class FileSystemBasedEventProviderTest
@Test @Test
public void unrelatedFilesAreNotCleaned() throws Exception public void unrelatedFilesAreNotCleaned() throws Exception
{ {
context.checking(new Expectations()
{
{
allowing(filter).accepts("other_event");
will(returnValue(false));
}
});
File event = new File(controlDir, "other_event"); File event = new File(controlDir, "other_event");
event.createNewFile(); event.createNewFile();
List<String> events = provider.getNewEvents(eventFilterAccepting("event")); List<String> events = provider.getNewEvents(filter);
assertThat(events.isEmpty(), is(true)); assertThat(events.isEmpty(), is(true));
assertThat(event.exists(), 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 private void createEvent(String event) throws Exception
{ {
createEvent(event, System.currentTimeMillis()); createEvent(event, System.currentTimeMillis());
...@@ -127,5 +142,4 @@ public class FileSystemBasedEventProviderTest ...@@ -127,5 +142,4 @@ public class FileSystemBasedEventProviderTest
f.createNewFile(); f.createNewFile();
f.setLastModified(timestamp); f.setLastModified(timestamp);
} }
} }
/*
* 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());
}
};
}
}
...@@ -24,9 +24,9 @@ public class MockClock implements IClock ...@@ -24,9 +24,9 @@ public class MockClock implements IClock
private long time; private long time;
public MockClock(long time) public MockClock()
{ {
this.time = time; this.time = System.currentTimeMillis();
} }
public void setTime(long time) public void setTime(long time)
......
...@@ -22,7 +22,10 @@ import static org.hamcrest.MatcherAssert.assertThat; ...@@ -22,7 +22,10 @@ import static org.hamcrest.MatcherAssert.assertThat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.UUID;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.jmock.Expectations; import org.jmock.Expectations;
import org.jmock.Mockery; import org.jmock.Mockery;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
...@@ -53,7 +56,7 @@ public class ParameterMapTest ...@@ -53,7 +56,7 @@ public class ParameterMapTest
context.checking(new Expectations() context.checking(new Expectations()
{ {
{ {
allowing(eventProvider).getNewEvents(with(Matchers.eventFilterAccepting("parameter"))); allowing(eventProvider).getNewEvents(with(eventFilterAccepting("parameter")));
will(returnValue(new ArrayList<String>())); will(returnValue(new ArrayList<String>()));
} }
}); });
...@@ -74,7 +77,7 @@ public class ParameterMapTest ...@@ -74,7 +77,7 @@ public class ParameterMapTest
context.checking(new Expectations() context.checking(new Expectations()
{ {
{ {
allowing(eventProvider).getNewEvents(with(Matchers.eventFilterAccepting("parameter"))); allowing(eventProvider).getNewEvents(with(eventFilterAccepting("parameter")));
will(returnValue(getUpdateOn("parameter"))); will(returnValue(getUpdateOn("parameter")));
} }
}); });
...@@ -88,7 +91,7 @@ public class ParameterMapTest ...@@ -88,7 +91,7 @@ public class ParameterMapTest
context.checking(new Expectations() context.checking(new Expectations()
{ {
{ {
allowing(eventProvider).getNewEvents(with(Matchers.eventFilterAccepting("parameter"))); allowing(eventProvider).getNewEvents(with(eventFilterAccepting("parameter")));
will(returnValue(Arrays.asList("parameter-"))); will(returnValue(Arrays.asList("parameter-")));
} }
}); });
...@@ -102,7 +105,7 @@ public class ParameterMapTest ...@@ -102,7 +105,7 @@ public class ParameterMapTest
context.checking(new Expectations() context.checking(new Expectations()
{ {
{ {
allowing(eventProvider).getNewEvents(with(Matchers.eventFilterAccepting("parameter"))); allowing(eventProvider).getNewEvents(with(eventFilterAccepting("parameter")));
will(returnValue(getUpdateOn("parameter"))); will(returnValue(getUpdateOn("parameter")));
} }
}); });
...@@ -147,4 +150,23 @@ public class ParameterMapTest ...@@ -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());
}
};
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment