/**
   * Tests that it is not possible to install a fix package if it specifies a fix-version range that
   * falls outside the installed target deployment package.
   */
  @Test
  public void testInstallFixPackageOutsideLowerTargetRangeFail() throws Exception {
    DeploymentPackageBuilder dpBuilder = createNewDeploymentPackageBuilder("1.0.0");
    dpBuilder.add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle2")));

    DeploymentPackage dp1 = m_deploymentAdmin.installDeploymentPackage(dpBuilder.generate());
    assertNotNull("No deployment package returned?!", dp1);

    awaitRefreshPackagesEvent();

    Bundle bundle = dp1.getBundle(getSymbolicName("bundle2"));
    assertNotNull("Failed to obtain bundle from deployment package?!", bundle);

    assertEquals(Bundle.INSTALLED, bundle.getState());

    dpBuilder = createDeploymentPackageBuilder(dpBuilder.getSymbolicName(), "1.0.1");
    dpBuilder
        .setFixPackage("(1.0,2.0)") // should not include version 1.0.0!
        .add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle1")))
        .add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle2")).setMissing());

    try {
      m_deploymentAdmin.installDeploymentPackage(dpBuilder.generate());
      fail("Succeeded into installing fix package for undefined target package?!");
    } catch (DeploymentException exception) {
      // Ok; expected
      assertDeploymentException(CODE_MISSING_FIXPACK_TARGET, exception);
    }
  }
  /** Tests that we can install a new bundle through a fix-package. */
  @Test
  public void testInstallBundleWithDependencyInFixPackageUpdateOk() throws Exception {
    DeploymentPackageBuilder dpBuilder = createNewDeploymentPackageBuilder("1.0.0");
    dpBuilder.add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle2")));

    DeploymentPackage dp1 = m_deploymentAdmin.installDeploymentPackage(dpBuilder.generate());
    assertNotNull("No deployment package returned?!", dp1);

    awaitRefreshPackagesEvent();

    Bundle bundle = dp1.getBundle(getSymbolicName("bundle2"));
    assertNotNull("Failed to obtain bundle from deployment package?!", bundle);

    assertEquals(Bundle.INSTALLED, bundle.getState());

    dpBuilder = createDeploymentPackageBuilder(dpBuilder.getSymbolicName(), "1.0.1");
    dpBuilder
        .setFixPackage("[1.0,2.0)")
        .add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle1")))
        .add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle2")).setMissing());

    DeploymentPackage dp2 = m_deploymentAdmin.installDeploymentPackage(dpBuilder.generate());
    assertNotNull("No deployment package returned?!", dp2);

    awaitRefreshPackagesEvent();

    bundle = dp2.getBundle(getSymbolicName("bundle2"));
    assertNotNull("Failed to obtain bundle from bundle context?!", bundle);

    assertBundleExists(getSymbolicName("bundle1"), "1.0.0");
    assertBundleExists(getSymbolicName("bundle2"), "1.0.0");

    assertTrue(isBundleActive(bundle));
  }
  /** Tests the removal of a bundle through a fix package. */
  @Test
  public void testRemoveBundleInFixPackageUpdateOk() throws Exception {
    DeploymentPackageBuilder dpBuilder = createNewDeploymentPackageBuilder("1.0.0");
    dpBuilder
        .add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle1")))
        .add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle2")));

    DeploymentPackage dp1 = m_deploymentAdmin.installDeploymentPackage(dpBuilder.generate());
    assertNotNull("No deployment package returned?!", dp1);

    awaitRefreshPackagesEvent();

    Bundle bundle = dp1.getBundle(getSymbolicName("bundle2"));
    assertNotNull("Failed to obtain bundle from deployment package?!", bundle);

    assertEquals(Bundle.ACTIVE, bundle.getState());

    // valid-bundle2 is to be removed by this fix package...
    dpBuilder = createDeploymentPackageBuilder(dpBuilder.getSymbolicName(), "1.0.1");
    dpBuilder
        .setFixPackage("[1.0,2.0)")
        .add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle1")).setMissing());

    DeploymentPackage dp2 = m_deploymentAdmin.installDeploymentPackage(dpBuilder.generate());
    assertNotNull("No deployment package returned?!", dp2);

    awaitRefreshPackagesEvent();

    assertBundleExists(getSymbolicName("bundle1"), "1.0.0");
    assertBundleNotExists(getSymbolicName("bundle2"), "1.0.0");
  }
  /** Tests that only in a fix-package bundle can be marked as missing. */
  @Test
  public void testMissingBundlesOnlyInFixPackageFail() throws Exception {
    DeploymentPackageBuilder dpBuilder = createNewDeploymentPackageBuilder("1.0.0");
    dpBuilder.add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle2")));

    DeploymentPackage dp1 = m_deploymentAdmin.installDeploymentPackage(dpBuilder.generate());
    assertNotNull("No deployment package returned?!", dp1);

    awaitRefreshPackagesEvent();

    Bundle bundle = dp1.getBundle(getSymbolicName("bundle2"));
    assertNotNull("Failed to obtain bundle from deployment package?!", bundle);

    assertEquals(Bundle.INSTALLED, bundle.getState());

    dpBuilder = createDeploymentPackageBuilder(dpBuilder.getSymbolicName(), "1.0.1");
    dpBuilder
        .disableVerification()
        .add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle1")))
        .add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle2")).setMissing());

    try {
      m_deploymentAdmin.installDeploymentPackage(dpBuilder.generate());
      fail("Failed to install missing bundle?!");
    } catch (DeploymentException exception) {
      // Ok; expected...
      assertEquals("Invalid exception code?!", CODE_BAD_HEADER, exception.getCode());
    }
  }
  /**
   * Tests that we can uninstall a fix-package and that this will only uninstall the bundles
   * installed by the fix-package.
   */
  @Test
  public void testUninstallFixPackageOnlyRemovesOwnArtifactsOk() throws Exception {
    DeploymentPackageBuilder dpBuilder = createNewDeploymentPackageBuilder("1.0.0");
    dpBuilder.add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle1")));

    DeploymentPackage dp1 = m_deploymentAdmin.installDeploymentPackage(dpBuilder.generate());
    assertNotNull("No deployment package returned?!", dp1);

    assertEquals(
        "Expected a single deployment package?!",
        1,
        m_deploymentAdmin.listDeploymentPackages().length);

    dpBuilder = createNewDeploymentPackageBuilder("1.0.0");
    dpBuilder.add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle2")));

    DeploymentPackage dp2 = m_deploymentAdmin.installDeploymentPackage(dpBuilder.generate());
    assertNotNull("No deployment package returned?!", dp2);

    awaitRefreshPackagesEvent();

    assertEquals(
        "Expected two deployment packages?!", 2, m_deploymentAdmin.listDeploymentPackages().length);

    // add bundle2 through fix package...
    dpBuilder = createDeploymentPackageBuilder(dpBuilder.getSymbolicName(), "1.0.1");
    dpBuilder
        .setFixPackage("[1.0,2.0)")
        .add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle3")))
        .add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle2")).setMissing());

    DeploymentPackage dp3 = m_deploymentAdmin.installDeploymentPackage(dpBuilder.generate());
    assertNotNull("No deployment package returned?!", dp3);

    awaitRefreshPackagesEvent();

    assertEquals(
        "Expected two deployment packages?!", 2, m_deploymentAdmin.listDeploymentPackages().length);

    assertBundleExists(getSymbolicName("bundle1"), "1.0.0");
    assertBundleExists(getSymbolicName("bundle2"), "1.0.0");
    assertBundleExists(getSymbolicName("bundle3"), "1.0.0");

    // Uninstall the deployment package; should yield the initial situation again...
    dp3.uninstall();

    awaitRefreshPackagesEvent();

    assertEquals(
        "Expected a single deployment package?!",
        1,
        m_deploymentAdmin.listDeploymentPackages().length);

    // None of our installed bundles should remain...
    assertBundleNotExists(getSymbolicName("bundle3"), "1.0.0");
    assertBundleNotExists(getSymbolicName("bundle2"), "1.0.0");
    // The bundle installed in another deployment package should still remain...
    assertBundleExists(getSymbolicName("bundle1"), "1.0.0");
  }
  @Test
  public void checkDefaultBehavior() throws DeploymentException, IOException {
    DeploymentAdmin admin =
        (DeploymentAdmin)
            context.getService(context.getServiceReference(DeploymentAdmin.class.getName()));
    Assert.assertNotNull(admin);

    URL d = new URL((String) context.getProperty("dp1"));
    DeploymentPackage dp = admin.installDeploymentPackage(d.openStream());

    Bundle b1 = getBundleByName(context, "org.apache.felix.log");
    Assert.assertNull(b1);

    Bundle b2 = getBundleByName(context, "org.apache.felix.bundlerepository");
    Assert.assertNotNull(b2);

    Assert.assertEquals(Bundle.ACTIVE, b2.getState());

    URL d1 = new URL((String) context.getProperty("dp2"));
    DeploymentPackage dp2 = admin.installDeploymentPackage(d1.openStream());

    b1 = getBundleByName(context, "org.apache.felix.log");
    Assert.assertNotNull(b1);

    b2 = getBundleByName(context, "org.apache.felix.bundlerepository");
    Assert.assertNotNull(b2);

    Assert.assertEquals(Bundle.ACTIVE, b1.getState());
    Assert.assertEquals(Bundle.ACTIVE, b2.getState());

    dp.uninstall();

    // Bundle Repository uninstalled.
    b1 = getBundleByName(context, "org.apache.felix.log");
    Assert.assertNotNull(b1);

    b2 = getBundleByName(context, "org.apache.felix.bundlerepository");
    Assert.assertNull(b2);

    dp2.uninstall();
    b1 = getBundleByName(context, "org.apache.felix.log");
    Assert.assertNull(b1);
  }
  /** Tests that we can uninstall a fix-package. */
  @Test
  public void testUninstallBundleAddedInFixPackageOk() throws Exception {
    DeploymentPackageBuilder dpBuilder = createNewDeploymentPackageBuilder("1.0.0");
    dpBuilder.add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle2")));

    DeploymentPackage dp1 = m_deploymentAdmin.installDeploymentPackage(dpBuilder.generate());
    assertNotNull("No deployment package returned?!", dp1);

    awaitRefreshPackagesEvent();

    // Add valid-bundle1 through fix-package...
    dpBuilder = createDeploymentPackageBuilder(dpBuilder.getSymbolicName(), "1.0.1");
    dpBuilder
        .setFixPackage("[1.0,2.0)")
        .add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle1")))
        .add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle2")).setMissing());

    DeploymentPackage dp2 = m_deploymentAdmin.installDeploymentPackage(dpBuilder.generate());
    assertNotNull("No deployment package returned?!", dp2);

    awaitRefreshPackagesEvent();

    assertEquals(
        "Expected a single deployment package?!",
        1,
        m_deploymentAdmin.listDeploymentPackages().length);

    assertBundleExists(getSymbolicName("bundle1"), "1.0.0");
    assertBundleExists(getSymbolicName("bundle2"), "1.0.0");

    // Uninstall the deployment package; should yield the original situation again...
    dp2.uninstall();

    awaitRefreshPackagesEvent();

    assertEquals(
        "Expected no deployment package?!", 0, m_deploymentAdmin.listDeploymentPackages().length);

    // None of our installed bundles should remain...
    assertBundleNotExists(getSymbolicName("bundle1"), "1.0.0");
    assertBundleNotExists(getSymbolicName("bundle2"), "1.0.0");
  }