@Test(expected = InstallationException.class)
  public void testNonExistentMetadataFile() throws InstallationException {
    InstallRequest request = new InstallRequest();
    request.addMetadata(metadata.setFile(new File("missing.xml")));

    installer.install(session, request);
  }
  @Test
  public void testSetArtifactTimestamps() throws InstallationException {
    artifact.getFile().setLastModified(artifact.getFile().lastModified() - 60000);

    request.addArtifact(artifact);

    installer.install(session, request);

    assertEquals(
        "artifact timestamp was not set to src file",
        artifact.getFile().lastModified(),
        localArtifactFile.lastModified());

    request = new InstallRequest();

    request.addArtifact(artifact);

    artifact.getFile().setLastModified(artifact.getFile().lastModified() - 60000);

    installer.install(session, request);

    assertEquals(
        "artifact timestamp was not set to src file",
        artifact.getFile().lastModified(),
        localArtifactFile.lastModified());
  }
  @Test(expected = InstallationException.class)
  public void testNonExistentArtifactFile() throws InstallationException {
    InstallRequest request = new InstallRequest();
    request.addArtifact(artifact.setFile(new File("missing.txt")));

    installer.install(session, request);
  }
  @Test(expected = InstallationException.class)
  public void testNullMetadataFile() throws InstallationException {
    InstallRequest request = new InstallRequest();
    request.addMetadata(metadata.setFile(null));

    installer.install(session, request);
  }
  @Test(expected = InstallationException.class)
  public void testNullArtifactFile() throws InstallationException {
    InstallRequest request = new InstallRequest();
    request.addArtifact(artifact.setFile(null));

    installer.install(session, request);
  }
  @Test
  public void testSuccessfulMetadataEvents() throws InstallationException {
    InstallRequest request = new InstallRequest();
    request.addMetadata(metadata);

    installer.install(session, request);
    checkEvents("Repository Event problem", metadata, false);
  }
  @Test
  public void testSuccessfulArtifactEvents() throws InstallationException {
    InstallRequest request = new InstallRequest();
    request.addArtifact(artifact);

    installer.install(session, request);
    checkEvents("Repository Event problem", artifact, false);
  }
  @Test
  public void testDoNotUpdateUnchangedArtifact() throws InstallationException {
    request.addArtifact(artifact);
    installer.install(session, request);

    installer.setFileProcessor(
        new DefaultFileProcessor() {
          @Override
          public long copy(File src, File target, ProgressListener listener) throws IOException {
            throw new IOException("copy called");
          }
        });

    request = new InstallRequest();
    request.addArtifact(artifact);
    installer.install(session, request);
  }
  @Override
  protected void doExecute() throws MojoExecutionException, MojoFailureException {
    final MavenProject project = buildContext.getSession().getCurrentProject();

    @SuppressWarnings("unchecked")
    final Collection<Artifact> artifacts =
        (Collection<Artifact>) project.getContextValue("osgified-artifacts");

    final RepositorySystemSession session = buildContext.getRepositorySession();

    final InstallRequest installRequest = new InstallRequest();
    installRequest.setArtifacts(artifacts);
    try {
      repositorySystem.install(session, installRequest);
    } catch (InstallationException e) {
      throw pipe(e);
    }
  }
  @Test(expected = InstallationException.class)
  public void testMetadataDestinationEqualsSource() throws Exception {
    String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
    File file = new File(session.getLocalRepository().getBasedir(), path);
    metadata = metadata.setFile(file);
    TestFileUtils.writeString(file, "test");

    request.addMetadata(metadata);
    installer.install(session, request);
  }
  @Test(expected = InstallationException.class)
  public void testMetadataExistsAsDir() throws InstallationException {
    String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
    assertTrue(
        "failed to setup test: could not create " + path,
        new File(session.getLocalRepository().getBasedir(), path).mkdirs());

    request.addMetadata(metadata);
    installer.install(session, request);
  }
  @Test(expected = InstallationException.class)
  public void testArtifactExistsAsDir() throws InstallationException {
    String path = session.getLocalRepositoryManager().getPathForLocalArtifact(artifact);
    File file = new File(session.getLocalRepository().getBasedir(), path);
    assertFalse(file.getAbsolutePath() + " is a file, not directory", file.isFile());
    assertFalse(file.getAbsolutePath() + " already exists", file.exists());
    assertTrue(
        "failed to setup test: could not create " + file.getAbsolutePath(),
        file.mkdirs() || file.isDirectory());

    request.addArtifact(artifact);
    installer.install(session, request);
  }
  @Test
  public void testSuccessfulInstall()
      throws InstallationException, UnsupportedEncodingException, IOException {
    File artifactFile =
        new File(
            session.getLocalRepositoryManager().getRepository().getBasedir(), localArtifactPath);
    File metadataFile =
        new File(
            session.getLocalRepositoryManager().getRepository().getBasedir(), localMetadataPath);

    artifactFile.delete();
    metadataFile.delete();

    request.addArtifact(artifact);
    request.addMetadata(metadata);

    InstallResult result = installer.install(session, request);

    assertTrue(artifactFile.exists());
    assertEquals("artifact", TestFileUtils.readString(artifactFile));

    assertTrue(metadataFile.exists());
    assertEquals("metadata", TestFileUtils.readString(metadataFile));

    assertEquals(result.getRequest(), request);

    assertEquals(result.getArtifacts().size(), 1);
    assertTrue(result.getArtifacts().contains(artifact));

    assertEquals(result.getMetadata().size(), 1);
    assertTrue(result.getMetadata().contains(metadata));

    assertEquals(1, lrm.getMetadataRegistration().size());
    assertTrue(lrm.getMetadataRegistration().contains(metadata));
    assertEquals(1, lrm.getArtifactRegistration().size());
    assertTrue(lrm.getArtifactRegistration().contains(artifact));
  }