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

Added some more JUnit tests for stages; Modified the StageTester slightly to allow "empty" input.

parent 7c0d5cef
No related branches found
No related tags found
No related merge requests found
...@@ -86,7 +86,7 @@ public final class StageTester { ...@@ -86,7 +86,7 @@ public final class StageTester {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public StageTester to(final InputPort<I> port) { public StageTester to(final InputPort<? extends I> port) {
if (port.getOwningStage() != stage) { if (port.getOwningStage() != stage) {
throw new AssertionError(); throw new AssertionError();
} }
......
/**
* 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));
}
}
/**
* 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));
}
}
/**
* 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));
}
}
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