From c62ba67e29a4a5ed2787afc682eb08cb45a576fb Mon Sep 17 00:00:00 2001 From: Nils Christian Ehmke <nils@rhocas.de> Date: Fri, 27 Feb 2015 10:28:32 +0100 Subject: [PATCH] Added some more JUnit tests for stages; Modified the StageTester slightly to allow "empty" input. --- .../teetime/framework/test/StageTester.java | 2 +- src/test/java/teetime/stage/CounterTest.java | 62 +++++++++++++++++ .../stage/InitialElementProducerTest.java | 59 ++++++++++++++++ .../teetime/stage/MappingCounterTest.java | 69 +++++++++++++++++++ 4 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 src/test/java/teetime/stage/CounterTest.java create mode 100644 src/test/java/teetime/stage/InitialElementProducerTest.java create mode 100644 src/test/java/teetime/stage/MappingCounterTest.java diff --git a/src/main/java/teetime/framework/test/StageTester.java b/src/main/java/teetime/framework/test/StageTester.java index 816cad49..a7940d96 100644 --- a/src/main/java/teetime/framework/test/StageTester.java +++ b/src/main/java/teetime/framework/test/StageTester.java @@ -86,7 +86,7 @@ public final class StageTester { } @SuppressWarnings("unchecked") - public StageTester to(final InputPort<I> port) { + public StageTester to(final InputPort<? extends I> port) { if (port.getOwningStage() != stage) { throw new AssertionError(); } diff --git a/src/test/java/teetime/stage/CounterTest.java b/src/test/java/teetime/stage/CounterTest.java new file mode 100644 index 00000000..a93d1fb5 --- /dev/null +++ b/src/test/java/teetime/stage/CounterTest.java @@ -0,0 +1,62 @@ +/** + * Copyright (C) 2015 TeeTime (http://teetime.sourceforge.net) + * + * 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 teetime.stage; + +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; +import static org.hamcrest.core.Is.is; +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.Test; + +/** + * @author Nils Christian Ehmke + */ +public class CounterTest { + + private Counter<Integer> counter; + + @Before + public void initializeCounter() { + counter = new Counter<Integer>(); + } + + @Test + public void counterValueShouldInitiallyBeZero() { + assertThat(counter.getNumElementsPassed(), is(0)); + } + + @Test + public void counterValueShouldBeCorrect() { + test(counter).and().send(1, 2, -5, 10, 9).to(counter.getInputPort()).start(); + + assertThat(counter.getNumElementsPassed(), is(5)); + } + + @Test + public void counterShouldForwardElements() { + List<Integer> results = new ArrayList<Integer>(); + + test(counter).and().send(1, 2, 3).to(counter.getInputPort()).and().receive(results).from(counter.getOutputPort()).start(); + + assertThat(results, contains(1, 2, 3)); + } + +} diff --git a/src/test/java/teetime/stage/InitialElementProducerTest.java b/src/test/java/teetime/stage/InitialElementProducerTest.java new file mode 100644 index 00000000..c3d505ee --- /dev/null +++ b/src/test/java/teetime/stage/InitialElementProducerTest.java @@ -0,0 +1,59 @@ +/** + * Copyright (C) 2015 TeeTime (http://teetime.sourceforge.net) + * + * 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 teetime.stage; + +import static org.hamcrest.collection.IsEmptyCollection.empty; +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; +import static org.hamcrest.core.Is.is; +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.Test; + +/** + * @author Nils Christian Ehmke + */ +public class InitialElementProducerTest { + + private InitialElementProducer<Integer> producer; + + @Before + public void initializeProducer() { + producer = new InitialElementProducer<Integer>(); + } + + @Test + public void producerShouldByDefaultSendNothing() { + List<Integer> results = new ArrayList<Integer>(); + + test(producer).and().receive(results).from(producer.getOutputPort()).start(); + assertThat(results, is(empty())); + } + + @Test + public void producerShouldSendDefinedValues() { + producer.setIter(new Integer[] { 1, 2, 3 }); + List<Integer> results = new ArrayList<Integer>(); + + test(producer).and().receive(results).from(producer.getOutputPort()).start(); + assertThat(results, contains(1, 2, 3)); + } + +} diff --git a/src/test/java/teetime/stage/MappingCounterTest.java b/src/test/java/teetime/stage/MappingCounterTest.java new file mode 100644 index 00000000..b34dbfaf --- /dev/null +++ b/src/test/java/teetime/stage/MappingCounterTest.java @@ -0,0 +1,69 @@ +/** + * Copyright (C) 2015 TeeTime (http://teetime.sourceforge.net) + * + * 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 teetime.stage; + +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.hamcrest.core.Is.is; +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.Test; + +import teetime.stage.util.CountingMap; + +/** + * @author Nils Christian Ehmke + */ +public class MappingCounterTest { + + private MappingCounter<Integer> counter; + + @Before + public void initializeCounter() { + counter = new MappingCounter<Integer>(); + } + + @Test + public void countingMapForNoInputShouldBeEmpty() { + List<CountingMap<Integer>> results = new ArrayList<CountingMap<Integer>>(); + + test(counter).and().send().to(counter.getInputPort()).receive(results).from(counter.getOutputPort()).start(); + + assertThat(results, hasSize(1)); + + final CountingMap<Integer> map = results.get(0); + assertThat(map.size(), is(0)); + } + + @Test + public void countingMapShouldBeCorrect() { + List<CountingMap<Integer>> results = new ArrayList<CountingMap<Integer>>(); + + test(counter).and().send(1, 4, 3, 4, 1).to(counter.getInputPort()).and().receive(results).from(counter.getOutputPort()).start(); + + assertThat(results, hasSize(1)); + + final CountingMap<Integer> map = results.get(0); + assertThat(map.size(), is(3)); + assertThat(map.get(1), is(2)); + assertThat(map.get(3), is(1)); + assertThat(map.get(4), is(2)); + } +} -- GitLab