/**
  * A feature container should not have VM arguments.
  *
  * @throws Exception
  */
 public void testArgumentsFeatureContainer() throws Exception {
   ITargetLocation featureContainer =
       getTargetService()
           .newFeatureLocation(
               TargetPlatform.getDefaultLocation(), "DOES NOT EXIST", "DOES NOT EXIST");
   assertNull("Feature containers should not have arguments", featureContainer.getVMArguments());
 }
 /**
  * A profile container should have VM arguments.
  *
  * @throws Exception
  */
 public void testArgumentsProfileContainer() throws Exception {
   ITargetLocation profileContainer =
       getTargetService().newProfileLocation(TargetPlatform.getDefaultLocation(), null);
   String[] arguments = profileContainer.getVMArguments();
   assertNotNull("Profile containers should have arguments", arguments);
   assertTrue("Profile containers should have arguments", arguments.length > 0);
 }
 /**
  * A directory that points to an installation should have VM arguments.
  *
  * @throws Exception
  */
 public void testArgumentsInstallDirectory() throws Exception {
   ITargetLocation installDirectory =
       getTargetService().newDirectoryLocation(TargetPlatform.getDefaultLocation());
   String[] installArgs = installDirectory.getVMArguments();
   assertNotNull("Install directory should have arguments", installArgs);
   assertTrue("Install directory should have arguments", installArgs.length > 0);
 }
 /**
  * A directory of bundles should not have VM arguments.
  *
  * @throws Exception
  */
 public void testArgumentsPluginsDirectory() throws Exception {
   // test bundle containers for known arguments
   ITargetLocation directoryContainer =
       getTargetService().newDirectoryLocation(TargetPlatform.getDefaultLocation() + "/plugins");
   assertNull(
       "Plugins directory containers should not have arguments",
       directoryContainer.getVMArguments());
 }
  /**
   * 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 equivalent to the default target platform contains the same
   * bundles as the default target platform (this is an explicit location with no target weaving).
   *
   * @throws Exception
   */
  public void testDefaultTargetPlatform() throws Exception {
    // the new way
    ITargetDefinition definition = getNewTarget();
    ITargetLocation container =
        getTargetService().newProfileLocation(TargetPlatform.getDefaultLocation(), 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 bundle directory container is equivalent to scanning locations.
   *
   * @throws Exception
   */
  public void testDirectoryBundleContainer() throws Exception {
    // the new way
    ITargetDefinition definition = getNewTarget();
    ITargetLocation container =
        getTargetService().newDirectoryLocation(TargetPlatform.getDefaultLocation() + "/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);
    }
  }
 /**
  * Returns the location of the JDT feature in the running host as a path in the local file system.
  *
  * @return path to JDT feature
  */
 protected IPath getJdtFeatureLocation() {
   IPath path = new Path(TargetPlatform.getDefaultLocation());
   path = path.append("features");
   File dir = path.toFile();
   assertTrue("Missing features directory", dir.exists() && !dir.isFile());
   String[] files = dir.list();
   String location = null;
   for (int i = 0; i < files.length; i++) {
     if (files[i].startsWith("org.eclipse.jdt_")) {
       location = path.append(files[i]).toOSString();
       break;
     }
   }
   assertNotNull("Missing JDT feature", location);
   return new Path(location);
 }
  /**
   * 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());
  }
  /**
   * 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());
    }
  }