brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [1/2] brooklyn-server git commit: Tidy tests to always terminate ManagementContext
Date Tue, 11 Oct 2016 20:31:23 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master e222bdb2f -> 01fe89134


Tidy tests to always terminate ManagementContext


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/b053b67b
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/b053b67b
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/b053b67b

Branch: refs/heads/master
Commit: b053b67bb10d205b982fb00f08a498d288c19521
Parents: 057b349
Author: Aled Sage <aled.sage@gmail.com>
Authored: Wed Oct 5 15:45:24 2016 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Wed Oct 5 20:59:34 2016 +0100

----------------------------------------------------------------------
 .../catalog/CatalogYamlLocationTest.java        | 13 ++-
 .../entity/AbstractApplicationLegacyTest.java   | 25 +++++-
 .../mgmt/internal/LocalUsageManagerTest.java    |  6 +-
 .../objs/BasicSpecParameterFromListTest.java    | 12 +--
 .../core/test/BrooklynMgmtUnitTestSupport.java  | 21 ++++-
 .../CleanOrphanedLocationsIntegrationTest.java  | 86 +++++++++++++-------
 .../emptyDirKeeper                              |  0
 .../java/org/apache/brooklyn/cli/CliTest.java   | 81 +++++++++++-------
 .../SoftwareProcessStopsDuringStartTest.java    |  2 +
 .../base/VanillaSoftwareProcessTest.java        |  2 +
 .../base/VanillaWindowsProcessTest.java         |  2 +
 11 files changed, 170 insertions(+), 80 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b053b67b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogYamlLocationTest.java
----------------------------------------------------------------------
diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogYamlLocationTest.java
b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogYamlLocationTest.java
index 091e972..5cf7443 100644
--- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogYamlLocationTest.java
+++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/catalog/CatalogYamlLocationTest.java
@@ -65,10 +65,15 @@ public class CatalogYamlLocationTest extends AbstractYamlTest {
         return false;
     }
 
