/**
  * Dumps out the persisted mementos that are at the given directory.
  *
  * <p>Binds to the persisted state (as a "hot standby") to load the raw data (as strings), and to
  * write out the entity, location, policy, enricher, feed and catalog-item data.
  *
  * @param dir The directory containing the persisted state
  */
 public static void dumpMementoDir(File dir) {
   LocalManagementContextForTests mgmt =
       new LocalManagementContextForTests(BrooklynProperties.Factory.newEmpty());
   FileBasedObjectStore store = null;
   BrooklynMementoPersisterToObjectStore persister = null;
   try {
     store = new FileBasedObjectStore(dir);
     store.injectManagementContext(mgmt);
     store.prepareForSharedUse(PersistMode.AUTO, HighAvailabilityMode.HOT_STANDBY);
     persister =
         new BrooklynMementoPersisterToObjectStore(
             store, BrooklynProperties.Factory.newEmpty(), RebindTestUtils.class.getClassLoader());
     BrooklynMementoRawData data =
         persister.loadMementoRawData(RebindExceptionHandlerImpl.builder().build());
     List<BrooklynObjectType> types =
         ImmutableList.of(
             BrooklynObjectType.ENTITY,
             BrooklynObjectType.LOCATION,
             BrooklynObjectType.POLICY,
             BrooklynObjectType.ENRICHER,
             BrooklynObjectType.FEED,
             BrooklynObjectType.CATALOG_ITEM);
     for (BrooklynObjectType type : types) {
       LOG.info(type + " (" + data.getObjectsOfType(type).keySet() + "):");
       for (Map.Entry<String, String> entry : data.getObjectsOfType(type).entrySet()) {
         LOG.info("\t" + type + " " + entry.getKey() + ": " + entry.getValue());
       }
     }
   } finally {
     if (persister != null) persister.stop(false);
     if (store != null) store.close();
     mgmt.terminate();
   }
 }
 @Test
 public void testAddUsageListenerViaProperties() throws Exception {
   BrooklynProperties brooklynProperties = BrooklynProperties.Factory.newEmpty();
   brooklynProperties.put(
       UsageManager.USAGE_LISTENERS, RecordingStaticUsageListener.class.getName());
   mgmt = LocalManagementContextForTests.newInstance(brooklynProperties);
   assertUsageListenerCalledWhenApplicationStarted();
 }
  @Override
  protected LocalManagementContext newTestManagementContext() {
    ResourceUtils ru = new ResourceUtils(this);
    File jar = createJar(ru);
    File catalog = createCatalog(ru, jar);

    BrooklynProperties properties = BrooklynProperties.Factory.newEmpty();
    properties.put(BrooklynServerConfig.BROOKLYN_CATALOG_URL, catalog.toURI().toString());
    return LocalManagementContextForTests.builder(true)
        .useProperties(properties)
        .disableOsgi(false)
        .build();
  }
    public LocalManagementContext buildUnstarted() {
      LocalManagementContext unstarted;
      BrooklynProperties properties =
          this.properties != null ? this.properties : BrooklynProperties.Factory.newDefault();
      if (this.emptyCatalog) {
        properties.putIfAbsent(
            BrooklynServerConfig.BROOKLYN_CATALOG_URL, ManagementContextInternal.EMPTY_CATALOG_URL);
      }
      if (!enablePersistenceBackups) {
        properties.putIfAbsent(
            BrooklynServerConfig.PERSISTENCE_BACKUPS_REQUIRED_ON_DEMOTION, false);
        properties.putIfAbsent(
            BrooklynServerConfig.PERSISTENCE_BACKUPS_REQUIRED_ON_PROMOTION, false);
        properties.putIfAbsent(BrooklynServerConfig.PERSISTENCE_BACKUPS_REQUIRED, false);
      }
      if (forLive) {
        unstarted = new LocalManagementContext(properties);
      } else {
        unstarted =
            LocalManagementContextForTests.builder(true)
                .useProperties(properties)
                .disableOsgi(!enableOsgi)
                .build();
      }

      objectStore.injectManagementContext(unstarted);
      objectStore.prepareForSharedUse(
          PersistMode.AUTO, (haMode == null ? HighAvailabilityMode.DISABLED : haMode));
      BrooklynMementoPersisterToObjectStore newPersister =
          new BrooklynMementoPersisterToObjectStore(
              objectStore, unstarted.getBrooklynProperties(), classLoader);
      ((RebindManagerImpl) unstarted.getRebindManager()).setPeriodicPersistPeriod(persistPeriod);
      unstarted
          .getRebindManager()
          .setPersister(newPersister, PersistenceExceptionHandlerImpl.builder().build());
      // set the HA persister, in case any children want to use HA
      unstarted
          .getHighAvailabilityManager()
          .setPersister(
              new ManagementPlaneSyncRecordPersisterToObjectStore(
                  unstarted, objectStore, classLoader));
      return unstarted;
    }
 @Test(expectedExceptions = ClassCastException.class)
 public void testErrorWhenConfiguredClassIsNotAUsageListener() {
   BrooklynProperties brooklynProperties = BrooklynProperties.Factory.newEmpty();
   brooklynProperties.put(UsageManager.USAGE_LISTENERS, Integer.class.getName());
   mgmt = LocalManagementContextForTests.newInstance(brooklynProperties);
 }
 protected LocalManagementContext newManagementContext() {
   // loads properties, by default, but not OSGi or anything else
   return LocalManagementContextForTests.builder(true).useDefaultProperties().build();
 }
 @Override
 protected LocalManagementContext newTestManagementContext() {
   // Don't need OSGi
   return LocalManagementContextForTests.newInstance();
 }