Package com.ibm.streams.flow.javaprimitives
Testing of an SPL directed flow graph that contains Java primitive operators.
See: Description
-
Interface Summary Interface Description JavaTestableGraph An executable flow graph of Java primitive operators with support for disconnected input and output ports. -
Class Summary Class Description JavaOperatorTester Utility methods for testingOperatorGraph
instances that contain implementations ofOperator
.
Package com.ibm.streams.flow.javaprimitives Description
Testing of an SPL directed flow graph that contains Java primitive operators.
Overview
In a test framework, theJavaTestableGraph
interface supports the execution
of an arbitrary graph
that contains Java primitive operators.
In a Java development environment using standard Java techniques,
this interface supports unit testing of Java code for classes that
implement Operator
.
The execution environment for the graph simulates an InfoSphere Streams standalone application, with these differences:
- All Java primitive operators are defined only by the Java class, not by the operator model.
- The external test framework loads classes, and therefore:
- All classes must be provided by the external framework. For example, the class path for executing JUnit tests in Apache Ant must include all the required classes.
- Classes are be shared across operators. In an InfoSphere Streams compiled SPL application, each operator has its own class loader that loads the classes defined by its operator model.
Testing Example
The following code is a simple example that shows how anOperatorGraph
is tested after it has been declared.
OperatorGraph graph = OperatorGraphFactory.newGraph();
// Definition of graph omitted.
// See overview for package com.ibm.streams.operator.flow.declare for example
// Create the testable version of the graph
JavaTestableGraph testableGraph = new JavaOperatorTester().executable(graph);
// Execute the initialization of operators within graph.
testableGraph.initialize().get();
Single Operator Java Class Testing
JavaOperatorTester
contains utility methods
that simplify testing for a single class that implements
Operator
.
Test code can create an OperatorInvocation
directly using
JavaOperatorTester.singleOp(Class)
and a JavaTestableGraph
for it using JavaOperatorTester.tester(OperatorInvocation)
.
Example
The following sample code represents a single operator unit test in the JUnit style for the sampleRandomBeacon
operator.
The test is a unit test that verifies that the number of tuples submitted by the operator matches
the value set for the corresponding iterations
parameter.
Test
public void testIterations() throws Exception {
final int expectedCount = 100;
JavaOperatorTester jot = new JavaOperatorTester();
// Set the invocation context for the single operator
// including setting the iterations parameter
OperatorInvocation<RandomBeacon> op = jot.singleOp(RandomBeacon.class);
OutputPortDeclaration output = op.addOutput("tuple<int32 a>");
op.paramInt("iterations", expectedCount);
// Create a graph for testing
TestableGraph tester = jot.tester(op);
// Attach a StreamHandler that counts tuples and punctuation marks to the output port
StreamCounter<Tuple> counter = new StreamCounter<Tuple>();
tester.registerStreamHandler(output, counter);
// Execute the graph
tester.executeToCompletion();
// Verify the counter see the correct number of tuples and punctuation marks.
assertEquals(expectedCount, counter.getTupleCount());
assertEquals(1, counter.getMarkCount(Punctuation.WINDOW_MARKER));
assertEquals(1, counter.getMarkCount(Punctuation.FINAL_MARKER));
}
- Since:
- InfoSphere® Streams Version 3.1