Search in sources :

Example 1 with VersionComparator

use of org.apache.archiva.common.utils.VersionComparator in project archiva by apache.

the class MetadataToolsTest method assertSnapshotVersions.

private void assertSnapshotVersions(String artifactId, String version, String[] expectedVersions) throws Exception {
    Path repoRootDir = Paths.get("src/test/repositories/metadata-repository");
    VersionedReference reference = new VersionedReference();
    reference.setGroupId("org.apache.archiva.metadata.tests");
    reference.setArtifactId(artifactId);
    reference.setVersion(version);
    MavenManagedRepository repo = createRepository("test-repo", "Test Repository: " + name.getMethodName(), repoRootDir);
    RepositoryContentProvider provider = applicationContext.getBean("repositoryContentProvider#maven", RepositoryContentProvider.class);
    ManagedRepositoryContent repoContent = provider.createManagedContent(repo);
    Set<String> testedVersionSet = tools.gatherSnapshotVersions(repoContent, reference);
    // Sort the list (for asserts)
    List<String> testedVersions = new ArrayList<>();
    testedVersions.addAll(testedVersionSet);
    Collections.sort(testedVersions, new VersionComparator());
    // Test the expected array of versions, to the actual tested versions
    assertEquals("Assert Snapshot Versions: length/size", expectedVersions.length, testedVersions.size());
    for (int i = 0; i < expectedVersions.length; i++) {
        String actualVersion = testedVersions.get(i);
        assertEquals("Snapshot Versions[" + i + "]", expectedVersions[i], actualVersion);
    }
}
Also used : Path(java.nio.file.Path) VersionedReference(org.apache.archiva.model.VersionedReference) RepositoryContentProvider(org.apache.archiva.repository.RepositoryContentProvider) MavenManagedRepository(org.apache.archiva.repository.maven2.MavenManagedRepository) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) ArrayList(java.util.ArrayList) VersionComparator(org.apache.archiva.common.utils.VersionComparator)

Example 2 with VersionComparator

use of org.apache.archiva.common.utils.VersionComparator in project archiva by apache.

the class MetadataTools method updateMetadata.

/**
 * Update the metadata based on the following rules.
 * <p>
 * 1) If this is a SNAPSHOT reference, then utilize the proxy/repository specific
 * metadata files to represent the current / latest SNAPSHOT available.
 * 2) If this is a RELEASE reference, and the metadata file does not exist, then
 * create the metadata file with contents required of the VersionedReference
 *
 * @param managedRepository the managed repository where the metadata is kept.
 * @param reference         the versioned reference to update
 * @throws LayoutException
 * @throws RepositoryMetadataException
 * @throws IOException
 * @throws ContentNotFoundException
 * @deprecated
 */
public void updateMetadata(ManagedRepositoryContent managedRepository, VersionedReference reference) throws LayoutException, RepositoryMetadataException, IOException, ContentNotFoundException {
    Path metadataFile = Paths.get(managedRepository.getRepoRoot(), toPath(reference));
    long lastUpdated = getExistingLastUpdated(metadataFile);
    ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
    metadata.setGroupId(reference.getGroupId());
    metadata.setArtifactId(reference.getArtifactId());
    if (VersionUtil.isSnapshot(reference.getVersion())) {
        // Do SNAPSHOT handling.
        metadata.setVersion(VersionUtil.getBaseVersion(reference.getVersion()));
        // Gather up all of the versions found in the reference dir, and any
        // proxied maven-metadata.xml files.
        Set<String> snapshotVersions = gatherSnapshotVersions(managedRepository, reference);
        if (snapshotVersions.isEmpty()) {
            throw new ContentNotFoundException("No snapshot versions found on reference [" + VersionedReference.toKey(reference) + "].");
        }
        // sort the list to determine to aide in determining the Latest version.
        List<String> sortedVersions = new ArrayList<>();
        sortedVersions.addAll(snapshotVersions);
        Collections.sort(sortedVersions, new VersionComparator());
        String latestVersion = sortedVersions.get(sortedVersions.size() - 1);
        if (VersionUtil.isUniqueSnapshot(latestVersion)) {
            // The latestVersion will contain the full version string "1.0-alpha-5-20070821.213044-8"
            // This needs to be broken down into ${base}-${timestamp}-${build_number}
            Matcher m = VersionUtil.UNIQUE_SNAPSHOT_PATTERN.matcher(latestVersion);
            if (m.matches()) {
                metadata.setSnapshotVersion(new SnapshotVersion());
                int buildNumber = NumberUtils.toInt(m.group(3), -1);
                metadata.getSnapshotVersion().setBuildNumber(buildNumber);
                Matcher mtimestamp = VersionUtil.TIMESTAMP_PATTERN.matcher(m.group(2));
                if (mtimestamp.matches()) {
                    String tsDate = mtimestamp.group(1);
                    String tsTime = mtimestamp.group(2);
                    long snapshotLastUpdated = toLastUpdatedLong(tsDate + tsTime);
                    lastUpdated = Math.max(lastUpdated, snapshotLastUpdated);
                    metadata.getSnapshotVersion().setTimestamp(m.group(2));
                }
            }
        } else if (VersionUtil.isGenericSnapshot(latestVersion)) {
            // The latestVersion ends with the generic version string.
            // Example: 1.0-alpha-5-SNAPSHOT
            metadata.setSnapshotVersion(new SnapshotVersion());
        /* Disabled due to decision in [MRM-535].
                 * Do not set metadata.lastUpdated to file.lastModified.
                 * 
                 * Should this be the last updated timestamp of the file, or in the case of an 
                 * archive, the most recent timestamp in the archive?
                 * 
                ArtifactReference artifact = getFirstArtifact( managedRepository, reference );

                if ( artifact == null )
                {
                    throw new IOException( "Not snapshot artifact found to reference in " + reference );
                }

                File artifactFile = managedRepository.toFile( artifact );

                if ( artifactFile.exists() )
                {
                    Date lastModified = new Date( artifactFile.lastModified() );
                    metadata.setLastUpdatedTimestamp( lastModified );
                }
                */
        } else {
            throw new RepositoryMetadataException("Unable to process snapshot version <" + latestVersion + "> reference <" + reference + ">");
        }
    } else {
        // Do RELEASE handling.
        metadata.setVersion(reference.getVersion());
    }
    // Set last updated
    if (lastUpdated > 0) {
        metadata.setLastUpdatedTimestamp(toLastUpdatedDate(lastUpdated));
    }
    // Save the metadata model to disk.
    RepositoryMetadataWriter.write(metadata, metadataFile);
    ChecksummedFile checksum = new ChecksummedFile(metadataFile);
    checksum.fixChecksums(algorithms);
}
Also used : Path(java.nio.file.Path) ContentNotFoundException(org.apache.archiva.repository.ContentNotFoundException) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) ChecksummedFile(org.apache.archiva.checksum.ChecksummedFile) SnapshotVersion(org.apache.archiva.model.SnapshotVersion) VersionComparator(org.apache.archiva.common.utils.VersionComparator) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Example 3 with VersionComparator

