Skip to content

Commit

Permalink
Make repository snapshot pruner stop deleting the checksums of maven-…
Browse files Browse the repository at this point in the history
…metadata.xml
  • Loading branch information
steinarb committed Oct 22, 2022
1 parent 23ff297 commit e47b835
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 Steinar Bang
* Copyright 2017-2022 Steinar Bang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,7 +57,7 @@ public List<File> getFilesInDirectory() {

public List<File> getCurrentSnapshotFilesInDirectory() {
FilenameFilter snapshotFilter =
(File dir, String name) -> name.contains(snapshotVersion) || name.equals("maven-metadata.xml");
(File dir, String name) -> name.contains(snapshotVersion) || name.contains("maven-metadata.xml");
File directory = path.getParent().toFile();
File[] snapshotFiles = directory.listFiles(snapshotFilter);
return Arrays.asList(snapshotFiles);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 Steinar Bang
* Copyright 2017-2022 Steinar Bang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@
package no.priv.bang.maven.repository.snapshotpruner;

import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static no.priv.bang.maven.repository.snapshotpruner.MavenProperties.*;

import java.io.File;
Expand All @@ -24,8 +25,11 @@
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.assertj.core.api.Condition;
import org.jdom2.JDOMException;
import org.junit.jupiter.api.Test;

Expand All @@ -39,14 +43,16 @@ void testDeleteSnapshotFiles() throws JDOMException, IOException {

Path mavenMetadataFileWithSnapshotVersion = Paths.get(repositoryDirectory.toString(), "no/priv/bang/ukelonn/ukelonn.api/1.0.0-SNAPSHOT/maven-metadata.xml");
MavenMetadata mavenMetadataWithSnapshotVersion = repository.parseMavenMetdata(mavenMetadataFileWithSnapshotVersion);
assertHasSha1AndMd5Checksums(findMavenMetadataFiles(mavenMetadataWithSnapshotVersion.getFilesInDirectory()));
assertTrue(mavenMetadataWithSnapshotVersion.hasSnapshotVersion());
assertEquals("1.0.0-20170922.181212-25", mavenMetadataWithSnapshotVersion.getSnapshotVersion());
assertEquals(mavenMetadataFileWithSnapshotVersion, mavenMetadataWithSnapshotVersion.getPath());
assertEquals(378, mavenMetadataWithSnapshotVersion.getFilesInDirectory().size());
assertEquals(16, mavenMetadataWithSnapshotVersion.getCurrentSnapshotFilesInDirectory().size());
assertEquals(18, mavenMetadataWithSnapshotVersion.getCurrentSnapshotFilesInDirectory().size());
int numberOfDeletedFiles = mavenMetadataWithSnapshotVersion.deleteFilesNotPartOfSnapshot();
assertEquals(362, numberOfDeletedFiles);
assertEquals(16, mavenMetadataWithSnapshotVersion.getFilesInDirectory().size());
assertEquals(360, numberOfDeletedFiles);
assertEquals(18, mavenMetadataWithSnapshotVersion.getFilesInDirectory().size());
assertHasSha1AndMd5Checksums(findMavenMetadataFiles(mavenMetadataWithSnapshotVersion.getFilesInDirectory()));
}


Expand Down Expand Up @@ -79,4 +85,19 @@ void deleteAFileInAdvanceOfTheTest(Set<File> filesToDelete) {
File fileToDeleteInAdvance = filesToDelete.iterator().next();
fileToDeleteInAdvance.delete();
}


private List<String> findMavenMetadataFiles(List<File> filesInDirectory) {
return filesInDirectory
.stream()
.map(f -> f.getAbsolutePath())
.filter(n -> n.contains("maven-metadata"))
.collect(Collectors.toList());
}

private void assertHasSha1AndMd5Checksums(List<String> mavenMetadataFiles) {
var sha1Checksum = new Condition<String>(filename -> filename.endsWith("sha1"), "sha1 checksum file");
var md5Checksum = new Condition<String>(filename -> filename.endsWith("md5"), "md5 checksum file");
assertThat(mavenMetadataFiles).as("sha1 and/or md5 file missing").haveAtLeastOne(sha1Checksum).haveAtLeastOne(md5Checksum);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void testRunSnapshotPrunerApplication() throws IOException, JDOMException {

// Verify that the old snapshot files have actually been deleted
int totalNumberOfFilesAfterDelete = repository.findMavenMetadataFilesWithSnapshotVersion().stream().mapToInt(p -> p.getFilesInDirectory().size()).sum();
assertEquals(20, totalNumberOfFilesAfterDelete);
assertEquals(24, totalNumberOfFilesAfterDelete);
}

private Path setCurrentDirectoryToMockRepositoryTop() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 Steinar Bang
* Copyright 2017-2022 Steinar Bang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -66,7 +66,7 @@ void testPruneSnapshotsInRepository() throws IOException, JDOMException {
MavenRepository repository = new MavenRepository(repositoryDirectory);

int totalNumberOfDeletedFiles = repository.pruneSnapshots();
assertEquals(400, totalNumberOfDeletedFiles);
assertEquals(396, totalNumberOfDeletedFiles);
}

}

0 comments on commit e47b835

Please sign in to comment.