Skip to content

Commit

Permalink
Make sorting of available icons more stable
Browse files Browse the repository at this point in the history
  • Loading branch information
strangelookingnerd committed Mar 29, 2023
1 parent a4bb391 commit a7cf520
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 deletions.
23 changes: 12 additions & 11 deletions src/main/java/jenkins/plugins/foldericon/CustomFolderIcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Comparator;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -79,24 +80,24 @@ public CustomFolderIcon(String foldericon) {
*
* @return all the icons that have been uploaded, sorted descending by {@link FilePath#lastModified()}.
*/
public static List<String> getAvailableIcons() {
public static Set<String> getAvailableIcons() {
try {
FilePath iconDir = Jenkins.get().getRootPath().child(USER_CONTENT_PATH).child(PLUGIN_PATH);

if (iconDir.exists()) {
return iconDir.list().stream().sorted((file1, file2) -> {
return iconDir.list().stream().sorted(Comparator.comparingLong((FilePath file) -> {
try {
return Long.compare(file2.lastModified(), file1.lastModified());
} catch (Exception ex) {
return file.lastModified();
} catch (IOException | InterruptedException ex) {
return 0;
}
}).map(FilePath::getName).collect(Collectors.toList());
}).reversed()).map(FilePath::getName).collect(Collectors.toSet());
} else {
return List.of();
return Set.of();
}
} catch (IOException | InterruptedException ex) {
LOGGER.log(Level.WARNING, ex, () -> "Unable to list available icons!");
return List.of();
return Set.of();
}
}

Expand Down Expand Up @@ -207,12 +208,12 @@ public HttpResponse doCleanup(StaplerRequest req) {

FilePath iconDir = Jenkins.get().getRootPath().child(USER_CONTENT_PATH).child(PLUGIN_PATH);

List<String> existingIcons = getAvailableIcons();
Set<String> existingIcons = getAvailableIcons();

List<String> usedIcons = Jenkins.get().getAllItems(AbstractFolder.class).stream()
Set<String> usedIcons = Jenkins.get().getAllItems(AbstractFolder.class).stream()
.filter(folder -> folder.getIcon() instanceof CustomFolderIcon)
.map(folder -> ((CustomFolderIcon) folder.getIcon()).getFoldericon())
.collect(Collectors.toList());
.collect(Collectors.toSet());

if (usedIcons.isEmpty() || existingIcons.removeAll(usedIcons)) {
for (String icon : existingIcons) {
Expand Down
65 changes: 40 additions & 25 deletions src/test/java/jenkins/plugins/foldericon/CustomFolderIconTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
import java.io.RandomAccessFile;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -612,7 +614,7 @@ void testDoCleanupFileNotDeletedWithMockedException(JenkinsRule r) throws Except
*/
@Test
void testGetAvailableIcons(JenkinsRule r) throws Exception {
List<String> icons = CustomFolderIcon.getAvailableIcons();
Set<String> icons = CustomFolderIcon.getAvailableIcons();

assertNotNull(icons);
assertTrue(icons.isEmpty());
Expand All @@ -621,30 +623,33 @@ void testGetAvailableIcons(JenkinsRule r) throws Exception {
FilePath iconDir = userContent.child("customFolderIcons");
iconDir.mkdirs();

String filename1 = System.nanoTime() + ".png";
long timestamp1 = System.nanoTime();
String filename1 = timestamp1 + ".png";
FilePath file1 = iconDir.child(filename1);
file1.touch(System.nanoTime());
file1.touch(timestamp1);

String filename2 = System.nanoTime() + ".png";
long timestamp2 = System.nanoTime();
String filename2 = timestamp2 + ".png";
FilePath file2 = iconDir.child(filename2);
file2.touch(System.nanoTime());
file2.touch(timestamp2);

String filename3 = System.nanoTime() + ".png";
long timestamp3 = System.nanoTime();
String filename3 = timestamp3 + ".png";
FilePath file3 = iconDir.child(filename3);
file3.touch(System.nanoTime());
file3.touch(timestamp3);

icons = CustomFolderIcon.getAvailableIcons();

assertNotNull(icons);
assertEquals(3, icons.size());

List<String> expected = Arrays.asList(file1, file2, file3).stream().sorted((filePath1, filePath2) -> {
Set<String> expected = Arrays.asList(file1, file2, file3).stream().sorted(Comparator.comparingLong((FilePath file) -> {
try {
return Long.compare(filePath2.lastModified(), filePath1.lastModified());
} catch (Exception ex) {
return file.lastModified();
} catch (IOException | InterruptedException ex) {
return 0;
}
}).map(FilePath::getName).collect(Collectors.toList());
}).reversed()).map(FilePath::getName).collect(Collectors.toSet());

assertEquals(expected, icons);
}
Expand All @@ -656,7 +661,7 @@ void testGetAvailableIcons(JenkinsRule r) throws Exception {
*/
@Test
void testGetAvailableIconsThrowingExceptions(JenkinsRule r) throws Exception {
List<String> icons = CustomFolderIcon.getAvailableIcons();
Set<String> icons = CustomFolderIcon.getAvailableIcons();

assertNotNull(icons);
assertTrue(icons.isEmpty());
Expand All @@ -665,13 +670,20 @@ void testGetAvailableIconsThrowingExceptions(JenkinsRule r) throws Exception {
FilePath iconDir = userContent.child("customFolderIcons");
iconDir.mkdirs();

String filename1 = System.nanoTime() + ".png";
long timestamp1 = System.nanoTime();
String filename1 = timestamp1 + ".png";
FilePath file1 = iconDir.child(filename1);
file1.touch(System.nanoTime());
file1.touch(timestamp1);

String filename2 = System.nanoTime() + ".png";
long timestamp2 = System.nanoTime();
String filename2 = timestamp2 + ".png";
FilePath file2 = iconDir.child(filename2);
file2.touch(System.nanoTime());
file2.touch(timestamp2);

long timestamp3 = System.nanoTime();
String filename3 = timestamp3 + ".png";
FilePath file3 = iconDir.child(filename3);
file3.touch(timestamp3);

try (MockedConstruction<FilePath> mocked = Mockito.mockConstructionWithAnswer(FilePath.class, invocation -> {
String call = invocation.toString();
Expand All @@ -694,7 +706,7 @@ void testGetAvailableIconsThrowingExceptions(JenkinsRule r) throws Exception {
*/
@Test
void testGetAvailableIconsComparatorThrowingExceptions(JenkinsRule r) throws Exception {
List<String> icons = CustomFolderIcon.getAvailableIcons();
Set<String> icons = CustomFolderIcon.getAvailableIcons();

assertNotNull(icons);
assertTrue(icons.isEmpty());
Expand All @@ -703,17 +715,20 @@ void testGetAvailableIconsComparatorThrowingExceptions(JenkinsRule r) throws Exc
FilePath iconDir = userContent.child("customFolderIcons");
iconDir.mkdirs();

String filename1 = System.nanoTime() + ".png";
long timestamp1 = System.nanoTime();
String filename1 = timestamp1 + ".png";
FilePath file1 = iconDir.child(filename1);
file1.touch(System.nanoTime());
file1.touch(timestamp1);

String filename2 = System.nanoTime() + ".png";
long timestamp2 = System.nanoTime();
String filename2 = timestamp2 + ".png";
FilePath file2 = iconDir.child(filename2);
file2.touch(System.nanoTime());
file2.touch(timestamp2);

String filename3 = System.nanoTime() + ".png";
long timestamp3 = System.nanoTime();
String filename3 = timestamp3 + ".png";
FilePath file3 = iconDir.child(filename3);
file3.touch(System.nanoTime());
file3.touch(timestamp3);;

final int[] counter = {0};

Expand Down Expand Up @@ -760,13 +775,13 @@ void testGetAvailableIconsComparatorThrowingExceptions(JenkinsRule r) throws Exc
assertEquals(3, icons.size());
}

List<String> expected = Arrays.asList(file1, file2, file3).stream().sorted((filePath1, filePath2) -> {
Set<String> expected = Arrays.asList(file1, file2, file3).stream().sorted((filePath1, filePath2) -> {
try {
return Long.compare(filePath2.lastModified(), filePath1.lastModified());
} catch (Exception ex) {
return 0;
}
}).map(FilePath::getName).collect(Collectors.toList());
}).map(FilePath::getName).collect(Collectors.toSet());

assertEquals(expected, icons);
}
Expand Down

0 comments on commit a7cf520

Please sign in to comment.