예제 #1
0
  @Test
  public void testGetArtifact() {
    final DbArtifact dbArtifact = new DbArtifact();
    dbArtifact.setGroupId("groupId");
    dbArtifact.setArtifactId("artifactId");
    dbArtifact.setVersion("1.0.0-SNAPSHOT");
    dbArtifact.setClassifier("win");
    dbArtifact.setType("component");
    dbArtifact.setExtension("jar");
    dbArtifact.setDownloadUrl("nowhere");
    dbArtifact.setSize("10Mo");
    dbArtifact.setProvider("provider");

    final DbLicense license = new DbLicense();
    license.setName("licenseId");
    dbArtifact.addLicense(license);

    final ModelMapper modelMapper = new ModelMapper(mock(RepositoryHandler.class));
    final Artifact artifact = modelMapper.getArtifact(dbArtifact);

    assertEquals(dbArtifact.getGroupId(), artifact.getGroupId());
    assertEquals(dbArtifact.getArtifactId(), artifact.getArtifactId());
    assertEquals(dbArtifact.getVersion(), artifact.getVersion());
    assertEquals(dbArtifact.getClassifier(), artifact.getClassifier());
    assertEquals(dbArtifact.getType(), artifact.getType());
    assertEquals(dbArtifact.getExtension(), artifact.getExtension());
    assertEquals(dbArtifact.getSize(), artifact.getSize());
    assertEquals(dbArtifact.getDownloadUrl(), artifact.getDownloadUrl());
    assertEquals(dbArtifact.getProvider(), artifact.getProvider());
    assertEquals(1, artifact.getLicenses().size());
    assertEquals("licenseId", artifact.getLicenses().get(0));
  }
예제 #2
0
  @Test
  public void testGetDbArtifact() {
    final Artifact artifact =
        DataModelFactory.createArtifact(
            "groupId", "artifactId", "version", "classifier", "type", "extension");
    artifact.setSize("10Mo");
    artifact.setDownloadUrl("http://www.nowhere.com");
    artifact.setProvider("http://www.nowhere.com/provider");

    final ModelMapper modelMapper = new ModelMapper(mock(RepositoryHandler.class));
    final DbArtifact dbArtifact = modelMapper.getDbArtifact(artifact);

    assertEquals(artifact.getGroupId(), dbArtifact.getGroupId());
    assertEquals(artifact.getArtifactId(), dbArtifact.getArtifactId());
    assertEquals(artifact.getVersion(), dbArtifact.getVersion());
    assertEquals(artifact.getClassifier(), dbArtifact.getClassifier());
    assertEquals(artifact.getType(), dbArtifact.getType());
    assertEquals(artifact.getExtension(), dbArtifact.getExtension());
    assertEquals(artifact.getSize(), dbArtifact.getSize());
    assertEquals(artifact.getDownloadUrl(), dbArtifact.getDownloadUrl());
    assertEquals(artifact.getProvider(), dbArtifact.getProvider());
  }
예제 #3
0
  @Override
  public void run() {
    File destinationFile =
        new File(
            destination,
            artifact.getArtifactId()
                + ((options.isStripVersions()) ? "" : "-" + artifact.getVersion())
                + (artifact.getClassifier().isEmpty() ? "" : ("-" + artifact.getClassifier()))
                + ".jar");
    for (String server : options.getServers()) {
      URL u = artifact.toURL(server);
      HttpURLConnection con = null;
      BufferedInputStream bis = null;
      BufferedOutputStream bos = null;

      try {
        if (!isUpToDate(u, destinationFile)) {
          con = URLSupport.openURL(u, options.getProxyServer());
          con.setConnectTimeout(CONNECTION_TIMEOUT);
          con.connect();

          bis = new BufferedInputStream(con.getInputStream());
          bos = new BufferedOutputStream(new FileOutputStream(destinationFile));
          Deque<TransferBuffer> dq = new ArrayDeque<TransferBuffer>();

          ArtifactReader r =
              new ArtifactReader(
                  project,
                  bis,
                  dq,
                  BUFFER_SIZE,
                  options.isCheckSHADigests() && !artifact.getDigest().isEmpty());
          Thread rt = new Thread(r);
          rt.start();

          ArtifactWriter w = new ArtifactWriter(project, bos, dq);
          Thread wt = new Thread(w);
          wt.start();

          rt.join();
          wt.join();

          if (r.wasSuccessful() && w.wasSuccessful()) {
            if (options.isCheckSHADigests() && !artifact.getDigest().isEmpty()) {
              if (!digestEquals(artifact, r.getDigest())) {
                artifact.setStatus(Artifact.Status.DIGEST_MISMATCH);
                destinationFile.deleteOnExit();
                project.log(
                    "download failed with incorrect digest: "
                        + artifact
                        + " - actual: "
                        + byteArrayDigestToHexString(r.getDigest()),
                    Project.MSG_ERR);
                return;
              }
            }
            artifact.setStatus(Artifact.Status.DOWNLOADED);
          }
        } else {
          artifact.setStatus(Artifact.Status.UPTODATE);
        }

        project.log(
            "download successful: " + artifact,
            (artifact.getStatus() != Artifact.Status.UPTODATE)
                ? Project.MSG_ERR
                : Project.MSG_VERBOSE);
        return;
      } catch (Exception e) {
        if (!YankTask.SOURCE_CLASSIFIER.equals(artifact.getClassifier())) {
          project.log(e.getMessage(), e, Project.MSG_VERBOSE);
          project.log("download failed: " + artifact, Project.MSG_ERR);
        }
        artifact.setStatus(Artifact.Status.FAILED);
      } finally {
        Closer.close(bis);
        Closer.close(bos);
        Closer.close(con);
      }
    }
  }