public void testModuleRefreshDuringServerConnect2() throws Exception {
    // Deploy and start an application.
    // Disconnect through the server behaviour. Verify through an external
    // client that the app
    // remains deployed and in started mode.
    // Reconnect, and verify that the application is still running (i.e.
    // disconnecting
    // the server should not stop the application).

    String appPrefix = "testModuleRefreshDuringServerConnect2";
    String expectedAppName = harness.getDefaultWebAppName(appPrefix);

    createWebApplicationProject();

    // Note that deploying application fires off an app change event AFTER
    // the deployment is
    // successful. To make sure that the second event listener further down
    // does not accidentally receive the app
    // change event,
    // wait for the app change event from the deploy first, and then
    // schedule the second listener to
    // listen to the expected refresh event
    deployAndWaitForDeploymentEvent(appPrefix);

    // Cloud module should have been created.
    Collection<CloudFoundryApplicationModule> appModules = cloudServer.getExistingCloudModules();
    assertEquals(
        harness.getDefaultWebAppName(appPrefix),
        appModules.iterator().next().getDeployedApplicationName());

    // Disconnect and verify that there are no cloud foundry application
    // modules
    serverBehavior.disconnect(new NullProgressMonitor());
    appModules = cloudServer.getExistingCloudModules();
    assertTrue(
        "Expected empty list of cloud application modules after server disconnect",
        appModules.isEmpty());

    // Now create an external client to independently check that the
    // application remains deployed and in started mode

    CloudFoundryOperations client = harness.createExternalClient();
    client.login();
    List<CloudApplication> deployedApplications = client.getApplications();
    assertEquals(
        "Expected 1 Cloud application in Cloud space after server disconnect",
        1,
        deployedApplications.size());
    assertEquals(expectedAppName, deployedApplications.get(0).getName());
    assertTrue(deployedApplications.get(0).getState() == AppState.STARTED);

    // Register a module refresh listener before connecting again to be
    // notified when
    // modules are refreshed
    ModulesRefreshListener listener =
        getModulesRefreshListener(null, cloudServer, CloudServerEvent.EVENT_SERVER_REFRESHED);

    serverBehavior.connect(new NullProgressMonitor());

    assertModuleRefreshedAndDispose(listener, CloudServerEvent.EVENT_SERVER_REFRESHED);

    appModules = cloudServer.getExistingCloudModules();
    CloudFoundryApplicationModule appModule = appModules.iterator().next();

    assertEquals(expectedAppName, appModule.getDeployedApplicationName());

    assertApplicationIsRunning(appModule);
  }
  public void testUpdateModulesCloudServer() throws Exception {

    // Tests the Update modules API in the server that will CREATE or return
    // an existing
    // CloudFoundryApplicationModule ONLY if it is given a CloudApplication.

    String prefix = "testUpdateModulesCloudServer";
    String expectedAppName = harness.getDefaultWebAppName(prefix);

    // Create the app externally AFTER the server connects in the setup to
    // ensure the tools did not pick up the Cloud application during refresh
    CloudFoundryOperations client = harness.createExternalClient();
    client.login();

    List<String> urls = new ArrayList<String>();
    urls.add(harness.getExpectedDefaultURL(prefix));
    client.createApplication(
        expectedAppName, new Staging(), CloudUtil.DEFAULT_MEMORY, urls, new ArrayList<String>());

    CloudFoundryApplicationModule appModule = cloudServer.getExistingCloudModule(expectedAppName);
    // Tooling has not yet been updated so there is no corresponding
    // appModule even though the app exists in the Cloud space
    assertNull(appModule);

    // No actual cloud application passed to update therefore no associated
    // CloudFoundryApplicationModule should be found
    appModule = cloudServer.updateModule(null, expectedAppName, new NullProgressMonitor());
    assertNull(appModule);

    appModule = cloudServer.updateModule(null, null, new NullProgressMonitor());
    assertNull(appModule);

    assertTrue(cloudServer.getExistingCloudModules().isEmpty());

    // Get the actual cloud app directly from the Cloud space
    CloudApplication actualApp = client.getApplications().get(0);

    // Now create the CloudFoundryApplicationModule
    appModule = cloudServer.updateModule(actualApp, expectedAppName, new NullProgressMonitor());

    assertEquals(expectedAppName, appModule.getDeployedApplicationName());
    assertEquals(appModule.getDeployedApplicationName(), appModule.getApplication().getName());

    // Check the mapping is correct
    assertEquals(actualApp.getName(), appModule.getApplication().getName());

    assertEquals(CloudUtil.DEFAULT_MEMORY, appModule.getApplication().getMemory());
    assertEquals(appModule.getDeploymentInfo().getMemory(), appModule.getApplication().getMemory());

    // It should match what is obtained through getExisting API
    CloudFoundryApplicationModule existingCloudMod =
        cloudServer.getExistingCloudModule(expectedAppName);

    assertEquals(expectedAppName, existingCloudMod.getDeployedApplicationName());
    assertEquals(
        existingCloudMod.getDeployedApplicationName(), existingCloudMod.getApplication().getName());

    // Check the mapping is correct
    assertEquals(actualApp.getName(), existingCloudMod.getApplication().getName());

    assertEquals(CloudUtil.DEFAULT_MEMORY, existingCloudMod.getApplication().getMemory());
    assertEquals(
        existingCloudMod.getDeploymentInfo().getMemory(),
        existingCloudMod.getApplication().getMemory());

    // Check the other existing Modules API
    CloudFoundryApplicationModule sameExistingApp =
        cloudServer.getExistingCloudModules().iterator().next();
    assertEquals(expectedAppName, sameExistingApp.getDeployedApplicationName());
    assertEquals(
        sameExistingApp.getDeployedApplicationName(), sameExistingApp.getApplication().getName());

    // Check the mapping is correct
    assertEquals(actualApp.getName(), sameExistingApp.getApplication().getName());

    assertEquals(CloudUtil.DEFAULT_MEMORY, sameExistingApp.getApplication().getMemory());
    assertEquals(
        sameExistingApp.getDeploymentInfo().getMemory(),
        sameExistingApp.getApplication().getMemory());
  }