use of org.apache.archiva.common.utils.VersionComparator in project archiva by apache.

the class ManagedDefaultRepositoryContentTest method assertVersions.

private void assertVersions(String artifactId, String version, String[] expectedVersions) throws Exception {
    VersionedReference reference = new VersionedReference();
    reference.setGroupId("org.apache.archiva.metadata.tests");
    reference.setArtifactId(artifactId);
    reference.setVersion(version);
    // Use the test metadata-repository, which is already setup for
    // These kind of version tests.
    Path repoDir = Paths.get("src/test/repositories/metadata-repository");
    ((EditableManagedRepository) repoContent.getRepository()).setLocation(repoDir.toAbsolutePath().toUri());
    // Request the versions.
    Set<String> testedVersionSet = repoContent.getVersions(reference);
    // Sort the list (for asserts later)
    List<String> testedVersions = new ArrayList<>();
    testedVersions.addAll(testedVersionSet);
    Collections.sort(testedVersions, new VersionComparator());
    // Test the expected array of versions, to the actual tested versions
    assertEquals("Assert Versions: length/size", expectedVersions.length, testedVersions.size());
    for (int i = 0; i < expectedVersions.length; i++) {
        String actualVersion = testedVersions.get(i);
        assertEquals("Versions[" + i + "]", expectedVersions[i], actualVersion);
    }
}
Also used : Path(java.nio.file.Path) VersionedReference(org.apache.archiva.model.VersionedReference) EditableManagedRepository(org.apache.archiva.repository.EditableManagedRepository) ArrayList(java.util.ArrayList) VersionComparator(org.apache.archiva.common.utils.VersionComparator)

Example 4 with VersionComparator

use of org.apache.archiva.common.utils.VersionComparator in project archiva by apache.

the class ManagedDefaultRepositoryContentTest method assertGetVersions.

private void assertGetVersions(String artifactId, List<String> expectedVersions) throws Exception {
    ProjectReference reference = new ProjectReference();
    reference.setGroupId("org.apache.archiva.metadata.tests");
    reference.setArtifactId(artifactId);
    // Use the test metadata-repository, which is already setup for
    // These kind of version tests.
    Path repoDir = Paths.get("src/test/repositories/metadata-repository");
    ((EditableManagedRepository) repoContent.getRepository()).setLocation(repoDir.toAbsolutePath().toUri());
    // Request the versions.
    Set<String> testedVersionSet = repoContent.getVersions(reference);
    // Sort the list (for asserts)
    List<String> testedVersions = new ArrayList<>();
    testedVersions.addAll(testedVersionSet);
    Collections.sort(testedVersions, new VersionComparator());
    // Test the expected array of versions, to the actual tested versions
    assertEquals("available versions", expectedVersions, testedVersions);
}
Also used : Path(java.nio.file.Path) ProjectReference(org.apache.archiva.model.ProjectReference) EditableManagedRepository(org.apache.archiva.repository.EditableManagedRepository) ArrayList(java.util.ArrayList) VersionComparator(org.apache.archiva.common.utils.VersionComparator)

Aggregations

Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)4 VersionComparator (org.apache.archiva.common.utils.VersionComparator)4 VersionedReference (org.apache.archiva.model.VersionedReference)2 EditableManagedRepository (org.apache.archiva.repository.EditableManagedRepository)2 Matcher (java.util.regex.Matcher)1 ChecksummedFile (org.apache.archiva.checksum.ChecksummedFile)1 ArchivaRepositoryMetadata (org.apache.archiva.model.ArchivaRepositoryMetadata)1 ProjectReference (org.apache.archiva.model.ProjectReference)1 SnapshotVersion (org.apache.archiva.model.SnapshotVersion)1 ContentNotFoundException (org.apache.archiva.repository.ContentNotFoundException)1 ManagedRepositoryContent (org.apache.archiva.repository.ManagedRepositoryContent)1 RepositoryContentProvider (org.apache.archiva.repository.RepositoryContentProvider)1 MavenManagedRepository (org.apache.archiva.repository.maven2.MavenManagedRepository)1