-    @AfterMethod
-    public void tearDown() {
-        for (RegisteredType ci : mgmt().getTypeRegistry().getMatching(RegisteredTypePredicates.IS_LOCATION))
{
-            mgmt().getCatalog().deleteCatalogItem(ci.getSymbolicName(), ci.getVersion());
+    @AfterMethod(alwaysRun=true)
+    @Override
+    public void tearDown() throws Exception {
+        try {
+            for (RegisteredType ci : mgmt().getTypeRegistry().getMatching(RegisteredTypePredicates.IS_LOCATION))
{
+                mgmt().getCatalog().deleteCatalogItem(ci.getSymbolicName(), ci.getVersion());
+            }
+        } finally {
+            super.tearDown();
         }
     }
     

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b053b67b/core/src/test/java/org/apache/brooklyn/core/entity/AbstractApplicationLegacyTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/entity/AbstractApplicationLegacyTest.java
b/core/src/test/java/org/apache/brooklyn/core/entity/AbstractApplicationLegacyTest.java
index cabff36..d1d4dc3 100644
--- a/core/src/test/java/org/apache/brooklyn/core/entity/AbstractApplicationLegacyTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/entity/AbstractApplicationLegacyTest.java
@@ -23,20 +23,24 @@ import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 
 import java.util.List;
+import java.util.Set;
 
 import org.apache.brooklyn.api.entity.EntitySpec;
 import org.apache.brooklyn.api.location.LocationSpec;
+import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.core.location.SimulatedLocation;
 import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport;
 import org.apache.brooklyn.core.test.entity.TestApplication;
 import org.apache.brooklyn.core.test.entity.TestApplicationImpl;
 import org.apache.brooklyn.core.test.entity.TestEntity;
 import org.apache.brooklyn.core.test.entity.TestEntityImpl;
+import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Sets;
 
 /**
  * Tests the deprecated use of AbstractAppliation, where its constructor is called directly.
@@ -47,6 +51,7 @@ public class AbstractApplicationLegacyTest extends BrooklynAppUnitTestSupport
{
 
     private SimulatedLocation loc;
     private List<SimulatedLocation> locs;
+    private Set<ManagementContext> extraMgmtContexts;
     
     @BeforeMethod(alwaysRun=true)
     @Override
@@ -54,9 +59,26 @@ public class AbstractApplicationLegacyTest extends BrooklynAppUnitTestSupport
{
         super.setUp();
         loc = mgmt.getLocationManager().createLocation(LocationSpec.create(SimulatedLocation.class));
         locs = ImmutableList.of(loc);
+        extraMgmtContexts = Sets.newLinkedHashSet();
     }
     
-    // App and its children will be implicitly managed on first effector call on app
+    @AfterMethod(alwaysRun=true)
+    @Override
+    public void tearDown() throws Exception {
+        try {
+            super.tearDown();
+        } finally {
+            for (ManagementContext extraMgmt : extraMgmtContexts) {
+                if (extraMgmt != null) Entities.destroyAll(extraMgmt);
+            }
+            extraMgmtContexts = null;
+            locs = null;
+            loc = null;
+        }
+    }
+    
+    // App and its children will be implicitly managed on first effector call on app.
+    // Will cause the implicit creation of a new management context!
     @Test
     public void testStartAndStopUnmanagedAppAutomanagesTheAppAndChildren() throws Exception
{
         // deliberately unmanaged
@@ -66,6 +88,7 @@ public class AbstractApplicationLegacyTest extends BrooklynAppUnitTestSupport
{
         assertFalse(Entities.isManaged(child));
         
         app2.invoke(AbstractApplication.START, ImmutableMap.of("locations", locs)).get();
+        extraMgmtContexts.add(app2.getManagementContext());
         assertTrue(Entities.isManaged(app2));
         assertTrue(Entities.isManaged(child));
         assertEquals(child.getCallHistory(), ImmutableList.of("start"));

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b053b67b/core/src/test/java/org/apache/brooklyn/core/mgmt/internal/LocalUsageManagerTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/mgmt/internal/LocalUsageManagerTest.java
b/core/src/test/java/org/apache/brooklyn/core/mgmt/internal/LocalUsageManagerTest.java
index e00788c..1851d72 100644
--- a/core/src/test/java/org/apache/brooklyn/core/mgmt/internal/LocalUsageManagerTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/mgmt/internal/LocalUsageManagerTest.java
@@ -67,7 +67,7 @@ public class LocalUsageManagerTest extends BrooklynAppUnitTestSupport {
     public void testAddUsageListenerInstance() throws Exception {
         BrooklynProperties brooklynProperties = BrooklynProperties.Factory.newEmpty();
         brooklynProperties.put(UsageManager.USAGE_LISTENERS, ImmutableList.of(new RecordingStaticUsageListener()));
-        mgmt = LocalManagementContextForTests.newInstance(brooklynProperties);
+        replaceManagementContext(LocalManagementContextForTests.newInstance(brooklynProperties));
         assertUsageListenerCalledWhenApplicationStarted();
     }
 
@@ -75,7 +75,7 @@ public class LocalUsageManagerTest extends BrooklynAppUnitTestSupport {
     public void testAddUsageListenerViaProperties() throws Exception {
         BrooklynProperties brooklynProperties = BrooklynProperties.Factory.newEmpty();
         brooklynProperties.put(UsageManager.USAGE_LISTENERS, RecordingStaticUsageListener.class.getName());
-        mgmt = LocalManagementContextForTests.newInstance(brooklynProperties);
+        replaceManagementContext(LocalManagementContextForTests.newInstance(brooklynProperties));
         assertUsageListenerCalledWhenApplicationStarted();
     }
 
@@ -83,7 +83,7 @@ public class LocalUsageManagerTest extends BrooklynAppUnitTestSupport {
     public void testErrorWhenConfiguredClassIsNotAUsageListener() {
         BrooklynProperties brooklynProperties = BrooklynProperties.Factory.newEmpty();
         brooklynProperties.put(UsageManager.USAGE_LISTENERS, Integer.class.getName());
-        mgmt = LocalManagementContextForTests.newInstance(brooklynProperties);
+        replaceManagementContext(LocalManagementContextForTests.newInstance(brooklynProperties));
     }
 
     private void assertUsageListenerCalledWhenApplicationStarted() {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b053b67b/core/src/test/java/org/apache/brooklyn/core/objs/BasicSpecParameterFromListTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/objs/BasicSpecParameterFromListTest.java
b/core/src/test/java/org/apache/brooklyn/core/objs/BasicSpecParameterFromListTest.java
index 847e619..dd893d9 100644
--- a/core/src/test/java/org/apache/brooklyn/core/objs/BasicSpecParameterFromListTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/objs/BasicSpecParameterFromListTest.java
@@ -25,14 +25,12 @@ import static org.testng.Assert.assertTrue;
 
 import java.util.List;
 
-import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.api.mgmt.classloading.BrooklynClassLoadingContext;
 import org.apache.brooklyn.api.objs.SpecParameter;
 import org.apache.brooklyn.config.ConfigKey;
 import org.apache.brooklyn.core.mgmt.classloading.JavaBrooklynClassLoadingContext;
-import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
+import org.apache.brooklyn.core.test.BrooklynMgmtUnitTestSupport;
 import org.apache.brooklyn.util.text.StringPredicates;
-import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 import com.google.common.base.Predicate;
@@ -42,13 +40,7 @@ import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Iterables;
 import com.google.common.reflect.TypeToken;
 
-public class BasicSpecParameterFromListTest {
-    private ManagementContext mgmt;
-
-    @BeforeMethod(alwaysRun=true)
-    public void setUp() {
-        mgmt = LocalManagementContextForTests.newInstance();
-    }
+public class BasicSpecParameterFromListTest extends BrooklynMgmtUnitTestSupport {
 
     @Test
     public void testInlineName() {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b053b67b/core/src/test/java/org/apache/brooklyn/core/test/BrooklynMgmtUnitTestSupport.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/test/BrooklynMgmtUnitTestSupport.java
b/core/src/test/java/org/apache/brooklyn/core/test/BrooklynMgmtUnitTestSupport.java
index ee2ab79..5a000d2 100644
--- a/core/src/test/java/org/apache/brooklyn/core/test/BrooklynMgmtUnitTestSupport.java
+++ b/core/src/test/java/org/apache/brooklyn/core/test/BrooklynMgmtUnitTestSupport.java
@@ -18,6 +18,7 @@
  */
 package org.apache.brooklyn.core.test;
 
+import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.core.entity.Entities;
 import org.apache.brooklyn.core.internal.BrooklynProperties;
 import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
@@ -53,17 +54,29 @@ public class BrooklynMgmtUnitTestSupport {
     @AfterMethod(alwaysRun=true)
     public void tearDown() throws Exception {
         try {
+            destroyManagementContextSafely(mgmt);
+        } finally {
+            mgmt = null;
+        }
+    }
+
+    protected void replaceManagementContext(ManagementContext newMgmt) {
+        destroyManagementContextSafely(mgmt);
+        mgmt = (ManagementContextInternal) newMgmt;
+    }
+    
+    protected void destroyManagementContextSafely(ManagementContext mgmt) {
+        try {
             if (mgmt != null) Entities.destroyAll(mgmt);
         } catch (Throwable t) {
-            LOG.error("Caught exception in tearDown method", t);
+            LOG.error("Caught exception destroying management context "+mgmt, t);
+            // If failing during teardown...
             // we should fail here, except almost always that masks a primary failure in
the test itself,
             // so it would be extremely unhelpful to do so. if we could check if test has
not already failed,
             // that would be ideal, but i'm not sure if that's possible with TestNG. ?
-        } finally {
-            mgmt = null;
         }
     }
-
+    
     protected BrooklynProperties getBrooklynProperties() {
         return null;
     }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b053b67b/launcher/src/test/java/org/apache/brooklyn/launcher/CleanOrphanedLocationsIntegrationTest.java
----------------------------------------------------------------------
diff --git a/launcher/src/test/java/org/apache/brooklyn/launcher/CleanOrphanedLocationsIntegrationTest.java
b/launcher/src/test/java/org/apache/brooklyn/launcher/CleanOrphanedLocationsIntegrationTest.java
index 2dbb971..b1f0430 100644
--- a/launcher/src/test/java/org/apache/brooklyn/launcher/CleanOrphanedLocationsIntegrationTest.java
+++ b/launcher/src/test/java/org/apache/brooklyn/launcher/CleanOrphanedLocationsIntegrationTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.brooklyn.launcher;
 
+import java.io.File;
 import java.util.Set;
 
 import org.apache.brooklyn.api.mgmt.ManagementContext;
@@ -25,9 +26,9 @@ import org.apache.brooklyn.api.mgmt.ha.HighAvailabilityMode;
 import org.apache.brooklyn.api.mgmt.rebind.PersistenceExceptionHandler;
 import org.apache.brooklyn.api.mgmt.rebind.RebindManager;
 import org.apache.brooklyn.api.mgmt.rebind.mementos.BrooklynMementoRawData;
+import org.apache.brooklyn.core.entity.Entities;
 import org.apache.brooklyn.core.internal.BrooklynProperties;
 import org.apache.brooklyn.core.mgmt.ha.OsgiManager;
-import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext;
 import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal;
 import org.apache.brooklyn.core.mgmt.persist.BrooklynMementoPersisterToObjectStore;
 import org.apache.brooklyn.core.mgmt.persist.BrooklynPersistenceUtils;
@@ -37,54 +38,85 @@ import org.apache.brooklyn.core.mgmt.rebind.PersistenceExceptionHandlerImpl;
 import org.apache.brooklyn.core.mgmt.rebind.RebindManagerImpl;
 import org.apache.brooklyn.core.server.BrooklynServerConfig;
 import org.apache.brooklyn.core.server.BrooklynServerPaths;
+import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
 import org.apache.brooklyn.test.Asserts;
 import org.apache.brooklyn.util.collections.MutableSet;
+import org.apache.brooklyn.util.os.Os;
 import org.apache.brooklyn.util.time.Duration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import com.google.common.collect.Sets;
+
 public class CleanOrphanedLocationsIntegrationTest extends AbstractCleanOrphanedStateTest
{
 
-    private String PERSISTED_STATE_PATH_WITH_ORPHANED_LOCATIONS = "/orphaned-locations-test-data/data-with-orphaned-locations";
-    private String PERSISTED_STATE_PATH_WITH_MULTIPLE_LOCATIONS_OCCURRENCE = "/orphaned-locations-test-data/fake-multiple-location-for-multiple-search-tests";
-    private String PERSISTED_STATE_PATH_WITHOUT_ORPHANED_LOCATIONS = "/orphaned-locations-test-data/data-without-orphaned-locations";
-    private String PERSISTED_STATE_DESTINATION_PATH = "/orphaned-locations-test-data/copy-persisted-state-destination";
+    private static final Logger LOG = LoggerFactory.getLogger(CleanOrphanedLocationsIntegrationTest.class);
 
+    private final String PERSISTED_STATE_PATH_WITH_ORPHANED_LOCATIONS = "/orphaned-locations-test-data/data-with-orphaned-locations";
+    private final String PERSISTED_STATE_PATH_WITH_MULTIPLE_LOCATIONS_OCCURRENCE = "/orphaned-locations-test-data/fake-multiple-location-for-multiple-search-tests";
+    private final String PERSISTED_STATE_PATH_WITHOUT_ORPHANED_LOCATIONS = "/orphaned-locations-test-data/data-without-orphaned-locations";
 
     private String persistenceDirWithOrphanedLocations;
     private String persistenceDirWithoutOrphanedLocations;
     private String persistenceDirWithMultipleLocationsOccurrence;
-    private String destinationDir;
-    private ManagementContext managementContext;
+    private File destinationDir;
+    private Set<ManagementContext> mgmts;
+    private ManagementContext mgmt;
 
     @BeforeMethod(alwaysRun = true)
-    public void initialize() throws Exception {
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
         persistenceDirWithOrphanedLocations = getClass().getResource(PERSISTED_STATE_PATH_WITH_ORPHANED_LOCATIONS).getFile();
         persistenceDirWithoutOrphanedLocations = getClass().getResource(PERSISTED_STATE_PATH_WITHOUT_ORPHANED_LOCATIONS).getFile();
         persistenceDirWithMultipleLocationsOccurrence = getClass().getResource(PERSISTED_STATE_PATH_WITH_MULTIPLE_LOCATIONS_OCCURRENCE).getFile();
 
-        destinationDir = getClass().getResource(PERSISTED_STATE_DESTINATION_PATH).getFile();
+        destinationDir = Os.newTempDir(getClass());
+        
+        mgmts = Sets.newLinkedHashSet();
     }
 
+    @AfterMethod(alwaysRun = true)
+    @Override
+    public void tearDown() throws Exception {
+        super.tearDown();
+        
+        // contents of method copied from BrooklynMgmtUnitTestSupport
+        for (ManagementContext mgmt : mgmts) {
+            try {
+                if (mgmt != null) Entities.destroyAll(mgmt);
+            } catch (Throwable t) {
+                LOG.error("Caught exception in tearDown method", t);
+                // we should fail here, except almost always that masks a primary failure
in the test itself,
+                // so it would be extremely unhelpful to do so. if we could check if test
has not already failed,
+                // that would be ideal, but i'm not sure if that's possible with TestNG.
?
+            }
+        }
+        if (destinationDir != null) Os.deleteRecursively(destinationDir);
+        mgmts.clear();
+    }
+    
     private void initManagementContextAndPersistence(String persistenceDir) {
-
         BrooklynProperties brooklynProperties = BrooklynProperties.Factory.builderDefault().build();
         brooklynProperties.put(BrooklynServerConfig.MGMT_BASE_DIR.getName(), "");
         brooklynProperties.put(BrooklynServerConfig.OSGI_CACHE_DIR, "target/" + BrooklynServerConfig.OSGI_CACHE_DIR.getDefaultValue());
 
-        managementContext = new LocalManagementContext(brooklynProperties);
-
+        mgmt = LocalManagementContextForTests.newInstance(brooklynProperties);
+        mgmts.add(mgmt);
+        
         persistenceDir = BrooklynServerPaths.newMainPersistencePathResolver(brooklynProperties).dir(persistenceDir).resolve();
-        PersistenceObjectStore objectStore = BrooklynPersistenceUtils.newPersistenceObjectStore(managementContext,
null, persistenceDir,
+        PersistenceObjectStore objectStore = BrooklynPersistenceUtils.newPersistenceObjectStore(mgmt,
null, persistenceDir,
                 PersistMode.AUTO, HighAvailabilityMode.HOT_STANDBY);
 
         BrooklynMementoPersisterToObjectStore persister = new BrooklynMementoPersisterToObjectStore(
                 objectStore,
-                ((ManagementContextInternal)managementContext).getBrooklynProperties(),
-                managementContext.getCatalogClassLoader());
+                ((ManagementContextInternal)mgmt).getBrooklynProperties(),
+                mgmt.getCatalogClassLoader());
 
-        RebindManager rebindManager = managementContext.getRebindManager();
+        RebindManager rebindManager = mgmt.getRebindManager();
 
         PersistenceExceptionHandler persistenceExceptionHandler = PersistenceExceptionHandlerImpl.builder().build();
         ((RebindManagerImpl) rebindManager).setPeriodicPersistPeriod(Duration.ONE_SECOND);
@@ -99,7 +131,7 @@ public class CleanOrphanedLocationsIntegrationTest extends AbstractCleanOrphaned
         );
 
         initManagementContextAndPersistence(persistenceDirWithOrphanedLocations);
-        BrooklynMementoRawData mementoRawData = managementContext.getRebindManager().retrieveMementoRawData();
+        BrooklynMementoRawData mementoRawData = mgmt.getRebindManager().retrieveMementoRawData();
 
         assertTransformDeletes(new Deletions().locations(orphanedLocations), mementoRawData);
     }
@@ -107,7 +139,7 @@ public class CleanOrphanedLocationsIntegrationTest extends AbstractCleanOrphaned
     @Test
     public void testSelectionWithoutOrphanedLocationsInData() throws Exception {
         initManagementContextAndPersistence(persistenceDirWithoutOrphanedLocations);
-        BrooklynMementoRawData mementoRawData = managementContext.getRebindManager().retrieveMementoRawData();
+        BrooklynMementoRawData mementoRawData = mgmt.getRebindManager().retrieveMementoRawData();
 
         assertTransformIsNoop(mementoRawData);
     }
@@ -121,19 +153,22 @@ public class CleanOrphanedLocationsIntegrationTest extends AbstractCleanOrphaned
                 .persistenceDir(persistenceDirWithOrphanedLocations)
                 .highAvailabilityMode(HighAvailabilityMode.DISABLED);
 
+        ManagementContext mgmtForCleaning = null;
         try {
-            launcher.cleanOrphanedState(destinationDir, null);
+            launcher.cleanOrphanedState(destinationDir.getAbsolutePath(), null);
+            mgmtForCleaning = launcher.getManagementContext();
         } finally {
             launcher.terminate();
+            if (mgmtForCleaning != null) Entities.destroyAll(mgmtForCleaning);
         }
 
-        initManagementContextAndPersistence(destinationDir);
-        BrooklynMementoRawData mementoRawDataFromCleanedState = managementContext.getRebindManager().retrieveMementoRawData();
+        initManagementContextAndPersistence(destinationDir.getAbsolutePath());
+        BrooklynMementoRawData mementoRawDataFromCleanedState = mgmt.getRebindManager().retrieveMementoRawData();
         Asserts.assertTrue(mementoRawDataFromCleanedState.getEntities().size() != 0);
         Asserts.assertTrue(mementoRawDataFromCleanedState.getLocations().size() != 0);
 
         initManagementContextAndPersistence(persistenceDirWithoutOrphanedLocations);
-        BrooklynMementoRawData mementoRawData = managementContext.getRebindManager().retrieveMementoRawData();
+        BrooklynMementoRawData mementoRawData = mgmt.getRebindManager().retrieveMementoRawData();
 
         assertRawData(mementoRawData, mementoRawDataFromCleanedState);
     }
@@ -141,13 +176,8 @@ public class CleanOrphanedLocationsIntegrationTest extends AbstractCleanOrphaned
     @Test
     public void testMultipleLocationOccurrenceInEntity() throws Exception {
         initManagementContextAndPersistence(persistenceDirWithMultipleLocationsOccurrence);
-        BrooklynMementoRawData mementoRawData = managementContext.getRebindManager().retrieveMementoRawData();
+        BrooklynMementoRawData mementoRawData = mgmt.getRebindManager().retrieveMementoRawData();
         
         assertTransformIsNoop(mementoRawData);
     }
-
-    @AfterMethod
-    public void cleanCopiedPersistedState() {
-
-    }
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b053b67b/launcher/src/test/resources/orphaned-locations-test-data/copy-persisted-state-destination/emptyDirKeeper
----------------------------------------------------------------------
diff --git a/launcher/src/test/resources/orphaned-locations-test-data/copy-persisted-state-destination/emptyDirKeeper
b/launcher/src/test/resources/orphaned-locations-test-data/copy-persisted-state-destination/emptyDirKeeper
deleted file mode 100644
index e69de29..0000000

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b053b67b/server-cli/src/test/java/org/apache/brooklyn/cli/CliTest.java
----------------------------------------------------------------------
diff --git a/server-cli/src/test/java/org/apache/brooklyn/cli/CliTest.java b/server-cli/src/test/java/org/apache/brooklyn/cli/CliTest.java
index c069f9e..c972bbf 100644
--- a/server-cli/src/test/java/org/apache/brooklyn/cli/CliTest.java
+++ b/server-cli/src/test/java/org/apache/brooklyn/cli/CliTest.java
@@ -31,7 +31,6 @@ import java.io.InputStream;
 import java.io.PrintStream;
 import java.util.Collection;
 import java.util.List;
-import java.util.Set;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
@@ -60,8 +59,6 @@ import org.apache.brooklyn.core.entity.StartableApplication;
 import org.apache.brooklyn.core.entity.factory.ApplicationBuilder;
 import org.apache.brooklyn.core.entity.trait.Startable;
 import org.apache.brooklyn.core.location.SimulatedLocation;
-import org.apache.brooklyn.core.mgmt.internal.LocalManagementContext;
-import org.apache.brooklyn.core.mgmt.internal.LocalManagementContextRegistry;
 import org.apache.brooklyn.core.objs.proxy.EntityProxy;
 import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
 import org.apache.brooklyn.test.Asserts;
@@ -80,12 +77,12 @@ import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import com.google.common.base.Function;
 import com.google.common.base.Joiner;
 import com.google.common.base.Splitter;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
 import com.google.common.io.Files;
 
 import groovy.lang.GroovyClassLoader;
@@ -103,7 +100,7 @@ public class CliTest {
     private ExecutorService executor;
     private StartableApplication app;
     private List<File> filesToDelete;
-    
+
     private static volatile ExampleEntity exampleEntity;
 
     // static so that they can be set from the static classes ExampleApp and ExampleEntity
@@ -130,6 +127,7 @@ public class CliTest {
                 file.delete();
             }
         }
+        app = null;
     }
     
     @Test
@@ -222,15 +220,19 @@ public class CliTest {
     public void testStopAllApplications() throws Exception {
         LaunchCommand launchCommand = new Main.LaunchCommand();
         ExampleApp app = new ExampleApp();
+        ManagementContext mgmt = null;
         try {
             Entities.startManagement(app);
+            mgmt = app.getManagementContext();
             app.start(ImmutableList.of(new SimulatedLocation()));
             assertTrue(app.running);
             
             launchCommand.stopAllApps(ImmutableList.of(app));
             assertFalse(app.running);
         } finally {
-            Entities.destroyAll(app.getManagementContext());
+            // Stopping the app will make app.getManagementContext return the "NonDeploymentManagementContext";
+            // hence we've retrieved it before calling stopAllApps()
+            if (mgmt != null) Entities.destroyAll(mgmt);
         }
     }
     
@@ -426,6 +428,7 @@ public class CliTest {
         runAddBomToCatalog(3);
     }
 
+    // This method assumes that only one test at a time will be running in the JVM!
     protected void runAddBomToCatalog(int numBoms) throws Exception {
         final List<String> bomFiles = Lists.newArrayList();
         final List<String> itemSymbolicNames = Lists.newArrayList();
@@ -437,24 +440,21 @@ public class CliTest {
             itemSymbolicNames.add(itemName+":"+itemVersion);
         }
 
-        final Set<LocalManagementContext> origMgmts = LocalManagementContextRegistry.getInstances();
-        
         Cli<BrooklynCommand> cli = buildCli();
         BrooklynCommand command = cli.parse("launch", "--noConsole", "--catalogAdd", Joiner.on(",").join(bomFiles));
-        submitCommandAndAssertRunnableSucceeds(command, new Runnable() {
-                public void run() {
-                    ManagementContext mgmt = assertMgmtStartedEventually();
+        submitCommandAndAssertFunctionSucceeds(command, new Function<ManagementContext,
Void>() {
+                public Void apply(ManagementContext mgmt) {
+                    assertMgmtStartedEventually(mgmt);
                     for (String itemName : itemSymbolicNames) {
                         CatalogItem<?, ?> item = mgmt.getCatalog().getCatalogItem(CatalogUtils.getSymbolicNameFromVersionedId(itemName),
CatalogUtils.getVersionFromVersionedId(itemName));
                         assertNotNull(item);
                     }
+                    return null;
                 }
-                private ManagementContext assertMgmtStartedEventually() {
-                    return Asserts.succeedsEventually(new Callable<ManagementContext>()
{
-                        public ManagementContext call() {
-                            ManagementContext mgmt = Iterables.getOnlyElement(Sets.difference(LocalManagementContextRegistry.getInstances(),
origMgmts));
+                private void assertMgmtStartedEventually(final ManagementContext mgmt) {
+                    Asserts.succeedsEventually(new Runnable() {
+                        public void run() {
                             assertTrue(mgmt.isStartupComplete());
-                            return mgmt;
                         }});
                 }
             });
@@ -640,23 +640,44 @@ public class CliTest {
         }
     }
 
-    private void submitCommandAndAssertRunnableSucceeds(final BrooklynCommand command, Runnable
runnable) {
+    void submitCommandAndAssertRunnableSucceeds(final BrooklynCommand command, final Runnable
runnable) {
+        submitCommandAndAssertFunctionSucceeds(command, new Function<ManagementContext,
Void>() {
+            public Void apply(ManagementContext mgmt) {
+                runnable.run();
+                return null;
+            }});
+    }
+    
+    // Function usage is a convoluted way of letting some callers know which management context
is 
+    // being used, while others can just pass a runnable.
+    void submitCommandAndAssertFunctionSucceeds(final BrooklynCommand command, final Function<ManagementContext,
Void> function) {
+        final AtomicReference<ManagementContext> mgmt = new AtomicReference<ManagementContext>();
         if (command instanceof LaunchCommand) {
-            ((LaunchCommand)command).useManagementContext(new LocalManagementContextForTests());
+            mgmt.set(LocalManagementContextForTests.newInstance());
+            ((LaunchCommand)command).useManagementContext(mgmt.get());
         }
-        executor.submit(new Callable<Void>() {
-            public Void call() throws Exception {
-                try {
-                    LOG.info("Calling command: "+command);
-                    command.call();
-                    return null;
-                } catch (Throwable t) {
-                    LOG.error("Error executing command: "+t, t);
-                    throw Exceptions.propagate(t);
+        try {
+            executor.submit(new Callable<Void>() {
+                public Void call() throws Exception {
+                    try {
+                        LOG.info("Calling command: "+command);
+                        command.call();
+                        return null;
+                    } catch (Throwable t) {
+                        LOG.error("Error executing command: "+t, t);
+                        throw Exceptions.propagate(t);
+                    }
+                }});
+    
+            Runnable functionWrapper = new Runnable() {
+                public void run() {
+                    function.apply(mgmt.get());
                 }
-            }});
-
-        Asserts.succeedsEventually(MutableMap.of("timeout", Duration.ONE_MINUTE), runnable);
+            };
+            Asserts.succeedsEventually(MutableMap.of("timeout", Duration.ONE_MINUTE), functionWrapper);
+        } finally {
+            if (mgmt.get() != null) Entities.destroyAll(mgmt.get());
+        }
     }
 
     //  An empty app to be used for testing

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b053b67b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessStopsDuringStartTest.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessStopsDuringStartTest.java
b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessStopsDuringStartTest.java
index a5d7ec0..eabea4b 100644
--- a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessStopsDuringStartTest.java
+++ b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessStopsDuringStartTest.java
@@ -57,6 +57,7 @@ import org.apache.brooklyn.util.time.Duration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.testng.Assert;
+import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
@@ -82,6 +83,7 @@ public class SoftwareProcessStopsDuringStartTest extends BrooklynAppUnitTestSupp
         executor = Executors.newCachedThreadPool();
     }
     
+    @AfterMethod(alwaysRun=true)
     @Override
     public void tearDown() throws Exception {
         if (executor != null) {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b053b67b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaSoftwareProcessTest.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaSoftwareProcessTest.java
b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaSoftwareProcessTest.java
index ae2cb73..fa8695f 100644
--- a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaSoftwareProcessTest.java
+++ b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaSoftwareProcessTest.java
@@ -41,6 +41,7 @@ import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool;
 import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.CustomResponse;
 import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.ExecCmdPredicates;
 import org.apache.brooklyn.util.core.internal.ssh.RecordingSshTool.ExecParams;
+import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
@@ -65,6 +66,7 @@ public class VanillaSoftwareProcessTest extends BrooklynAppUnitTestSupport
{
         RecordingSshTool.clear();
     }
 
+    @AfterMethod(alwaysRun=true)
     @Override
     public void tearDown() throws Exception {
         super.tearDown();

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b053b67b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
----------------------------------------------------------------------
diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
index 4523e11..60a72ed 100644
--- a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
+++ b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/VanillaWindowsProcessTest.java
@@ -35,6 +35,7 @@ import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool;
 import org.apache.brooklyn.util.core.internal.winrm.RecordingWinRmTool.ExecParams;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
@@ -60,6 +61,7 @@ public class VanillaWindowsProcessTest extends BrooklynAppUnitTestSupport
{
         RecordingWinRmTool.clear();
     }
 
+    @AfterMethod(alwaysRun=true)
     @Override
     public void tearDown() throws Exception {
         super.tearDown();


Mime
View raw message