Skip to content
Snippets Groups Projects
Commit 6167741b authored by Nils Christian Ehmke's avatar Nils Christian Ehmke
Browse files

Added some methods to the stage tester; Modified the distributor test in order...

Added some methods to the stage tester; Modified the distributor test in order to use the new stage tester
parent fda679f0
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package teetime.framework.test; package teetime.framework.test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import teetime.framework.Analysis; import teetime.framework.Analysis;
...@@ -31,7 +33,7 @@ import teetime.stage.IterableProducer; ...@@ -31,7 +33,7 @@ import teetime.stage.IterableProducer;
/** /**
* This class can be used to test single stages in JUnit test cases. * This class can be used to test single stages in JUnit test cases.
* *
* @author Nils Christian Ehmke * @author Nils Christian Ehmke
*/ */
public final class StageTester { public final class StageTester {
...@@ -54,6 +56,14 @@ public final class StageTester { ...@@ -54,6 +56,14 @@ public final class StageTester {
return inputHolder; return inputHolder;
} }
public <I> InputHolder<I> send(final I... input) {
return this.send(Arrays.asList(input));
}
public <I> InputHolder<I> send(final I input) {
return this.send(Collections.singletonList(input));
}
public <O> OutputHolder<O> receive(final List<O> output) { public <O> OutputHolder<O> receive(final List<O> output) {
OutputHolder<O> outputHolder = new OutputHolder<O>(output); OutputHolder<O> outputHolder = new OutputHolder<O>(output);
this.outputHolders.add(outputHolder); this.outputHolders.add(outputHolder);
......
...@@ -20,19 +20,19 @@ import static org.hamcrest.Matchers.is; ...@@ -20,19 +20,19 @@ import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.hamcrest.collection.IsEmptyCollection.empty; import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static teetime.framework.test.StageTester.test;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import teetime.framework.pipe.IPipeFactory;
import teetime.framework.pipe.SingleElementPipeFactory;
import teetime.stage.CollectorSink;
/** /**
* @author Nils Christian Ehmke * @author Nils Christian Ehmke
* *
* @since 1.0 * @since 1.0
*/ */
public class DistributorTest { public class DistributorTest {
...@@ -40,93 +40,80 @@ public class DistributorTest { ...@@ -40,93 +40,80 @@ public class DistributorTest {
@Rule @Rule
public ExpectedException expectedException = ExpectedException.none(); public ExpectedException expectedException = ExpectedException.none();
private Distributor<Integer> distributorUnderTest; private Distributor<Integer> distributor;
private CollectorSink<Integer> fstCollector; private List<Integer> fstList;
private CollectorSink<Integer> sndCollector; private List<Integer> sndList;
@Before @Before
public void initializeDistributor() throws Exception { public void initializeDistributor() throws Exception {
this.distributorUnderTest = new Distributor<Integer>(); this.distributor = new Distributor<Integer>();
this.fstCollector = new CollectorSink<Integer>(); this.fstList = new ArrayList<Integer>();
this.sndCollector = new CollectorSink<Integer>(); this.sndList = new ArrayList<Integer>();
final IPipeFactory pipeFactory = new SingleElementPipeFactory();
pipeFactory.create(this.distributorUnderTest.getNewOutputPort(), this.fstCollector.getInputPort());
pipeFactory.create(this.distributorUnderTest.getNewOutputPort(), this.sndCollector.getInputPort());
distributorUnderTest.onStarting();
} }
@Test @Test
public void roundRobinShouldWork() { public void roundRobinShouldWork() {
distributorUnderTest.setStrategy(new RoundRobinStrategy()); distributor.setStrategy(new RoundRobinStrategy());
this.distributorUnderTest.execute(1); test(distributor).and().send(1, 2, 3, 4, 5).to(distributor.getInputPort()).and().receive(fstList).from(distributor.getNewOutputPort()).and()
this.distributorUnderTest.execute(2); .receive(sndList).from(distributor.getNewOutputPort()).start();
this.distributorUnderTest.execute(3);
this.distributorUnderTest.execute(4);
this.distributorUnderTest.execute(5);
assertThat(this.fstCollector.getElements(), contains(1, 3, 5)); assertThat(this.fstList, contains(1, 3, 5));
assertThat(this.sndCollector.getElements(), contains(2, 4)); assertThat(this.sndList, contains(2, 4));
} }
@Test @Test
public void singleElementRoundRobinShouldWork() { public void singleElementRoundRobinShouldWork() {
distributorUnderTest.setStrategy(new RoundRobinStrategy()); distributor.setStrategy(new RoundRobinStrategy());
this.distributorUnderTest.execute(1); test(distributor).and().send(1).to(distributor.getInputPort()).and().receive(fstList).from(distributor.getNewOutputPort()).and().receive(sndList)
.from(distributor.getNewOutputPort()).start();
assertThat(this.fstCollector.getElements(), contains(1)); assertThat(this.fstList, contains(1));
assertThat(this.sndCollector.getElements(), is(empty())); assertThat(this.sndList, is(empty()));
} }
@Test @Test
public void copyByReferenceShouldWork() { public void copyByReferenceShouldWork() {
distributorUnderTest.setStrategy(new CopyByReferenceStrategy()); distributor.setStrategy(new CopyByReferenceStrategy());
this.distributorUnderTest.execute(1); test(distributor).and().send(1, 2, 3, 4, 5).to(distributor.getInputPort()).and().receive(fstList).from(distributor.getNewOutputPort()).and()
this.distributorUnderTest.execute(2); .receive(sndList).from(distributor.getNewOutputPort()).start();
this.distributorUnderTest.execute(3);
this.distributorUnderTest.execute(4);
this.distributorUnderTest.execute(5);
assertThat(this.fstCollector.getElements(), contains(1, 2, 3, 4, 5)); assertThat(this.fstList, contains(1, 2, 3, 4, 5));
assertThat(this.sndCollector.getElements(), contains(1, 2, 3, 4, 5)); assertThat(this.sndList, contains(1, 2, 3, 4, 5));
} }
@Test @Test
public void singleElementCopyByReferenceShouldWork() { public void singleElementCopyByReferenceShouldWork() {
distributorUnderTest.setStrategy(new CopyByReferenceStrategy()); distributor.setStrategy(new CopyByReferenceStrategy());
this.distributorUnderTest.execute(1); test(distributor).and().send(1).to(distributor.getInputPort()).and().receive(fstList).from(distributor.getNewOutputPort()).and().receive(sndList)
.from(distributor.getNewOutputPort()).start();
assertThat(this.fstCollector.getElements(), contains(1)); assertThat(this.fstList, contains(1));
assertThat(this.sndCollector.getElements(), contains(1)); assertThat(this.sndList, contains(1));
} }
@Test @Test
public void cloneForIntegerShouldNotWork() { public void cloneForIntegerShouldNotWork() throws Exception {
distributorUnderTest.setStrategy(new CloneStrategy()); this.distributor.setStrategy(new CloneStrategy());
this.distributor.getNewOutputPort();
this.distributor.onStarting();
expectedException.expect(UnsupportedOperationException.class); expectedException.expect(UnsupportedOperationException.class);
this.distributorUnderTest.execute(1); this.distributor.execute(1);
} }
@Test @Test
public void cloneForSimpleBeanShoulWork() throws Exception { public void cloneForSimpleBeanShoulWork() throws Exception {
final Distributor<SimpleBean> distributorUnderTest = new Distributor<SimpleBean>(new CloneStrategy()); final Distributor<SimpleBean> distributor = new Distributor<SimpleBean>(new CloneStrategy());
final CollectorSink<SimpleBean> collector = new CollectorSink<SimpleBean>(); final List<SimpleBean> results = new ArrayList<SimpleBean>();
final IPipeFactory pipeFactory = new SingleElementPipeFactory();
pipeFactory.create(distributorUnderTest.getNewOutputPort(), collector.getInputPort());
distributorUnderTest.onStarting();
final SimpleBean originalBean = new SimpleBean(42); final SimpleBean originalBean = new SimpleBean(42);
distributorUnderTest.execute(originalBean);
final SimpleBean clonedBean = collector.getElements().get(0);
test(distributor).and().send(originalBean).to(distributor.getInputPort()).and().receive(results).from(distributor.getNewOutputPort()).start();
final SimpleBean clonedBean = results.get(0);
assertThat(originalBean, is(not(clonedBean))); assertThat(originalBean, is(not(clonedBean)));
assertThat(originalBean.getValue(), is(clonedBean.getValue())); assertThat(originalBean.getValue(), is(clonedBean.getValue()));
} }
......
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