Skip to content

Commit

Permalink
Fix checksum sort (#1082)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck authored Jun 17, 2024
1 parent 76fd541 commit 40fadc7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
26 changes: 22 additions & 4 deletions core/src/main/java/org/jboss/pnc/build/finder/core/Checksum.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,25 @@ public int compareTo(Checksum o) {
return 1;
}

int i = Integer.compare(type.ordinal(), o.getType().ordinal());
return i != 0 ? i : StringUtils.compare(value, o.value);
int i = Integer.compare(type.ordinal(), o.type.ordinal());

if (i != 0) {
return i;
}

int j = StringUtils.compare(value, o.value);

if (j != 0) {
return j;
}

int k = StringUtils.compare(filename, o.filename);

if (k != 0) {
return k;
}

return Long.compare(fileSize, o.fileSize);
}

@Override
Expand All @@ -321,12 +338,13 @@ public boolean equals(Object o) {
}

Checksum checksum = (Checksum) o;
return type == checksum.type && Objects.equals(value, checksum.value);
return fileSize == checksum.fileSize && type == checksum.type && Objects.equals(value, checksum.value)
&& Objects.equals(filename, checksum.filename);
}

@Override
public int hashCode() {
return Objects.hash(type, value);
return Objects.hash(type, value, filename, fileSize);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
*/
package org.jboss.pnc.build.finder.core;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.jboss.pnc.build.finder.core.ChecksumType.md5;
import static org.jboss.pnc.build.finder.core.ChecksumType.sha1;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.vfs2.FileContent;
import org.apache.commons.vfs2.FileObject;
Expand All @@ -36,4 +42,15 @@ void testMissingFile(@TempDir File folder) {
}
}).isExactlyInstanceOf(FileSystemException.class).hasMessageMatching(".*Does file.*exist.*");
}

@Test
void testSort() {
Checksum c1 = new Checksum(md5, "7215ee9c7d9dc229d2921a40e899ec5f", "c.txt", 2L);
Checksum c2 = new Checksum(sha1, "b858cb282617fb0956d960215c8e84d1ccf909c6", "b.txt", 0L);
Checksum c3 = new Checksum(sha1, "b858cb282617fb0956d960215c8e84d1ccf909c6", "a.txt", 0L);
Checksum c4 = new Checksum(sha1, "b858cb282617fb0956d960215c8e84d1ccf909c6", "a.txt", 1L);
List<Checksum> l = Arrays.asList(c1, c2, c3, c4);
Collections.sort(l);
assertThat(l).containsExactly(c1, c3, c4, c2);
}
}

0 comments on commit 40fadc7

Please sign in to comment.