/**
   * Tests reading a 3.0.2 install with a mix of classic and OSGi plug-ins.
   *
   * @throws Exception
   */
  public void testClassicPlugins() throws Exception {
    // extract the 3.0.2 skeleton
    IPath location = extractClassicPlugins();

    // the new way
    ITargetDefinition definition = getNewTarget();
    ITargetLocation container = getTargetService().newDirectoryLocation(location.toOSString());
    definition.setTargetLocations(new ITargetLocation[] {container});
    Set urls = getAllBundleURLs(definition);
    assertTrue("Must be bundles", urls.size() > 0);

    Preferences store = PDECore.getDefault().getPluginPreferences();
    boolean restore = store.getBoolean(ICoreConstants.TARGET_PLATFORM_REALIZATION);
    try {
      store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, false);
      // the old way
      URL[] pluginPaths = PluginPathFinder.getPluginPaths(location.toOSString());
      for (int i = 0; i < pluginPaths.length; i++) {
        URL url = pluginPaths[i];
        if (!urls.contains(url)) {
          System.err.println(url.toString());
        }
      }
      assertEquals("Wrong number of bundles", pluginPaths.length, urls.size());
    } finally {
      store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, restore);
    }
  }
  /**
   * Tests that resetting the target platform should work OK (i.e. is equivalent to the models in
   * the default target platform).
   *
   * @throws CoreException
   */
  public void testResetTargetPlatform() throws Exception {
    ITargetDefinition definition = getDefaultTargetPlatorm();
    Set urls = getAllBundleURLs(definition);
    Set fragments = new HashSet();
    TargetBundle[] bundles = definition.getBundles();
    for (int i = 0; i < bundles.length; i++) {
      if (bundles[i].isFragment()) {
        fragments.add(new File(bundles[i].getBundleInfo().getLocation()).toURL());
      }
    }

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

    // should be equivalent
    assertEquals("Should have same number of bundles", urls.size(), models.length);
    for (int i = 0; i < models.length; i++) {
      String location = models[i].getInstallLocation();
      URL url = new File(location).toURL();
      assertTrue("Missing plug-in " + location, urls.contains(url));
      if (models[i].isFragmentModel()) {
        assertTrue("Missing fragmnet", fragments.remove(url));
      }
    }
    assertTrue("Different number of fragments", fragments.isEmpty());
  }
  /**
   * Tests identification of source bundles in a 3.0.2 install.
   *
   * @throws Exception
   */
  public void testClassicSourcePlugins() throws Exception {
    // extract the 3.0.2 skeleton
    IPath location = extractClassicPlugins();

    // the new way
    ITargetDefinition definition = getNewTarget();
    ITargetLocation container = getTargetService().newDirectoryLocation(location.toOSString());
    definition.setTargetLocations(new ITargetLocation[] {container});

    definition.resolve(null);
    TargetBundle[] bundles = definition.getBundles();
    List source = new ArrayList();
    for (int i = 0; i < bundles.length; i++) {
      TargetBundle sb = bundles[i];
      if (sb.isSourceBundle()) {
        source.add(sb);
      }
    }

    assertEquals("Wrong number of source bundles", 4, source.size());
    Set names = new HashSet();
    for (int i = 0; i < source.size(); i++) {
      names.add(((TargetBundle) source.get(i)).getBundleInfo().getSymbolicName());
    }
    String[] expected =
        new String[] {
          "org.eclipse.platform.source",
          "org.eclipse.jdt.source",
          "org.eclipse.pde.source",
          "org.eclipse.platform.source.win32.win32.x86"
        };
    for (int i = 0; i < expected.length; i++) {
      assertTrue("Missing source for " + expected[i], names.contains(expected[i]));
    }
  }
 /**
  * Retrieves all bundles (source and code) in the given target definition returning them as a set
  * of URLs.
  *
  * @param target target definition
  * @return all bundle URLs
  */
 protected Set getAllBundleURLs(ITargetDefinition target) throws Exception {
   if (!target.isResolved()) {
     target.resolve(null);
   }
   TargetBundle[] bundles = target.getBundles();
   Set urls = new HashSet(bundles.length);
   for (int i = 0; i < bundles.length; i++) {
     urls.add(new File(bundles[i].getBundleInfo().getLocation()).toURL());
   }
   return urls;
 }
  /**
   * 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 reading a 3.0 style plug-in that has a MANIFEST file that is not a bundle manifest.
   *
   * @throws Exception
   */
  public void testClassicPluginsWithNonBundleManifest() throws Exception {
    // extract the plug-in
    IPath location = extractClassicNonBundleManifestPlugins();

    // the new way
    ITargetDefinition definition = getNewTarget();
    ITargetLocation container = getTargetService().newDirectoryLocation(location.toOSString());
    definition.setTargetLocations(new ITargetLocation[] {container});
    definition.resolve(null);
    TargetBundle[] bundles = definition.getAllBundles();
    assertEquals("Wrong number of bundles", 1, bundles.length);
    assertEquals(
        "Wrong bundle", "org.eclipse.core.variables", bundles[0].getBundleInfo().getSymbolicName());
  }
  /**
   * 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 equivalent to the default target platform contains the same
   * bundles as the default target platform using the platform's configuration location (which will
   * do target weaving). This is really only tested when run as a JUnit plug-in test suite from
   * within Eclipse.
   *
   * @throws Exception
   */
  public void testWovenTargetPlatform() throws Exception {
    // the new way
    ITargetDefinition definition = getNewTarget();
    ITargetLocation container =
        getTargetService()
            .newProfileLocation(
                TargetPlatform.getDefaultLocation(),
                new File(Platform.getConfigurationLocation().getURL().getFile()).getAbsolutePath());
    definition.setTargetLocations(new ITargetLocation[] {container});
    Set urls = getAllBundleURLs(definition);

    // the old way
    URL[] pluginPaths = PluginPathFinder.getPluginPaths(TargetPlatform.getDefaultLocation());
    assertEquals("Should have same number of bundles", pluginPaths.length, urls.size());
    for (int i = 0; i < pluginPaths.length; i++) {
      URL url = pluginPaths[i];
      assertTrue("Missing plug-in " + url.toString(), urls.contains(url));
    }
  }
  /**
   * 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 equivalent to the default target platform contains the same
   * bundles as the default target platform (this is an explicit location with no target weaving),
   * when created with a variable referencing ${eclipse_home}
   *
   * @throws Exception
   */
  public void testEclipseHomeTargetPlatform() throws Exception {
    // the new way
    ITargetDefinition definition = getNewTarget();
    ITargetLocation container = getTargetService().newProfileLocation("${eclipse_home}", null);
    definition.setTargetLocations(new ITargetLocation[] {container});
    Set urls = getAllBundleURLs(definition);

    // the old way
    IPath location = new Path(TargetPlatform.getDefaultLocation());
    URL[] pluginPaths =
        P2Utils.readBundlesTxt(
            location.toOSString(), location.append("configuration").toFile().toURL());
    // pluginPaths will be null (and NPE) when self-hosting and the target platform is not a real
    // installation
    assertEquals("Should have same number of bundles", pluginPaths.length, urls.size());
    for (int i = 0; i < pluginPaths.length; i++) {
      URL url = pluginPaths[i];
      assertTrue("Missing plug-in " + url.toString(), urls.contains(url));
    }
  }
  /**
   * 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());
  }
  /**
   * Tests the ability to add arguments to a target platform and have them show up on new configs
   *
   * @throws Exception
   */
  public void testArguments() throws Exception {
    ITargetDefinition definition = getNewTarget();

    // Add program arguments
    String programArgs = "-testProgramArgument -testProgramArgument2";
    definition.setProgramArguments(programArgs);
    assertEquals(programArgs, definition.getProgramArguments());

    // Add VM arguments
    String vmArgs = "-testVMArgument -testVMArgument2";
    definition.setVMArguments(vmArgs);
    assertEquals(vmArgs, definition.getVMArguments());

    try {
      getTargetService().saveTargetDefinition(definition);
      setTargetPlatform(definition);

      // Check that new launch configs will be prepopulated from target
      assertEquals(vmArgs, LaunchArgumentsHelper.getInitialVMArguments());
      assertEquals(
          "-os ${target.os} -ws ${target.ws} -arch ${target.arch} -nl ${target.nl} -consoleLog "
              .concat(programArgs),
          LaunchArgumentsHelper.getInitialProgramArguments());

    } finally {
      getTargetService().deleteTarget(definition.getHandle());
      resetTargetPlatform();
    }
  }
  /**
   * Tests that a bundle directory container is equivalent to scanning locations when it uses a
   * variable to specify its location.
   *
   * @throws Exception
   */
  public void testVariableDirectoryBundleContainer() throws Exception {
    // the new way
    ITargetDefinition definition = getNewTarget();
    ITargetLocation container = getTargetService().newDirectoryLocation("${eclipse_home}/plugins");
    definition.setTargetLocations(new ITargetLocation[] {container});
    Set urls = getAllBundleURLs(definition);

    Preferences store = PDECore.getDefault().getPluginPreferences();
    boolean restore = store.getBoolean(ICoreConstants.TARGET_PLATFORM_REALIZATION);
    try {
      store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, false);
      // the old way
      URL[] pluginPaths = PluginPathFinder.getPluginPaths(TargetPlatform.getDefaultLocation());
      assertEquals("Should have same number of bundles", pluginPaths.length, urls.size());
      for (int i = 0; i < pluginPaths.length; i++) {
        URL url = pluginPaths[i];
        assertTrue("Missing plug-in " + url.toString(), urls.contains(url));
      }
    } finally {
      store.setValue(ICoreConstants.TARGET_PLATFORM_REALIZATION, restore);
    }
  }
  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 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());
    }
  }
  /**
   * 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());
  }
  /* (non-Javadoc)
   * @see org.eclipse.pde.internal.ui.wizards.target.TargetDefinitionPage#targetChanged()
   */
  protected void targetChanged(ITargetDefinition definition) {
    super.targetChanged(definition);
    if (definition != null) {
      // When  If the page isn't open yet, try running a UI job so the dialog has time to finish
      // opening
      new UIJob(PDEUIMessages.TargetDefinitionContentPage_0) {
        public IStatus runInUIThread(IProgressMonitor monitor) {
          ITargetDefinition definition = getTargetDefinition();
          if (!definition.isResolved()) {
            try {
              getContainer()
                  .run(
                      true,
                      true,
                      new IRunnableWithProgress() {
                        public void run(IProgressMonitor monitor)
                            throws InvocationTargetException, InterruptedException {
                          getTargetDefinition().resolve(new ResolutionProgressMonitor(monitor));
                          if (monitor.isCanceled()) {
                            throw new InterruptedException();
                          }
                        }
                      });
            } catch (InvocationTargetException e) {
              PDECore.log(e);
            } catch (InterruptedException e) {
              fContentTree.setCancelled();
              return Status.CANCEL_STATUS;
            }
          }
          fContentTree.setInput(definition);
          fLocationTree.setInput(definition);
          if (definition.isResolved() && definition.getStatus().getSeverity() == IStatus.ERROR) {
            fLocationTab.setImage(
                PlatformUI.getWorkbench()
                    .getSharedImages()
                    .getImage(ISharedImages.IMG_OBJS_ERROR_TSK));
          } else {
            fLocationTab.setImage(null);
          }
          return Status.OK_STATUS;
        }
      }.schedule();
      String name = definition.getName();
      if (name == null) {
        name = EMPTY_STRING;
      }

      if (name.trim().length() > 0) fNameText.setText(name);
      else setMessage(PDEUIMessages.TargetDefinitionContentPage_8);

      fLocationTree.setInput(definition);
      fContentTree.setInput(definition);

      String presetValue = (definition.getOS() == null) ? EMPTY_STRING : definition.getOS();
      fOSCombo.setText(presetValue);
      presetValue = (definition.getWS() == null) ? EMPTY_STRING : definition.getWS();
      fWSCombo.setText(presetValue);
      presetValue = (definition.getArch() == null) ? EMPTY_STRING : definition.getArch();
      fArchCombo.setText(presetValue);
      presetValue =
          (definition.getNL() == null)
              ? EMPTY_STRING
              : LocaleUtil.expandLocaleName(definition.getNL());
      fNLCombo.setText(presetValue);

      IPath jrePath = definition.getJREContainer();
      if (jrePath == null || jrePath.equals(JavaRuntime.newDefaultJREContainerPath())) {
        fDefaultJREButton.setSelection(true);
      } else {
        String ee = JavaRuntime.getExecutionEnvironmentId(jrePath);
        if (ee != null) {
          fExecEnvButton.setSelection(true);
          fExecEnvsCombo.select(fExecEnvsCombo.indexOf(ee));
        } else {
          String vm = JavaRuntime.getVMInstallName(jrePath);
          if (vm != null) {
            fNamedJREButton.setSelection(true);
            fNamedJREsCombo.select(fNamedJREsCombo.indexOf(vm));
          }
        }
      }

      if (fExecEnvsCombo.getSelectionIndex() == -1)
        fExecEnvsCombo.setText(fExecEnvChoices.first().toString());

      if (fNamedJREsCombo.getSelectionIndex() == -1)
        fNamedJREsCombo.setText(VMUtil.getDefaultVMInstallName());

      updateJREWidgets();

      presetValue =
          (definition.getProgramArguments() == null)
              ? EMPTY_STRING
              : definition.getProgramArguments();
      fProgramArgs.setText(presetValue);
      presetValue =
          (definition.getVMArguments() == null) ? EMPTY_STRING : definition.getVMArguments();
      fVMArgs.setText(presetValue);

      fElementViewer.refresh();
    }
  }