/**
   * Tests that a target definition based on the default target platform restricted to a subset of
   * bundle versions contains the right set.
   *
   * @throws Exception
   */
  public void testVersionRestrictedDefaultTargetPlatform() throws Exception {
    ITargetDefinition definition = getNewTarget();
    ITargetLocation container =
        getTargetService().newProfileLocation(TargetPlatform.getDefaultLocation(), null);
    definition.setTargetLocations(new ITargetLocation[] {container});
    List infos = getAllBundleInfos(definition);
    // find right versions
    String v1 = null;
    String v2 = null;
    Iterator iterator = infos.iterator();
    while (iterator.hasNext() && (v2 == null || v1 == null)) {
      BundleInfo info = (BundleInfo) iterator.next();
      if (info.getSymbolicName().equals("org.eclipse.jdt.launching")) {
        v1 = info.getVersion();
      } else if (info.getSymbolicName().equals("org.eclipse.jdt.debug")) {
        v2 = info.getVersion();
      }
    }
    assertNotNull(v1);
    assertFalse(v1.equals(BundleInfo.EMPTY_VERSION));
    assertNotNull(v2);
    assertFalse(v2.equals(BundleInfo.EMPTY_VERSION));

    NameVersionDescriptor[] restrictions =
        new NameVersionDescriptor[] {
          new NameVersionDescriptor("org.eclipse.jdt.launching", v1),
          new NameVersionDescriptor("org.eclipse.jdt.debug", v2)
        };
    definition.setIncluded(restrictions);
    infos = getAllBundleInfos(definition);

    assertEquals("Wrong number of bundles", 2, infos.size());
    iterator = infos.iterator();
    while (iterator.hasNext()) {
      BundleInfo info = (BundleInfo) iterator.next();
      if (info.getSymbolicName().equals("org.eclipse.jdt.launching")) {
        assertEquals(v1, info.getVersion());
      } else if (info.getSymbolicName().equals("org.eclipse.jdt.debug")) {
        assertEquals(v2, info.getVersion());
      }
    }
  }
  /**
   * Tests that a target definition based on the default target platform restricted to a subset of
   * bundles contains the right set.
   *
   * @throws Exception
   */
  public void testRestrictedDefaultTargetPlatform() throws Exception {
    ITargetDefinition definition = getNewTarget();
    ITargetLocation container =
        getTargetService().newProfileLocation(TargetPlatform.getDefaultLocation(), null);
    NameVersionDescriptor[] restrictions =
        new NameVersionDescriptor[] {
          new NameVersionDescriptor("org.eclipse.jdt.launching", null),
          new NameVersionDescriptor("org.eclipse.jdt.debug", null)
        };
    definition.setTargetLocations(new ITargetLocation[] {container});
    definition.setIncluded(restrictions);
    List infos = getAllBundleInfos(definition);

    assertEquals("Wrong number of bundles", 2, infos.size());
    Set set = collectAllSymbolicNames(infos);
    for (int i = 0; i < restrictions.length; i++) {
      NameVersionDescriptor info = restrictions[i];
      set.remove(info.getId());
    }
    assertEquals("Wrong bundles", 0, set.size());
  }
  protected void doIncludeVersions(NameVersionDescriptor[] descriptions) throws Exception {
    String bsn = MULTI_VERSION_LOW_DESCRIPTION.getId();

    IPath extras = extractMultiVersionPlugins();
    ITargetDefinition target = getNewTarget();
    ITargetLocation container = getTargetService().newDirectoryLocation(extras.toOSString());
    target.setTargetLocations(new ITargetLocation[] {container});
    target.setIncluded(descriptions);
    try {
      getTargetService().saveTargetDefinition(target);
      setTargetPlatform(target);
      IPluginModelBase[] models = PluginRegistry.getExternalModels();
      Set enabled = new HashSet();
      for (int i = 0; i < models.length; i++) {
        IPluginModelBase pm = models[i];
        if (pm.getBundleDescription().getSymbolicName().equals(bsn)) {
          NameVersionDescriptor desc =
              new NameVersionDescriptor(
                  pm.getPluginBase().getId(), pm.getPluginBase().getVersion());
          if (pm.isEnabled()) {
            enabled.add(desc);
          }
        }
      }
      if (descriptions == null) {

      } else {
        assertEquals("Wrong number of enabled bundles", descriptions.length, enabled.size());
        for (int i = 0; i < descriptions.length; i++) {
          assertTrue("Missing bundle", enabled.contains(descriptions[i]));
        }
      }
    } finally {
      getTargetService().deleteTarget(target.getHandle());
      resetTargetPlatform();
    }
  }
  /**
   * Tests that a target definition based on the JDT feature restricted to a subset of bundles
   * contains the right set.
   *
   * @throws Exception
   */
  public void testRestrictedFeatureBundleContainer() throws Exception {
    // extract the feature
    IPath location = extractModifiedFeatures();

    ITargetDefinition definition = getNewTarget();
    ITargetLocation container =
        getTargetService().newFeatureLocation(location.toOSString(), "org.eclipse.jdt", null);
    NameVersionDescriptor[] restrictions =
        new NameVersionDescriptor[] {
          new NameVersionDescriptor("org.eclipse.jdt", null),
          new NameVersionDescriptor("org.junit", "3.8.2.v20090203-1005")
        };
    definition.setTargetLocations(new ITargetLocation[] {container});
    definition.setIncluded(restrictions);
    List infos = getAllBundleInfos(definition);

    assertEquals("Wrong number of bundles", 2, infos.size());
    Set set = collectAllSymbolicNames(infos);
    for (int i = 0; i < restrictions.length; i++) {
      NameVersionDescriptor info = restrictions[i];
      set.remove(info.getId());
    }
    assertEquals("Wrong bundles", 0, set.size());
  }
  /**
   * Tests that a target definition based on the default target platform restricted to a subset of
   * bundles contains the right set. In this case empty, since the versions specified are bogus.
   *
   * @throws Exception
   */
  public void testMissingVersionRestrictedDefaultTargetPlatform() throws Exception {
    ITargetDefinition definition = getNewTarget();
    ITargetLocation container =
        getTargetService().newProfileLocation(TargetPlatform.getDefaultLocation(), null);
    NameVersionDescriptor[] restrictions =
        new NameVersionDescriptor[] {
          new NameVersionDescriptor("org.eclipse.jdt.launching", "xyz"),
          new NameVersionDescriptor("org.eclipse.jdt.debug", "abc")
        };
    definition.setTargetLocations(new ITargetLocation[] {container});
    definition.setIncluded(restrictions);
    definition.resolve(null);
    TargetBundle[] bundles = definition.getBundles();

    assertEquals("Wrong number of bundles", 2, bundles.length);
    for (int i = 0; i < bundles.length; i++) {
      TargetBundle rb = bundles[i];
      assertEquals(
          "Should be a missing bundle version",
          TargetBundle.STATUS_VERSION_DOES_NOT_EXIST,
          rb.getStatus().getCode());
      assertEquals("Should be an error", IStatus.ERROR, rb.getStatus().getSeverity());
    }
  }