public void test008_staleMissingParent() throws Exception {
    // p1 does not have parent
    IProject p1 = createExisting("t008-p1");
    assertNotNull(p1);
    IProject p3 = createExisting("t008-p3");
    assertNotNull(p3);
    waitForJobsToComplete();

    IMavenProjectFacade f1 = manager.create(p1, monitor);
    assertNull(f1); // XXX should I return non-null facade that does not have MavenProject?

    // update p1 to have p3 parent
    InputStream contents = p1.getFile("pom_updated.xml").getContents();
    p1.getFile("pom.xml").setContents(contents, IResource.FORCE, monitor);
    contents.close();
    waitForJobsToComplete();

    f1 = manager.create(p1, monitor);
    assertEquals("t008-p3", getParentProject(f1).getArtifactId());

    events.clear();
    IProject p2 = createExisting("t008-p2");
    waitForJobsToComplete();

    assertEquals(1, events.size());
    MavenProjectChangedEvent event = events.get(0);
    assertEquals(p2.getFile(IMavenConstants.POM_FILE_NAME), event.getSource());
    assertEquals(MavenProjectChangedEvent.KIND_ADDED, event.getKind());
  }
  public void testOptionalDependencies() throws Exception {
    IProject p1 = createExisting("optionaldependency-p01");
    IProject p2 = createExisting("optionaldependency-p02");
    waitForJobsToComplete();

    IFile pom1 = p1.getFile("pom.xml");
    IFile pom2 = p2.getFile("pom.xml");

    {
      IMavenProjectFacade f = manager.create(pom2, false, null);
      Artifact[] a = getMavenProjectArtifacts(f).toArray(new Artifact[0]);
      assertEquals(2, a.length);
      assertEquals(
          pom1.getLocation().toFile().getCanonicalFile(), a[0].getFile().getCanonicalFile());
      assertEquals("junit-3.8.1.jar", a[1].getFile().getName());
    }

    copyContent(p1, "pom-changed.xml", "pom.xml");

    {
      IMavenProjectFacade f = manager.create(pom2, false, null);
      Artifact[] a = getMavenProjectArtifacts(f).toArray(new Artifact[0]);
      assertEquals(1, a.length);
      assertEquals(
          pom1.getLocation().toFile().getCanonicalFile(), a[0].getFile().getCanonicalFile());
    }
  }
  public void test007_changedVersion() throws Exception {
    // p1 depends on p2
    IProject p1 = createExisting("t007-p1");
    IProject p2 = createExisting("t007-p2");
    waitForJobsToComplete();

    IMavenProjectFacade f1 = manager.create(p1, monitor);
    List<Artifact> a1 = new ArrayList<Artifact>(f1.getMavenProject(monitor).getArtifacts());
    assertEquals(1, f1.getMavenProject(monitor).getArtifacts().size());
    assertEquals(
        p2.getFile(IMavenConstants.POM_FILE_NAME).getLocation().toFile(), a1.get(0).getFile());

    // update p2 to have new version
    copyContent(p2, "pom_newVersion.xml", "pom.xml");
    f1 = manager.create(p1, monitor);
    a1 = new ArrayList<Artifact>(f1.getMavenProject(monitor).getArtifacts());
    assertEquals(1, f1.getMavenProject(monitor).getArtifacts().size());
    assertStartWith(repo.getAbsolutePath(), a1.get(0).getFile().getAbsolutePath());

    // update p2 back to the original version
    copyContent(p2, "pom_original.xml", "pom.xml");
    f1 = manager.create(p1, monitor);
    a1 = new ArrayList<Artifact>(f1.getMavenProject(monitor).getArtifacts());
    assertEquals(1, f1.getMavenProject(monitor).getArtifacts().size());
    assertEquals(
        p2.getFile(IMavenConstants.POM_FILE_NAME).getLocation().toFile(), a1.get(0).getFile());
  }
  public void test007_staleDependencies() throws Exception {
    // p1 depends on p2
    IProject p1 = createExisting("t007-p1");
    IProject p2 = createExisting("t007-p2");
    waitForJobsToComplete();

    IMavenProjectFacade f1 = manager.create(p1, monitor);
    assertEquals(1, f1.getMavenProject(monitor).getArtifacts().size());

    // update p1 to remove dependency on p2
    copyContent(p1, "pom_updated.xml", "pom.xml");

    f1 = manager.create(p1, monitor);
    assertEquals(0, f1.getMavenProject(monitor).getArtifacts().size());

    events.clear();

    // remove p2
    deleteProject(p2);
    waitForJobsToComplete();

    assertEquals(1, events.size());
    MavenProjectChangedEvent event = events.get(0);
    assertEquals(p2.getFile(IMavenConstants.POM_FILE_NAME), event.getSource());
    assertEquals(MavenProjectChangedEvent.KIND_REMOVED, event.getKind());
  }
  public void test000_noChangeReload() throws Exception {
    IProject p1 = createExisting("t000-p1");
    IFile pom = p1.getFile(IMavenConstants.POM_FILE_NAME);
    waitForJobsToComplete();

    IMavenProjectFacade oldFacade = manager.create(p1, monitor);

    MavenProjectChangedEvent event;
    //    assertEquals(1, events.size());
    //    event = events.get(0);
    //    assertEquals(pom, event.getSource());
    //    assertEquals(MavenProjectChangedEvent.KIND_ADDED, event.getKind());

    events.clear();

    pom.setLocalTimeStamp(pom.getLocalTimeStamp() + 1000L);
    pom.touch(monitor);
    waitForJobsToComplete();

    IMavenProjectFacade newFacade = manager.create(p1, monitor);
    assertNotSame(oldFacade.getMavenProject(monitor), newFacade.getMavenProject(monitor));

    assertEquals(1, events.size());
    event = events.get(0);
    assertEquals(pom, event.getSource());
    assertEquals(MavenProjectChangedEvent.KIND_CHANGED, event.getKind());
    assertEquals(MavenProjectChangedEvent.FLAG_NONE, event.getFlags());
    assertNotNull(event.getOldMavenProject());
    assertNotNull(event.getMavenProject());
  }
  public void test005_snapshotAvailableFromLocalRepoAndWorkspace() throws Exception {
    IProject p1 = createExisting("t005-p3");
    waitForJobsToComplete();

    {
      IMavenProjectFacade f = manager.create(p1, monitor);
      Set<Artifact> artifacts = getMavenProjectArtifacts(f);
      assertEquals(1, artifacts.size());
      Artifact a = artifacts.iterator().next();
      assertEquals(true, a.isResolved());
      // assertTrue(a.getFile().getAbsolutePath().startsWith(repo.getAbsolutePath()));
      assertStartWith(repo.getAbsolutePath(), a.getFile().getAbsolutePath());
    }

    IProject p2 = createExisting("t005-p4");
    waitForJobsToComplete();

    {
      IMavenProjectFacade f = manager.create(p1, monitor);
      Set<Artifact> artifacts = getMavenProjectArtifacts(f);
      assertEquals(1, artifacts.size());
      Artifact a = artifacts.iterator().next();
      assertEquals(true, a.isResolved());
      assertEquals(p2.getFile(IMavenConstants.POM_FILE_NAME).getLocation().toFile(), a.getFile());
    }
  }
  public void testDependencyScopeChanged() throws Exception {
    // Changing the scope of a dependency should trigger a project update
    IProject p1 = createExisting("changedscope-p01");
    IProject p2 = createExisting("changedscope-p02");
    waitForJobsToComplete();

    IFile pom1 = p1.getFile("pom.xml");
    IFile pom2 = p2.getFile("pom.xml");

    {
      IMavenProjectFacade f = manager.create(pom2, false, null);
      Artifact[] a = getMavenProjectArtifacts(f).toArray(new Artifact[0]);
      assertEquals(2, a.length);
      assertEquals(
          pom1.getLocation().toFile().getCanonicalFile(), a[0].getFile().getCanonicalFile());
      assertEquals("junit-3.8.1.jar", a[1].getFile().getName());
    }

    copyContent(p1, "pom-scope-changed.xml", "pom.xml");

    {
      IMavenProjectFacade f = manager.create(pom2, false, null);
      Artifact[] a = getMavenProjectArtifacts(f).toArray(new Artifact[0]);
      assertEquals("provided scope dependency should disappear", 1, a.length);
      assertEquals(
          pom1.getLocation().toFile().getCanonicalFile(), a[0].getFile().getCanonicalFile());
    }
  }
  public void test000_removeClosed() throws Exception {
    IProject p1 = createExisting("t000-p1");
    waitForJobsToComplete();

    IMavenProjectFacade f1 = manager.create(p1, monitor);

    MavenProjectChangedEvent event;
    //    assertEquals(1, events.size());
    //    event = events.get(0);
    //    assertEquals(MavenProjectChangedEvent.KIND_ADDED, event.getKind());
    //    assertNull(event.getOldMavenProject());
    //    assertSame(f1, event.getMavenProject());

    assertEquals(p1.getFullPath(), f1.getFullPath());

    events.clear();

    p1.close(monitor);
    waitForJobsToComplete();

    assertNull(manager.create(p1, monitor));

    assertEquals(1, events.size());
    event = events.get(0);
    assertEquals(MavenProjectChangedEvent.KIND_REMOVED, event.getKind());
    assertSame(f1, event.getOldMavenProject());
    assertNull(event.getMavenProject());
  }
  public void test015_refreshOffline() throws Exception {
    // XXX fix this test on Windows and remove this condition
    if (System.getProperty("os.name", "").toLowerCase().indexOf("windows") > -1) {
      return;
    }

    IProject p1 = createExisting("t015-p1");
    waitForJobsToComplete();

    File file = new File(repo, "junit/junit/3.8.1/junit-3.8.1.jar");
    assertTrue("Can't delete file " + file.getAbsolutePath(), !file.exists() || file.delete());

    MavenUpdateRequest updateRequest =
        new MavenUpdateRequest(true /*offline*/, false /* updateSources */);
    updateRequest.addPomFile(p1);
    manager.refresh(updateRequest, monitor);
    assertEquals(false, file.exists());

    {
      IMavenProjectFacade f1 = manager.create(p1, monitor);
      List<Artifact> a1 = new ArrayList<Artifact>(getMavenProjectArtifacts(f1));
      assertEquals(false, a1.get(0).isResolved());
    }

    updateRequest = new MavenUpdateRequest(false /*offline*/, false /* updateSources */);
    updateRequest.addPomFile(p1);
    manager.refresh(updateRequest, monitor);
    assertEquals(true, file.exists());

    {
      IMavenProjectFacade f1 = manager.create(p1, monitor);
      List<Artifact> a1 = new ArrayList<Artifact>(getMavenProjectArtifacts(f1));
      assertEquals(true, a1.get(0).isResolved());
    }
  }
  public void test000_deletePom() throws Exception {
    IProject p1 = createExisting("t000-p1");
    waitForJobsToComplete();

    assertNotNull(manager.create(p1, monitor));

    deleteProject(p1);
    waitForJobsToComplete();

    assertNull(manager.create(p1, monitor));
  }
  public void test000_removeDeleted() throws Exception {
    IProject p1 = createExisting("t000-p1");
    waitForJobsToComplete();

    IMavenProjectFacade f1 = manager.create(p1, monitor);
    assertEquals(p1.getFullPath(), f1.getFullPath());

    deleteProject(p1);
    waitForJobsToComplete();

    assertNull(manager.create(p1, monitor));
  }
  public void test013_cantParsePomMarker() throws Exception {
    IProject project = createExisting("t013-p1");
    waitForJobsToComplete();

    assertNull(manager.create(project, monitor));
    WorkspaceHelpers.assertErrorMarker(
        IMavenConstants.MARKER_POM_LOADING_ID,
        "Project build error: Non-readable POM ",
        1 /*lineNumber*/,
        project);

    copyContent(project, "pom_good.xml", "pom.xml");
    WorkspaceHelpers.assertNoErrors(project);
    assertNotNull(manager.create(project, monitor));
  }
  /** This test disabled until https://issues.sonatype.org/browse/MNGECLIPSE-1448 is resolved */
  public void _testExtensionPluginResolution() throws Exception {
    IProject p1 = createExisting("MNGECLIPSE380-plugin", "resources/MNGECLIPSE380/plugin");
    IProject p2 = createExisting("MNGECLIPSE380-project", "resources/MNGECLIPSE380/project");
    waitForJobsToComplete();

    p1.build(IncrementalProjectBuilder.FULL_BUILD, monitor);

    IFile pom2 = p2.getFile("pom.xml");
    assertNotNull(manager.create(pom2, false, null));

    deleteProject(p1);
    waitForJobsToComplete();
    assertNull(manager.create(pom2, false, null));

    assertNotNull(manager.create(pom2, false, null));
  }
  protected void setUp() throws Exception {
    super.setUp();

    manager = MavenPlugin.getDefault().getMavenProjectManagerImpl();

    events = new ArrayList<MavenProjectChangedEvent>();
    manager.addMavenProjectChangedListener(listener);
  }
  public void test021_dependencyVersionRange() throws Exception {
    IProject p1 = importProject("resources/t021_dependencyVersionRange/t021-p1/pom.xml");
    waitForJobsToComplete();

    MavenProjectFacade f1 = manager.create(p1, monitor);
    List<Artifact> a1 = new ArrayList<Artifact>(f1.getMavenProject(monitor).getArtifacts());
    assertEquals(1, a1.size());
    assertStartWith(repo.getAbsolutePath(), a1.get(0).getFile().getAbsolutePath());

    IProject p2 = importProject("resources/t021_dependencyVersionRange/t021-p2/pom.xml");
    waitForJobsToComplete();

    f1 = manager.create(p1, monitor);
    a1 = new ArrayList<Artifact>(f1.getMavenProject(monitor).getArtifacts());
    assertEquals(1, a1.size());
    assertEquals(
        p2.getFile(IMavenConstants.POM_FILE_NAME).getLocation().toFile(), a1.get(0).getFile());
  }
 protected void tearDown() throws Exception {
   try {
     manager.removeMavenProjectChangedListener(listener);
     listener = null;
     events = null;
     manager = null;
   } finally {
     super.tearDown();
   }
 }
  public void test001_dependencyOnModuleWithInheritedGroupAndVersion() throws Exception {
    IProject p1 = createExisting("t001-p1");
    IProject p2 = createExisting("t001-p2");
    IProject p3 = createExisting("t001-p3");
    waitForJobsToComplete();

    IMavenProjectFacade f1 = manager.create(p1, monitor);
    Set<Artifact> artifacts = getMavenProjectArtifacts(f1);
    assertEquals(1, artifacts.size());
    Artifact a1 = artifacts.iterator().next();
    assertEquals(true, a1.isResolved());
    assertEquals(p2.getFile(IMavenConstants.POM_FILE_NAME).getLocation().toFile(), a1.getFile());

    IMavenProjectFacade f2 = manager.create(p2, monitor);
    assertNotNull(f2);

    IMavenProjectFacade f3 = manager.create(p3, monitor);
    assertNotNull(f3);
  }
  public void test000_simple() throws Exception {
    IProject p1 = createExisting("t000-p1");
    waitForJobsToComplete();

    IMavenProjectFacade f1 = manager.create(p1, monitor);
    assertEquals(p1.getFullPath(), f1.getFullPath());

    assertEquals("t000", f1.getMavenProject(monitor).getGroupId());
    assertEquals("t000-p1", f1.getMavenProject(monitor).getArtifactId());
    assertEquals("0.0.1-SNAPSHOT", f1.getMavenProject(monitor).getVersion());
  }
  public void test006_parentAvailableFromLocalRepoAndWorkspace() throws Exception {
    IProject p1 = createExisting("t006-p1");
    IProject p2 = createExisting("t006-p2");
    waitForJobsToComplete();

    IMavenProjectFacade f1 = manager.create(p1, monitor);
    getParentProject(f1);
    assertEquals(
        p2.getFile(IMavenConstants.POM_FILE_NAME).getLocation().toFile(),
        f1.getMavenProject(monitor).getParentArtifact().getFile());

    deleteProject(p2);
    waitForJobsToComplete();

    f1 = manager.create(p1, monitor);
    getParentProject(f1);
    // assertTrue(f1.getMavenProject().getParent().getFile().getAbsolutePath().startsWith(repo.getAbsolutePath()));
    assertStartWith(
        repo.getAbsolutePath(),
        f1.getMavenProject(monitor).getParentArtifact().getFile().getAbsolutePath());
  }
  public void test017_moduleRefresh() throws Exception {
    IProject p2 = createExisting("t017-p2");
    IProject p3 = createExisting("t017-p3");
    IProject p4 = createExisting("t017-p4");
    waitForJobsToComplete();

    IFile pom = p3.getFile("pom.xml");

    MavenProject mavenProject = manager.create(pom, false, null).getMavenProject(monitor);
    Artifact a = mavenProject.getArtifacts().iterator().next();
    assertTrue(a.isResolved());
    assertEquals(
        p4.getFile(IMavenConstants.POM_FILE_NAME).getLocation().toFile().getAbsoluteFile(),
        a.getFile().getAbsoluteFile());

    copyContent(p2, "pom_changed.xml", "pom.xml");

    mavenProject = manager.create(pom, false, null).getMavenProject(monitor);
    a = mavenProject.getArtifacts().iterator().next();
    assertFalse(a.isResolved());
  }
  public void testJdkProfileActivation() throws Exception {
    IProject[] projects =
        importProjects(
            "projects/jdkprofileactivation",
            new String[] {"p001/pom.xml", "p002/pom.xml"},
            new ResolverConfiguration());
    waitForJobsToComplete();

    IMavenProjectFacade f1 = manager.create(projects[0], monitor);
    ArrayList<ArtifactRef> a1 = new ArrayList<ArtifactRef>(f1.getMavenProjectArtifacts());
    assertEquals(2, a1.size());
    assertEquals("p002", a1.get(0).getArtifactId());
  }
  public void test020_moduleWithPomErrors() throws Exception {
    IProject p1 = createExisting("t020-p1");
    IProject p1m1 = createExisting("t020-p1-m1");
    waitForJobsToComplete();

    copyContent(p1, "pom_updated.xml", "pom.xml");

    IFile pom11 = p1m1.getFile("pom.xml");
    IMavenProjectFacade f11 = manager.create(pom11, false, null);

    Artifact a = f11.getMavenProject(monitor).getArtifacts().iterator().next();
    assertEquals("3.8.1", a.getVersion());
  }
  public void testPropertiesSubstitution() throws Exception {
    IProject p1 = createExisting("t019-p1");
    waitForJobsToComplete();

    p1.build(IncrementalProjectBuilder.FULL_BUILD, monitor);

    IMavenProjectFacade f1 = manager.create(p1, monitor);
    MavenProject m1 = f1.getMavenProject(monitor);

    assertEquals("t019-p1", m1.getArtifactId());
    assertEquals("1.0.0-SNAPSHOT", m1.getVersion());
    assertEquals("plain description", m1.getDescription());
  }
  public void test007_newTransitiveDependency() throws Exception {
    // p1 depends on p2
    IProject p1 = createExisting("t007-p1");
    IProject p2 = createExisting("t007-p2");
    waitForJobsToComplete();

    IMavenProjectFacade f1 = manager.create(p1, monitor);
    assertEquals(1, f1.getMavenProject(monitor).getArtifacts().size());

    events.clear();

    // update p2 to have new dependency on junit
    copyContent(p2, "pom_newDependency.xml", "pom.xml");

    f1 = manager.create(p1, monitor);
    List<Artifact> a1 = new ArrayList<Artifact>(f1.getMavenProject(monitor).getArtifacts());
    assertEquals(2, a1.size());
    assertEquals("t007-p2", a1.get(0).getArtifactId());
    assertEquals("junit", a1.get(1).getArtifactId());

    assertEquals(2, events.size());
  }
  public void test000_pom_simple() throws Exception {
    IProject p1 = createExisting("t000-p1");
    waitForJobsToComplete();

    IFile pom = p1.getFile(IMavenConstants.POM_FILE_NAME);

    IMavenProjectFacade f1 = manager.create(pom, false, null);
    assertEquals(p1.getFullPath(), f1.getFullPath());

    assertEquals("t000", f1.getMavenProject(monitor).getGroupId());
    assertEquals("t000-p1", f1.getMavenProject(monitor).getArtifactId());
    assertEquals("0.0.1-SNAPSHOT", f1.getMavenProject(monitor).getVersion());
  }
 public void testWorkspaceResolutionApi() throws Exception {
   IProject[] projects =
       importProjects(
           "projects/simple-pom", new String[] {"pom.xml"}, new ResolverConfiguration());
   assertEquals(1, projects.length);
   assertNotNull(projects[0]);
   Artifact artifact =
       new DefaultArtifact(
           "org.eclipse.m2e.projects", "simple-pom", "1.0.0", null, "pom", "", null);
   artifact = manager.getWorkspaceLocalRepository().find(artifact);
   assertTrue(artifact.isResolved());
   assertNotNull(artifact.getFile());
   assertEquals(projects[0].getFile("pom.xml").getLocation().toFile(), artifact.getFile());
 }
  public void testWorkspaceDependencyVersionRange() throws Exception {
    IProject[] projects =
        importProjects(
            "projects/versionrange",
            new String[] {"p001/pom.xml", "p002/pom.xml"},
            new ResolverConfiguration());
    waitForJobsToComplete();

    IMavenProjectFacade f1 = manager.create(projects[0], monitor);
    MavenProject p1 = f1.getMavenProject(monitor);
    ArrayList<Artifact> a1 = new ArrayList<Artifact>(p1.getArtifacts());
    assertEquals(1, a1.size());

    assertEquals(projects[1].getLocation().append("target/classes").toFile(), a1.get(0).getFile());
  }
  public void test001_missingParent() throws Exception {
    IProject p2 = createExisting("t001-p2");
    waitForJobsToComplete();

    assertNull(manager.create(p2, monitor));

    IMarker[] markers = p2.findMarkers(null, true, IResource.DEPTH_INFINITE);
    assertEquals(WorkspaceHelpers.toString(markers), 1, markers.length);
    WorkspaceHelpers.assertErrorMarker(
        IMavenConstants.MARKER_POM_LOADING_ID,
        "Project build error: Non-resolvable parent POM: Failure to find t001:t001-p3:pom:0.0.1-SNAPSHOT in file:remoterepo was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced and 'parent.relativePath' points at wrong local POM",
        1 /*lineNumber*/,
        markers[0]);

    IProject p3 = createExisting("t001-p3");
    waitForJobsToComplete();

    assertNotNull(manager.create(p2, monitor));

    deleteProject(p3);
    waitForJobsToComplete();

    assertNull(manager.create(p2, monitor));
  }
  public void testEnvironmentVariablesConsidered() throws Exception {
    String tmpDir = System.getenv("TEMP");
    assertTrue("This test requires the environment variable TEMP to be set", tmpDir != null);

    File systemJar = new File("projects/MNGECLIPSE-581/mngeclipse-581.jar");
    File tempJar = new File(tmpDir, "mngeclipse-581.jar");
    FileUtils.copyFile(systemJar, tempJar);
    tempJar.deleteOnExit();

    IProject[] projects =
        importProjects(
            "projects/MNGECLIPSE-581", new String[] {"pom.xml"}, new ResolverConfiguration());
    assertEquals(1, projects.length);
    assertNotNull(projects[0]);
    IMavenProjectFacade facade = manager.getProject(projects[0]);
    assertNotNull(facade);
    MavenProject project = facade.getMavenProject();
    assertNotNull(project);
    File file = project.getArtifacts().iterator().next().getFile();
    assertTrue(file.toString(), file.isFile());
  }
  public void test012_downloadExternalDependency() throws Exception {
    IProject p1 = createExisting("t012-p1");
    waitForJobsToComplete();

    File jar = new File(repo + "/log4j/log4j/1.2.13", "log4j-1.2.13.jar").getAbsoluteFile();
    //    assertTrue(jar.exists());

    jar.delete();

    MavenUpdateRequest request =
        new MavenUpdateRequest(p1, false /*offline*/, false /*updateSnapshots*/);
    plugin.getMavenProjectManager().refresh(request);
    waitForJobsToComplete();

    IMavenProjectFacade f1 = manager.create(p1, monitor);

    Artifact[] a1 = getMavenProjectArtifacts(f1).toArray(new Artifact[0]);
    assertEquals(1, a1.length);
    assertEquals(jar, a1[0].getFile());
    assertTrue(jar.exists());
  }