public void dotestArtifactExceptions(boolean are, boolean anfe) throws Exception {
    ArtifactItem item = new ArtifactItem();

    item.setArtifactId("artifactId");
    item.setClassifier("");
    item.setGroupId("groupId");
    item.setType("type");
    item.setVersion("1.0");

    List<ArtifactItem> list = new ArrayList<ArtifactItem>();
    list.add(item);
    mojo.setArtifactItems(list);

    // init classifier things
    mojo.setFactory(DependencyTestUtils.getArtifactFactory());
    mojo.setResolver(new StubArtifactResolver(null, are, anfe));
    mojo.setLocal(new StubArtifactRepository(this.testDir.getAbsolutePath()));

    try {
      mojo.execute();
      fail("ExpectedException");
    } catch (MojoExecutionException e) {
      if (are) {
        assertEquals("Unable to resolve artifact.", e.getMessage());
      } else {
        assertEquals("Unable to find artifact.", e.getMessage());
      }
    }
  }
  public void testMissingVersionFromDependencyMgtWithClassifier() throws Exception {
    ArtifactItem item = new ArtifactItem();

    item.setArtifactId("artifactId");
    item.setClassifier("classifier");
    item.setGroupId("groupId");
    item.setType("jar");

    MavenProject project = mojo.getProject();
    project.setDependencies(getDependencyList(item));

    item = new ArtifactItem();

    item.setArtifactId("artifactId-2");
    item.setClassifier("classifier");
    item.setGroupId("groupId");
    item.setType("jar");

    List<ArtifactItem> list = new ArrayList<ArtifactItem>();
    list.add(item);

    mojo.setArtifactItems(list);

    project.getDependencyManagement().setDependencies(getDependencyMgtList(item));

    mojo.execute();

    assertMarkerFile(true, item);
    assertEquals("3.1", item.getVersion());
  }
  public void testUnpackFile() throws Exception {
    List<ArtifactItem> list = stubFactory.getArtifactItems(stubFactory.getClassifiedArtifacts());

    mojo.setArtifactItems(list);

    mojo.execute();

    assertMarkerFiles(list, true);
  }
  protected void setUp() throws Exception {
    super.setUp("unpack", true);

    File testPom = new File(getBasedir(), "target/test-classes/unit/unpack-test/plugin-config.xml");
    mojo = (UnpackMojo) lookupMojo("unpack", testPom);
    mojo.setOutputDirectory(new File(this.testDir, "outputDirectory"));
    mojo.setMarkersDirectory(new File(this.testDir, "markers"));
    setSilent(mojo, true);

    assertNotNull(mojo);
    assertNotNull(mojo.getProject());
    // MavenProject project = mojo.getProject();
    // init classifier things
    // it needs to get the archivermanager
    stubFactory.setUnpackableFile(mojo.getArchiverManager());
    // i'm using one file repeatedly to archive so I can test the name
    // programmatically.
    stubFactory.setSrcFile(
        new File(
            getBasedir()
                + File.separatorChar
                + "target/test-classes/unit/unpack-dependencies-test/test.txt"));

    mojo.setFactory(DependencyTestUtils.getArtifactFactory());
    mojo.setResolver(new StubArtifactResolver(stubFactory, false, false));
    mojo.setLocal(new StubArtifactRepository(this.testDir.getAbsolutePath()));
    mojo.setArtifactCollector(new StubArtifactCollector());
    mojo.setUseJvmChmod(true);
  }
  public void testUnpackToLocation() throws Exception {
    List<ArtifactItem> list = stubFactory.getArtifactItems(stubFactory.getClassifiedArtifacts());
    ArtifactItem item = list.get(0);
    item.setOutputDirectory(new File(mojo.getOutputDirectory(), "testOverride"));

    mojo.setArtifactItems(list);

    mojo.execute();

    assertMarkerFiles(list, true);
  }
  public void testUnpackOverWriteIfNewer() throws Exception {
    final long now = System.currentTimeMillis();

    setSilent(mojo, false);
    stubFactory.setCreateFiles(true);
    Artifact artifact = stubFactory.getSnapshotArtifact();
    assertTrue(artifact.getFile().setLastModified(now - 20000));

    ArtifactItem item = new ArtifactItem(artifact);

    List<ArtifactItem> list = Collections.singletonList(item);
    mojo.setArtifactItems(list);
    mojo.setOverWriteIfNewer(true);
    mojo.execute();
    File unpackedFile = getUnpackedFile(item);

    // round down to the last second
    long time = now;
    time = time - (time % 1000);
    // go back 10 more seconds for linux
    time -= 10000;
    // set to known value
    assertTrue(unpackedFile.setLastModified(time));
    // set source to be newer was 4s but test is brittle on MacOS if less than 5s
    assertTrue(artifact.getFile().setLastModified(time + 5000));

    // manually set markerfile (must match getMarkerFile in DefaultMarkerFileHandler)
    File marker =
        new File(mojo.getMarkersDirectory(), artifact.getId().replace(':', '-') + ".marker");
    assertTrue(marker.setLastModified(time));

    displayFile("unpackedFile", unpackedFile);
    displayFile("artifact    ", artifact.getFile());
    displayFile("marker      ", marker);
    System.out.println("mojo.execute()");
    mojo.execute();
    displayFile("unpackedFile", unpackedFile);
    displayFile("artifact    ", artifact.getFile());
    displayFile("marker      ", marker);
    System.out.println("marker.lastModified() = " + marker.lastModified());
    System.out.println("unpackedFile.lastModified() = " + unpackedFile.lastModified());
    assertTrue(
        "unpackedFile '"
            + unpackedFile
            + "' lastModified() == "
            + marker.lastModified()
            + ": should be different",
        marker.lastModified() != unpackedFile.lastModified());
  }
  public void testUnpackOverWriteReleases() throws Exception {
    stubFactory.setCreateFiles(true);
    Artifact release = stubFactory.getReleaseArtifact();
    release.getFile().setLastModified(System.currentTimeMillis() - 2000);

    ArtifactItem item = new ArtifactItem(release);

    List<ArtifactItem> list = new ArrayList<ArtifactItem>(1);
    list.add(item);
    mojo.setArtifactItems(list);

    mojo.setOverWriteIfNewer(false);
    mojo.setOverWriteReleases(true);
    mojo.execute();

    assertUnpacked(item, true);
  }
 public void testNoArtifactItems() {
   try {
     mojo.getProcessedArtifactItems(false);
     fail("Expected Exception");
   } catch (MojoExecutionException e) {
     assertEquals("There are no artifactItems configured.", e.getMessage());
   }
 }
 public void assertMarkerFile(boolean val, ArtifactItem item) {
   UnpackFileMarkerHandler handle = new UnpackFileMarkerHandler(item, mojo.getMarkersDirectory());
   try {
     assertEquals(val, handle.isMarkerSet());
   } catch (MojoExecutionException e) {
     fail(e.getLongMessage());
   }
 }
  public void testMissingVersionNotFound() throws Exception {
    ArtifactItem item = new ArtifactItem();

    item.setArtifactId("artifactId");
    item.setClassifier("");
    item.setGroupId("groupId");
    item.setType("type");

    List<ArtifactItem> list = new ArrayList<ArtifactItem>();
    list.add(item);
    mojo.setArtifactItems(list);

    try {
      mojo.execute();
      fail("Expected Exception Here.");
    } catch (MojoExecutionException e) {
      // caught the expected exception.
    }
  }
  public void testGetArtifactItems() throws MojoExecutionException {

    ArtifactItem item = new ArtifactItem();

    item.setArtifactId("artifact");
    item.setGroupId("groupId");
    item.setVersion("1.0");

    ArrayList<ArtifactItem> list = new ArrayList<ArtifactItem>(1);
    list.add(item);

    mojo.setArtifactItems(list);

    ArtifactItem result = getSingleArtifactItem(false);
    assertEquals(mojo.getOutputDirectory(), result.getOutputDirectory());

    File output = new File(mojo.getOutputDirectory(), "override");
    item.setOutputDirectory(output);
    result = getSingleArtifactItem(false);
    assertEquals(output, result.getOutputDirectory());
  }
  public void assertUnpacked(ArtifactItem item, boolean overWrite) throws Exception {

    File unpackedFile = getUnpackedFile(item);

    Thread.sleep(100);
    // round down to the last second
    long time = System.currentTimeMillis();
    time = time - (time % 1000);
    unpackedFile.setLastModified(time);

    assertEquals(time, unpackedFile.lastModified());
    mojo.execute();

    if (overWrite) {
      assertTrue(time != unpackedFile.lastModified());
    } else {
      assertEquals(time, unpackedFile.lastModified());
    }
  }
 public ArtifactItem getSingleArtifactItem(boolean removeVersion) throws MojoExecutionException {
   List<ArtifactItem> list = mojo.getProcessedArtifactItems(removeVersion);
   return list.get(0);
 }