From 502093cd87c671bfdb0140aaae2dcc844e2e69ee Mon Sep 17 00:00:00 2001 From: Christian Wulf <chw@informatik.uni-kiel.de> Date: Mon, 6 Jul 2015 15:31:56 +0200 Subject: [PATCH] fixed some findbug issues --- .settings/edu.umd.cs.findbugs.core.prefs | 2 +- .../java/teetime/framework/Execution.java | 4 ++ .../java/teetime/framework/ExecutionTest.java | 13 +++-- .../ExceptionHandlingTest.java | 56 ++++++++++--------- .../ExceptionTestConfiguration.java | 1 + .../exceptionHandling/TestListener.java | 14 ++--- .../TestListenerFactory.java | 13 ++++- .../dynamic/DynamicDistributorTest.java | 10 +--- 8 files changed, 66 insertions(+), 47 deletions(-) diff --git a/.settings/edu.umd.cs.findbugs.core.prefs b/.settings/edu.umd.cs.findbugs.core.prefs index d4ea81f4..6102e83f 100644 --- a/.settings/edu.umd.cs.findbugs.core.prefs +++ b/.settings/edu.umd.cs.findbugs.core.prefs @@ -1,5 +1,5 @@ #FindBugs User Preferences -#Thu Jul 02 16:19:05 CEST 2015 +#Mon Jul 06 14:06:35 CEST 2015 detector_threshold=2 effort=max excludefilter0=.fbExcludeFilterFile|true diff --git a/src/main/java/teetime/framework/Execution.java b/src/main/java/teetime/framework/Execution.java index 42007f50..67ed43a0 100644 --- a/src/main/java/teetime/framework/Execution.java +++ b/src/main/java/teetime/framework/Execution.java @@ -320,4 +320,8 @@ public final class Execution<T extends Configuration> implements UncaughtExcepti traversor.traverse(stage); return traversor.getVisitedStage(); } + + public IExceptionListenerFactory getFactory() { + return factory; + } } diff --git a/src/test/java/teetime/framework/ExecutionTest.java b/src/test/java/teetime/framework/ExecutionTest.java index a892873b..0e19ac37 100644 --- a/src/test/java/teetime/framework/ExecutionTest.java +++ b/src/test/java/teetime/framework/ExecutionTest.java @@ -36,7 +36,7 @@ import teetime.util.StopWatch; public class ExecutionTest { private static final long DELAY_IN_MS = 500; - private static final long ABSOLUTE_MAX_ERROR_IN_MS = 2; + private static final long ABSOLUTE_MAX_ERROR_IN_MS = 6; private Execution<TestConfig> execution; @@ -67,7 +67,7 @@ public class ExecutionTest { execution.executeBlocking(); watch.end(); - assertThat(watch.getDurationInMs() + ABSOLUTE_MAX_ERROR_IN_MS, is(greaterThanOrEqualTo(DELAY_IN_MS))); + assertThat(watch.getDurationInMs(), is(greaterThanOrEqualTo(DELAY_IN_MS - ABSOLUTE_MAX_ERROR_IN_MS))); } private static class TestConfig extends Configuration { @@ -179,18 +179,21 @@ public class ExecutionTest { @Test public void threadNameing() { NameConfig configuration = new NameConfig(); - Execution<NameConfig> execution = new Execution<NameConfig>(configuration); + new Execution<NameConfig>(configuration); // do not execute, but just initialize the execution assertThat(configuration.stageWithNamedThread.getOwningThread().getName(), is("TestName")); } private class NameConfig extends Configuration { - public InitialElementProducer<Object> stageWithNamedThread; + InitialElementProducer<Object> stageWithNamedThread; public NameConfig() { stageWithNamedThread = new InitialElementProducer<Object>(new Object()); + Sink<Object> sink = new Sink<Object>(); + addThreadableStage(stageWithNamedThread, "TestName"); - connectPorts(stageWithNamedThread.getOutputPort(), new Sink().getInputPort()); + + connectPorts(stageWithNamedThread.getOutputPort(), sink.getInputPort()); } } diff --git a/src/test/java/teetime/framework/exceptionHandling/ExceptionHandlingTest.java b/src/test/java/teetime/framework/exceptionHandling/ExceptionHandlingTest.java index f3869122..8b8e93d4 100644 --- a/src/test/java/teetime/framework/exceptionHandling/ExceptionHandlingTest.java +++ b/src/test/java/teetime/framework/exceptionHandling/ExceptionHandlingTest.java @@ -15,41 +15,16 @@ */ package teetime.framework.exceptionHandling; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import org.junit.Test; import teetime.framework.Execution; import teetime.framework.ExecutionException; -import teetime.framework.StageState; public class ExceptionHandlingTest { - private Execution<ExceptionTestConfiguration> execution; - - public ExceptionTestConfiguration newInstances() { - ExceptionTestConfiguration configuration = new ExceptionTestConfiguration(); - execution = new Execution<ExceptionTestConfiguration>(configuration, new TestListenerFactory()); - return configuration; - } - - public void exceptionPassingAndTermination() { - newInstances(); - execution.executeBlocking(); - assertEquals(TestListener.exceptionInvoked, 2); // listener did not kill thread too early - } - - public void terminatesAllStages() { - ExceptionTestConfiguration config = newInstances(); - execution.executeBlocking(); - assertThat(config.first.getCurrentState(), is(StageState.TERMINATED)); - assertThat(config.second.getCurrentState(), is(StageState.TERMINATED)); - assertThat(config.third.getCurrentState(), is(StageState.TERMINATED)); - } - @Test public void forAFewTimes() { for (int i = 0; i < 100; i++) { @@ -70,4 +45,33 @@ public class ExceptionHandlingTest { assertTrue(exceptionArised); } } + + public void exceptionPassingAndTermination() { + Execution<ExceptionTestConfiguration> execution = newInstances(); + + execution.executeBlocking(); + + fail(); + // TestListenerFactory factory = (TestListenerFactory) execution.getFactory(); + // assertThat(factory.getInstances(), hasSize(2)); + // assertEquals(factory.getInstances().get(0).getNumExceptionsInvoked(), 2); // listener did not kill thread too early + } + + public Execution<ExceptionTestConfiguration> newInstances() { + ExceptionTestConfiguration configuration = new ExceptionTestConfiguration(); + TestListenerFactory factory = new TestListenerFactory(); + return new Execution<ExceptionTestConfiguration>(configuration, factory); + } + + public void terminatesAllStages() { + Execution<ExceptionTestConfiguration> execution = newInstances(); + + execution.executeBlocking(); + + fail(); + // ExceptionTestConfiguration config = execution.getConfiguration(); + // assertThat(config.first.getCurrentState(), is(StageState.TERMINATED)); + // assertThat(config.second.getCurrentState(), is(StageState.TERMINATED)); + // assertThat(config.third.getCurrentState(), is(StageState.TERMINATED)); + } } diff --git a/src/test/java/teetime/framework/exceptionHandling/ExceptionTestConfiguration.java b/src/test/java/teetime/framework/exceptionHandling/ExceptionTestConfiguration.java index 0afaff22..ca231db9 100644 --- a/src/test/java/teetime/framework/exceptionHandling/ExceptionTestConfiguration.java +++ b/src/test/java/teetime/framework/exceptionHandling/ExceptionTestConfiguration.java @@ -34,4 +34,5 @@ public class ExceptionTestConfiguration extends Configuration { this.addThreadableStage(second); this.addThreadableStage(third); } + } diff --git a/src/test/java/teetime/framework/exceptionHandling/TestListener.java b/src/test/java/teetime/framework/exceptionHandling/TestListener.java index d2c75b29..7a8826a9 100644 --- a/src/test/java/teetime/framework/exceptionHandling/TestListener.java +++ b/src/test/java/teetime/framework/exceptionHandling/TestListener.java @@ -19,20 +19,20 @@ import teetime.framework.Stage; public class TestListener extends AbstractExceptionListener { - public static int exceptionInvoked = 0; - - public TestListener() { - TestListener.exceptionInvoked = 0; - } + private int numExceptionsInvoked; @Override public FurtherExecution onStageException(final Exception e, final Stage throwingStage) { - exceptionInvoked++; - if (exceptionInvoked == 2) { + numExceptionsInvoked++; + if (numExceptionsInvoked == 2) { return FurtherExecution.TERMINATE; } else { return FurtherExecution.CONTINUE; } } + public int getNumExceptionsInvoked() { + return numExceptionsInvoked; + } + } diff --git a/src/test/java/teetime/framework/exceptionHandling/TestListenerFactory.java b/src/test/java/teetime/framework/exceptionHandling/TestListenerFactory.java index 9561ccaf..270f4f0b 100644 --- a/src/test/java/teetime/framework/exceptionHandling/TestListenerFactory.java +++ b/src/test/java/teetime/framework/exceptionHandling/TestListenerFactory.java @@ -15,11 +15,22 @@ */ package teetime.framework.exceptionHandling; +import java.util.ArrayList; +import java.util.List; + public class TestListenerFactory implements IExceptionListenerFactory { + private final List<TestListener> instances = new ArrayList<TestListener>(); + @Override public AbstractExceptionListener createInstance() { - return new TestListener(); + TestListener listener = new TestListener(); + instances.add(listener); + return listener; + } + + public List<TestListener> getInstances() { + return instances; } } diff --git a/src/test/java/teetime/stage/basic/distributor/dynamic/DynamicDistributorTest.java b/src/test/java/teetime/stage/basic/distributor/dynamic/DynamicDistributorTest.java index c252c8c0..a8e569c1 100644 --- a/src/test/java/teetime/stage/basic/distributor/dynamic/DynamicDistributorTest.java +++ b/src/test/java/teetime/stage/basic/distributor/dynamic/DynamicDistributorTest.java @@ -29,7 +29,6 @@ import teetime.framework.Configuration; import teetime.framework.DynamicOutputPort; import teetime.framework.Execution; import teetime.framework.Stage; -import teetime.framework.exceptionHandling.TerminatingExceptionListenerFactory; import teetime.stage.CollectorSink; import teetime.stage.InitialElementProducer; import teetime.util.framework.port.PortAction; @@ -45,8 +44,7 @@ public class DynamicDistributorTest { List<PortAction<DynamicDistributor<Integer>>> inputActions = Arrays.asList(createAction, createAction, createAction, createAction, createAction); DynamicDistributorTestConfig<Integer> config = new DynamicDistributorTestConfig<Integer>(inputNumbers, inputActions); - Execution<DynamicDistributorTestConfig<Integer>> analysis = new Execution<DynamicDistributorTestConfig<Integer>>(config, - new TerminatingExceptionListenerFactory()); + Execution<DynamicDistributorTestConfig<Integer>> analysis = new Execution<DynamicDistributorTestConfig<Integer>>(config); analysis.executeBlocking(); @@ -65,8 +63,7 @@ public class DynamicDistributorTest { } DynamicDistributorTestConfig<Integer> config = new DynamicDistributorTestConfig<Integer>(inputNumbers, Arrays.asList(inputActions)); - Execution<DynamicDistributorTestConfig<Integer>> analysis = new Execution<DynamicDistributorTestConfig<Integer>>(config, - new TerminatingExceptionListenerFactory()); + Execution<DynamicDistributorTestConfig<Integer>> analysis = new Execution<DynamicDistributorTestConfig<Integer>>(config); analysis.executeBlocking(); @@ -97,8 +94,7 @@ public class DynamicDistributorTest { inputActions[5] = new RemovePortAction<Integer>(portContainer2); DynamicDistributorTestConfig<Integer> config = new DynamicDistributorTestConfig<Integer>(inputNumbers, Arrays.asList(inputActions)); - Execution<DynamicDistributorTestConfig<Integer>> analysis = new Execution<DynamicDistributorTestConfig<Integer>>(config, - new TerminatingExceptionListenerFactory()); + Execution<DynamicDistributorTestConfig<Integer>> analysis = new Execution<DynamicDistributorTestConfig<Integer>>(config); analysis.executeBlocking(); -- GitLab