Skip to content
Snippets Groups Projects
Commit 05d40419 authored by Lars Erik Blümke's avatar Lars Erik Blümke
Browse files

migration + test of TypeFilter

parent 4c2cabf0
No related branches found
No related tags found
No related merge requests found
package kieker.analysis.plugin.filter.select;
import teetime.framework.AbstractConsumerStage;
import teetime.framework.OutputPort;
/**
* This filter has exactly one input port and one output port.
*
* Only the specified objects are forwarded to the output port.
* All other objects are forwarded to the output-not port.
*
* @author Jan Waller, Lars Erik Bluemke
*
* @since 1.5
*/
public class TypeFilterStage extends AbstractConsumerStage<Object> {
private final Class<?>[] acceptedClasses;
private final OutputPort<Object> matchingTypeOutputPort = createOutputPort();
private final OutputPort<Object> mismatchingTypeOutputPort = createOutputPort();
/**
* Creates a new instance of this class using the given parameters.
*
* @param acceptedClasses
* The types which will be accepted by the filter.
*/
public TypeFilterStage(final Class<?>[] acceptedClasses) {
this.acceptedClasses = acceptedClasses;
}
/**
* This method represents the input port for the incoming objects.
*
* @param event
* The new incoming object.
*/
@Override
protected void execute(final Object event) {
final Class<?> eventClass = event.getClass();
for (final Class<?> clazz : this.acceptedClasses) {
if (clazz.isAssignableFrom(eventClass)) {
matchingTypeOutputPort.send(event);
return; // only deliver once!
}
}
mismatchingTypeOutputPort.send(event);
}
/**
* Returns the output port where the incoming matching objects will be sent to.
*/
public OutputPort<Object> getMatchingTypeOutputPort() {
return matchingTypeOutputPort;
}
/**
* Returns the output port where the incoming objects will be sent to, which do not match the configured types.
*/
public OutputPort<Object> getMismatchingTypeOutputPort() {
return mismatchingTypeOutputPort;
}
}
package kieker.analysis.plugin.filter.select;
import static teetime.framework.test.StageTester.test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
/**
* A test for the class {@link TypeFilter}.
*
* @author Nils Christian Ehmke, Lars Erik Bluemke
*
* @since 1.8
*/
public class TypeFilterStageTest {
private static final Class<?>[] ALLOWED_TYPES = { String.class };
private static final Object[] MATCHING_OBJECTS = { "First String", "Second String", "Third String", "Fourth String" };
private static final Object[] MISMATCHING_OBJECTS = { 'A', 42L, 2.5, new Object() };
/**
* Default constructor.
*/
public TypeFilterStageTest() {
// empty default constructor
}
@Test
public void testFiltering() {
TypeFilterStage typeFilter = new TypeFilterStage(ALLOWED_TYPES);
List<Object> inputElements = new ArrayList<>();
List<Object> validOutputElements = new ArrayList<>();
List<Object> invalidOutputElements = new ArrayList<>();
// Make sure that the reader sends valid and invalid objects interleaved
for (int i = 0; i < Math.min(MATCHING_OBJECTS.length, MISMATCHING_OBJECTS.length); i++) {
inputElements.add(MATCHING_OBJECTS[i]);
inputElements.add(MISMATCHING_OBJECTS[i]);
}
test(typeFilter)
.and().send(inputElements).to(typeFilter.getInputPort())
.and().receive(validOutputElements).from(typeFilter.getMatchingTypeOutputPort())
.and().receive(invalidOutputElements).from(typeFilter.getMismatchingTypeOutputPort())
.start();
// Make sure that valid objects are sent to the valid output port and that invalid objects are sent to the invalid output port
Assert.assertTrue("Type filter filtered matching types", validOutputElements.containsAll(Arrays.asList(MATCHING_OBJECTS)));
Assert.assertTrue("Type filter did not filter mismatching types", invalidOutputElements.containsAll(Arrays.asList(MISMATCHING_OBJECTS)));
Assert.assertEquals("Type filter sent too much to the match output port", MATCHING_OBJECTS.length, validOutputElements.size());
Assert.assertEquals("Type filter sent too much to the mismatch output port.", MISMATCHING_OBJECTS.length, invalidOutputElements.size());
}
}
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