@AfterMethod(alwaysRun = true)
  public void tearDown() throws Exception {
    List<Exception> exceptions = Lists.newArrayList();
    try {
      if (machines != null) {
        exceptions.addAll(releaseMachineSafely(machines));
        machines.clear();
      }
    } finally {
      try {
        if (managementContext != null) Entities.destroyAll(managementContext);
      } catch (Exception e) {
        LOG.warn("Error destroying management context", e);
        exceptions.add(e);
      }
    }

    // TODO Debate about whether to:
    //  - use destroyAllCatching (i.e. not propagating exception)
    //    Benefit is that other tests in class will subsequently be run, rather than bailing out.
    //  - propagate exceptions from tearDown
    //    Benefit is that we don't hide errors; release(...) etc should not be throwing exceptions.
    if (exceptions.size() > 0) {
      throw new CompoundRuntimeException("Error in tearDown of " + getClass(), exceptions);
    }
  }
 @AfterMethod(alwaysRun = true)
 public void tearDown() throws Exception {
   if (feed != null) feed.stop();
   if (jmxHelper != null) jmxHelper.disconnect();
   if (jmxService != null) jmxService.shutdown();
   if (app != null) Entities.destroyAll(app.getManagementContext());
   feed = null;
 }
  // Test reproduces functionality used in Monterey, for Venue entity being told of requestActor
  @Test
  public void testSubscribeToJmxNotificationAndEmitCorrespondingNotificationSensor()
      throws Exception {
    TestApplication app2 = new TestApplicationImpl();
    final EntityWithEmitter entity = new EntityWithEmitter(app2);
    Entities.startManagement(app2);
    try {
      app2.start(ImmutableList.of(new SimulatedLocation()));

      final List<SensorEvent<String>> received = Lists.newArrayList();
      app2.subscriptions()
          .subscribe(
              null,
              EntityWithEmitter.MY_NOTIF,
              new SensorEventListener<String>() {
                public void onEvent(SensorEvent<String> event) {
                  received.add(event);
                }
              });

      final StandardEmitterMBean mbean =
          jmxService.registerMBean(ImmutableList.of("one"), objectName);
      final AtomicInteger sequence = new AtomicInteger(0);

      jmxHelper.connect(TIMEOUT_MS);
      jmxHelper.addNotificationListener(
          jmxObjectName,
          new NotificationListener() {
            public void handleNotification(Notification notif, Object callback) {
              if (notif.getType().equals("one")) {
                entity.sensors().emit(EntityWithEmitter.MY_NOTIF, (String) notif.getUserData());
              }
            }
          });

      Asserts.succeedsEventually(
          ImmutableMap.of("timeout", TIMEOUT_MS),
          new Runnable() {
            public void run() {
              sendNotification(mbean, "one", sequence.getAndIncrement(), "abc");
              assertTrue(received.size() > 0, "received size should be bigger than 0");
              assertEquals(received.get(0).getValue(), "abc");
            }
          });
    } finally {
      Entities.destroyAll(app2.getManagementContext());
    }
  }
 @AfterMethod(alwaysRun = true)
 public void tearDown() throws Exception {
   if (managementContext != null) Entities.destroyAll(managementContext);
 }
 @AfterMethod(alwaysRun = true)
 public void tearDown() {
   if (app != null) Entities.destroyAll(app.getManagementContext());
   app = null;
 }