/**
   * Tests a JDT feature bundle container contains the appropriate bundles for a specific OS.
   *
   * @throws Exception
   */
  public void testMacOSFeatureBundleContainer() throws Exception {
    // extract the feature
    IPath location = extractModifiedFeatures();

    ITargetDefinition definition = getNewTarget();
    definition.setOS(Platform.OS_MACOSX);
    ITargetLocation container =
        getTargetService().newFeatureLocation(location.toOSString(), "org.eclipse.jdt", null);
    container.resolve(definition, null);
    TargetBundle[] bundles = container.getBundles();

    List expected = new ArrayList();
    expected.add("org.eclipse.jdt");
    expected.add("org.eclipse.jdt.launching");
    // 2 versions of JUnit
    expected.add("org.junit");
    expected.add("org.junit");
    expected.add("org.junit4");
    expected.add("org.eclipse.jdt.launching.macosx");

    assertEquals("Wrong number of bundles in JDT feature", expected.size(), bundles.length);
    for (int i = 0; i < bundles.length; i++) {
      String symbolicName = bundles[i].getBundleInfo().getSymbolicName();
      expected.remove(symbolicName);
      if (symbolicName.equals("org.eclipse.jdt.launching.macosx")) {
        // the bundle should be missing unless on Mac
        IStatus status = bundles[i].getStatus();
        if (Platform.getOS().equals(Platform.OS_MACOSX)) {
          assertTrue("Mac bundle should be present", status.isOK());
        } else {
          assertFalse("Mac bundle should be missing", status.isOK());
          assertEquals(
              "Mac bundle should be mssing",
              TargetBundle.STATUS_PLUGIN_DOES_NOT_EXIST,
              status.getCode());
        }
      }
    }
    Iterator iterator = expected.iterator();
    while (iterator.hasNext()) {
      String name = (String) iterator.next();
      System.err.println("Missing: " + name);
    }
    assertTrue("Wrong bundles in JDT feature", expected.isEmpty());

    // should be no source bundles
    for (int i = 0; i < bundles.length; i++) {
      TargetBundle bundle = bundles[i];
      assertFalse("Should be no source bundles", bundle.isSourceBundle());
    }
  }
  /**
   * Tests setting the target platform to the stored JDT feature test data
   *
   * @throws Exception
   */
  public void testSetTargetPlatformToJdtFeature() throws Exception {
    try {
      // extract the feature
      IPath location = extractModifiedFeatures();
      // org.eclipse.jdt_3.6.0.v20100105-0800-7z8VFR9FMTb52_pOyKHhoek1

      ITargetDefinition target = getNewTarget();
      ITargetLocation container =
          getTargetService()
              .newFeatureLocation(
                  location.toOSString(),
                  "org.eclipse.jdt",
                  "3.6.0.v20100105-0800-7z8VFR9FMTb52_pOyKHhoek1");

      target.setTargetLocations(new ITargetLocation[] {container});

      setTargetPlatform(target);

      List expected = new ArrayList();
      expected.add("org.eclipse.jdt");
      expected.add("org.eclipse.jdt.launching");
      // 2 versions of JUnit
      expected.add("org.junit");
      expected.add("org.junit");
      expected.add("org.junit4");
      if (Platform.getOS().equals(Platform.OS_MACOSX)) {
        expected.add("org.eclipse.jdt.launching.macosx");
      }

      // current platform
      IPluginModelBase[] models = TargetPlatformHelper.getPDEState().getTargetModels();

      assertEquals("Wrong number of bundles in JDT feature", expected.size(), models.length);
      for (int i = 0; i < models.length; i++) {
        expected.remove(models[i].getPluginBase().getId());
        assertTrue(models[i].isEnabled());
      }
      Iterator iterator = expected.iterator();
      while (iterator.hasNext()) {
        String name = (String) iterator.next();
        System.err.println("Missing: " + name);
      }
      assertTrue("Wrong bundles in target platform", expected.isEmpty());
    } finally {
      resetTargetPlatform();
    }
  }
  /**
   * 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());
      }
    }
  }
  public static void handleAddRequired(IProductPlugin[] plugins, boolean includeOptional) {
    if (plugins.length == 0) return;

    ArrayList<BundleDescription> list = new ArrayList<BundleDescription>(plugins.length);
    for (int i = 0; i < plugins.length; i++) {
      list.add(TargetPlatformHelper.getState().getBundle(plugins[i].getId(), null));
    }
    DependencyCalculator calculator = new DependencyCalculator(includeOptional);
    calculator.findDependencies(list.toArray());

    BundleDescription[] bundles = TargetPlatformHelper.getState().getBundles();
    for (int i = 0; i < bundles.length; i++) {
      HostSpecification host = bundles[i].getHost();
      if (host != null
          && !("org.eclipse.ui.workbench.compatibility"
              .equals(bundles[i].getSymbolicName())) // $NON-NLS-1$
          && calculator.containsPluginId(host.getName())) {
        calculator.findDependency(bundles[i]);
      }
    }

    Collection<?> dependencies = calculator.getBundleIDs();

    IProduct product = plugins[0].getProduct();
    IProductModelFactory factory = product.getModel().getFactory();
    IProductPlugin[] requiredPlugins = new IProductPlugin[dependencies.size()];
    int i = 0;
    Iterator<?> iter = dependencies.iterator();
    while (iter.hasNext()) {
      String id = iter.next().toString();
      IProductPlugin plugin = factory.createPlugin();
      plugin.setId(id);
      requiredPlugins[i++] = plugin;
    }
    product.addPlugins(requiredPlugins);
  }