You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by du...@apache.org on 2019/04/09 10:02:06 UTC

[brooklyn-server] branch master updated: Add support for finally block in TestCase entity

This is an automated email from the ASF dual-hosted git repository.

duncangrant pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-server.git


The following commit(s) were added to refs/heads/master by this push:
     new 75874a8  Add support for finally block in TestCase entity
     new b71ae49  Merge pull request #1063 from tbouron/feature/test-case-finally
75874a8 is described below

commit 75874a8998888a908a53d84a1feee9e98792e21d
Author: Thomas Bouron <th...@cloudsoftcorp.com>
AuthorDate: Tue Apr 9 10:18:46 2019 +0100

    Add support for finally block in TestCase entity
---
 .../apache/brooklyn/test/framework/TestCase.java   |  7 +++++
 .../brooklyn/test/framework/TestCaseImpl.java      | 21 ++++++++-------
 .../brooklyn/test/framework/TestCaseTest.java      | 31 ++++++++++++++++++++++
 3 files changed, 50 insertions(+), 9 deletions(-)

diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestCase.java b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestCase.java
index cd53993..2fa3bfe 100644
--- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestCase.java
+++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestCase.java
@@ -39,6 +39,13 @@ public interface TestCase extends TargetableTestComponent {
             .runtimeInheritance(BasicConfigInheritance.NOT_REINHERITED)
             .build();
 
+    @SuppressWarnings("serial")
+    ConfigKey<EntitySpec<?>> ON_FINALLY_SPEC = ConfigKeys.builder(new TypeToken<EntitySpec<?>>() {})
+            .name("on.finally.spec")
+            .description("Spec of entity to instantiate (and start, if startable) after a test-case either passes or fails")
+            .runtimeInheritance(BasicConfigInheritance.NOT_REINHERITED)
+            .build();
+
     ConfigKey<Boolean> CONTINUE_ON_FAILURE = ConfigKeys.builder(Boolean.class)
             .name("continueOnFailure")
             .description("Whether to continue executing subsequent children if an earlier child fails")
diff --git a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestCaseImpl.java b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestCaseImpl.java
index 2b7351a..bd90543 100644
--- a/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestCaseImpl.java
+++ b/test-framework/src/main/java/org/apache/brooklyn/test/framework/TestCaseImpl.java
@@ -24,6 +24,7 @@ import java.util.List;
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.entity.EntitySpec;
 import org.apache.brooklyn.api.location.Location;
+import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.entity.Attributes;
 import org.apache.brooklyn.core.entity.lifecycle.Lifecycle;
 import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic;
@@ -77,7 +78,7 @@ public class TestCaseImpl extends TargetableTestComponentImpl implements TestCas
         } catch (Throwable t) {
             Exceptions.propagateIfInterrupt(t);
             try {
-                execOnErrorSpec();
+                execOnSpec(ON_ERROR_SPEC);
             } catch (Throwable t2) {
                 LOG.error("Problem executing on-error for "+this, t2);
                 Exceptions.propagateIfInterrupt(t2);
@@ -85,6 +86,8 @@ public class TestCaseImpl extends TargetableTestComponentImpl implements TestCas
             sensors().set(Attributes.SERVICE_UP, false);
             ServiceStateLogic.setExpectedState(this, Lifecycle.ON_FIRE);
             throw Exceptions.propagate(t);
+        } finally {
+            execOnSpec(ON_FINALLY_SPEC);
         }
     }
 
@@ -134,16 +137,16 @@ public class TestCaseImpl extends TargetableTestComponentImpl implements TestCas
         start(locations);
     }
 
-    protected void execOnErrorSpec() {
-        EntitySpec<?> onErrorSpec = config().get(ON_ERROR_SPEC);
-        if (onErrorSpec != null) {
-            LOG.info("Creating and starting on-error child entity {} for {}", onErrorSpec.getType().getSimpleName(), this);
-            Entity onErrorEntity = addChild(onErrorSpec);
-            if (onErrorEntity instanceof Startable){
-                ((Startable) onErrorEntity).start(getLocations());
+    protected void execOnSpec(ConfigKey<EntitySpec<?>> configKey) {
+        EntitySpec<?> spec = config().get(configKey);
+        if (spec != null) {
+            LOG.info("Creating and starting {} child entity {} for {}", configKey.getName(), spec.getType().getSimpleName(), this);
+            Entity onEntity = addChild(spec);
+            if (onEntity instanceof Startable){
+                ((Startable) onEntity).start(getLocations());
             }
         } else {
-            LOG.debug("No on-error spec for {}", this);
+            LOG.debug("No {} for {}", configKey.getName(), this);
         }
     }
 }
diff --git a/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestCaseTest.java b/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestCaseTest.java
index d599f9f..7f96181 100644
--- a/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestCaseTest.java
+++ b/test-framework/src/test/java/org/apache/brooklyn/test/framework/TestCaseTest.java
@@ -64,6 +64,18 @@ public class TestCaseTest extends BrooklynAppUnitTestSupport {
                 { Boolean.TRUE }
         };
     }
+
+    @DataProvider(name = "onFinallyPermutations")
+    public Object[][] onFinallyPermutations() {
+        return new Object[][] {
+                { Boolean.FALSE, (Boolean)null },
+                { Boolean.FALSE, Boolean.TRUE },
+                { Boolean.FALSE, Boolean.FALSE },
+                { Boolean.TRUE, (Boolean)null },
+                { Boolean.TRUE, Boolean.TRUE },
+                { Boolean.TRUE, Boolean.FALSE }
+        };
+    }
     
     @Test(dataProvider = "continueOnFailurePermutations")
     public void testSucceedsWhenEmpty(Boolean continueOnFailure) throws Exception {
@@ -122,6 +134,25 @@ public class TestCaseTest extends BrooklynAppUnitTestSupport {
         TestEntity onErrEntity = (TestEntity) Iterables.tryFind(testCase.getChildren(), EntityPredicates.displayNameEqualTo("onerr")).get();
         assertEquals(onErrEntity.getCallHistory(), ImmutableList.of("start"));
     }
+
+    @Test(dataProvider = "onFinallyPermutations")
+    public void testCallsOnFinallyEntity(Boolean failOnStart, Boolean continueOnFailure) throws Exception {
+        TestCase testCase = app.createAndManageChild(EntitySpec.create(TestCase.class)
+                .configure(TestCase.CONTINUE_ON_FAILURE, continueOnFailure)
+                .configure(TestCase.ON_FINALLY_SPEC, EntitySpec.create(TestEntity.class).displayName("onfinally"))
+                .child(EntitySpec.create(FailingEntity.class)
+                        .configure(FailingEntity.FAIL_ON_START, failOnStart)));
+
+        try {
+            app.start(locs);
+            assertTestCaseSucceeds(testCase);
+        } catch (Throwable t) {
+            Asserts.expectedFailureContains(t, "Simulating entity start failure for test");
+        }
+
+        TestEntity onFinallyEntity = (TestEntity) Iterables.tryFind(testCase.getChildren(), EntityPredicates.displayNameEqualTo("onfinally")).get();
+        assertEquals(onFinallyEntity.getCallHistory(), ImmutableList.of("start"));
+    }
     
     @Test
     public void testOnErrorEntityNotInherited() throws Exception {