Repository: nifi Updated Branches: refs/heads/master d94f7f0b5 -> 4f9219315
NIFI-787: Ensure that Mock Framework passes a ProcessContext to methods using the @OnStopped annotation (if the method takes a ProcessContext object as an argument); updated developer guide to reflect that the object is allowed as an argument Signed-off-by: joewitt <joew...@apache.org> Project: http://git-wip-us.apache.org/repos/asf/nifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/4f921931 Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/4f921931 Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/4f921931 Branch: refs/heads/master Commit: 4f921931599f29e1554ca44b106cccf45fc85e22 Parents: d94f7f0 Author: Mark Payne <marka...@hotmail.com> Authored: Wed Aug 19 17:04:51 2015 -0400 Committer: joewitt <joew...@apache.org> Committed: Wed Aug 19 20:11:01 2015 -0400 ---------------------------------------------------------------------- .../src/main/asciidoc/developer-guide.adoc | 7 ++- .../nifi/util/StandardProcessorTestRunner.java | 2 +- .../util/TestStandardProcessorTestRunner.java | 56 +++++++++++++++++++- 3 files changed, 61 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi/blob/4f921931/nifi-docs/src/main/asciidoc/developer-guide.adoc ---------------------------------------------------------------------- diff --git a/nifi-docs/src/main/asciidoc/developer-guide.adoc b/nifi-docs/src/main/asciidoc/developer-guide.adoc index bce4917..2e19cba 100644 --- a/nifi-docs/src/main/asciidoc/developer-guide.adoc +++ b/nifi-docs/src/main/asciidoc/developer-guide.adoc @@ -487,8 +487,11 @@ and all threads have returned from the `onTrigger` method. If such a method throws an Exception, a log message will be generated, and the Exception will otherwise be ignored; other methods with -this annotation will still be invoked. Methods with this annotation -must take zero arguments. +this annotation will still be invoked. +Methods with this annotation are permitted to take either 0 or 1 argument. If +an argument is used, it must be of type ConfigurationContext if the +component is a ReportingTask or of type ProcessContext if the +component is a Processor. ==== @OnShutdown http://git-wip-us.apache.org/repos/asf/nifi/blob/4f921931/nifi-mock/src/main/java/org/apache/nifi/util/StandardProcessorTestRunner.java ---------------------------------------------------------------------- diff --git a/nifi-mock/src/main/java/org/apache/nifi/util/StandardProcessorTestRunner.java b/nifi-mock/src/main/java/org/apache/nifi/util/StandardProcessorTestRunner.java index 048e2b9..f21b981 100644 --- a/nifi-mock/src/main/java/org/apache/nifi/util/StandardProcessorTestRunner.java +++ b/nifi-mock/src/main/java/org/apache/nifi/util/StandardProcessorTestRunner.java @@ -249,7 +249,7 @@ public class StandardProcessorTestRunner implements TestRunner { if (stopOnFinish) { try { - ReflectionUtils.invokeMethodsWithAnnotation(OnStopped.class, processor); + ReflectionUtils.invokeMethodsWithAnnotation(OnStopped.class, processor, context); } catch (final Exception e) { Assert.fail("Could not invoke methods annotated with @OnStopped annotation due to: " + e); } http://git-wip-us.apache.org/repos/asf/nifi/blob/4f921931/nifi-mock/src/test/java/org/apache/nifi/util/TestStandardProcessorTestRunner.java ---------------------------------------------------------------------- diff --git a/nifi-mock/src/test/java/org/apache/nifi/util/TestStandardProcessorTestRunner.java b/nifi-mock/src/test/java/org/apache/nifi/util/TestStandardProcessorTestRunner.java index 323a357..a15f6ee 100644 --- a/nifi-mock/src/test/java/org/apache/nifi/util/TestStandardProcessorTestRunner.java +++ b/nifi-mock/src/test/java/org/apache/nifi/util/TestStandardProcessorTestRunner.java @@ -16,6 +16,9 @@ */ package org.apache.nifi.util; +import static org.junit.Assert.assertEquals; + +import org.apache.nifi.annotation.lifecycle.OnStopped; import org.apache.nifi.processor.AbstractProcessor; import org.apache.nifi.processor.ProcessContext; import org.apache.nifi.processor.ProcessSession; @@ -23,25 +26,47 @@ import org.apache.nifi.processor.exception.ProcessException; import org.junit.Ignore; import org.junit.Test; -@Ignore("This should not be enabled until we actually fail processor unit tests for using deprecated methods, which should happen in 0.1.0") public class TestStandardProcessorTestRunner { + @Test + public void testProcessContextPassedToOnStoppedMethods() { + final ProcessorWithOnStop proc = new ProcessorWithOnStop(); + final TestRunner runner = TestRunners.newTestRunner(proc); + + assertEquals(0, proc.getOnStoppedCallsWithContext()); + assertEquals(0, proc.getOnStoppedCallsWithoutContext()); + + runner.run(1, false); + + assertEquals(0, proc.getOnStoppedCallsWithContext()); + assertEquals(0, proc.getOnStoppedCallsWithoutContext()); + + runner.run(1, true); + + assertEquals(1, proc.getOnStoppedCallsWithContext()); + assertEquals(1, proc.getOnStoppedCallsWithoutContext()); + } + @Test(expected = AssertionError.class) + @Ignore("This should not be enabled until we actually fail processor unit tests for using deprecated methods") public void testFailOnDeprecatedTypeAnnotation() { new StandardProcessorTestRunner(new DeprecatedAnnotation()); } @Test + @Ignore("This should not be enabled until we actually fail processor unit tests for using deprecated methods") public void testDoesNotFailOnNonDeprecatedTypeAnnotation() { new StandardProcessorTestRunner(new NewAnnotation()); } @Test(expected = AssertionError.class) + @Ignore("This should not be enabled until we actually fail processor unit tests for using deprecated methods") public void testFailOnDeprecatedMethodAnnotation() { new StandardProcessorTestRunner(new DeprecatedMethodAnnotation()); } @Test + @Ignore("This should not be enabled until we actually fail processor unit tests for using deprecated methods") public void testDoesNotFailOnNonDeprecatedMethodAnnotation() { new StandardProcessorTestRunner(new NewMethodAnnotation()); } @@ -87,4 +112,33 @@ public class TestStandardProcessorTestRunner { public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { } } + + private static class ProcessorWithOnStop extends AbstractProcessor { + + private int callsWithContext = 0; + private int callsWithoutContext = 0; + + @OnStopped + public void onStoppedWithContext(final ProcessContext procContext) { + callsWithContext++; + } + + @OnStopped + public void onStoppedWithoutContext() { + callsWithoutContext++; + } + + public int getOnStoppedCallsWithContext() { + return callsWithContext; + } + + public int getOnStoppedCallsWithoutContext() { + return callsWithoutContext; + } + + @Override + public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException { + } + + } }