From ed6a92d2fd4c149c7d429f38c5ac2f0a9a7d87da Mon Sep 17 00:00:00 2001 From: strangelookingnerd <49242855+strangelookingnerd@users.noreply.github.com> Date: Wed, 21 Sep 2022 13:20:36 +0200 Subject: [PATCH 1/3] Provide Java implementation to list available icons (#14) --- .../jenkins/plugins/ionicons/Ionicons.java | 77 +++++++++++++++++++ .../plugins/ionicons/IoniconsTest.java | 53 +++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 src/main/java/jenkins/plugins/ionicons/Ionicons.java create mode 100644 src/test/java/jenkins/plugins/ionicons/IoniconsTest.java diff --git a/src/main/java/jenkins/plugins/ionicons/Ionicons.java b/src/main/java/jenkins/plugins/ionicons/Ionicons.java new file mode 100644 index 0000000..22dcf58 --- /dev/null +++ b/src/main/java/jenkins/plugins/ionicons/Ionicons.java @@ -0,0 +1,77 @@ +package jenkins.plugins.ionicons; + +import java.io.File; +import java.net.URL; +import java.util.Collection; +import java.util.Enumeration; +import java.util.Map; +import java.util.concurrent.ConcurrentSkipListMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; + +/** + * Utility to work with icons provided by the ionicons-api-plugin. + * + * @author strangelookingnerd + * + */ +public final class Ionicons { + + private static final Logger LOGGER = Logger.getLogger(Ionicons.class.getName()); + + private static final String[] SVG_FILE_FORMAT = { "svg" }; + private static final String SVG_FILE_ENDING = "." + SVG_FILE_FORMAT[0]; + private static final String IMAGES_SYMBOLS_PATH = "images/symbols/"; + private static final String IONICONS_API_PLUGIN = "ionicons-api-plugin"; + private static final String ICON_CLASS_NAME_PATTERN = "symbol-%s plugin-ionicons-api"; + + private static final Ionicons INSTANCE = new Ionicons(); + + private final Map availableIcons = new ConcurrentSkipListMap<>(); + + private Ionicons() { + try { + Enumeration urls = getClass().getClassLoader().getResources(IMAGES_SYMBOLS_PATH); + + while (urls.hasMoreElements()) { + URL url = urls.nextElement(); + + if (StringUtils.contains(url.toExternalForm(), IONICONS_API_PLUGIN)) { + Collection icons = FileUtils.listFiles(new File(url.toURI()), SVG_FILE_FORMAT, false); + + for (File icon : icons) { + String iconName = StringUtils.removeEnd(icon.getName(), SVG_FILE_ENDING); + availableIcons.put(iconName, getIconClassName(iconName)); + } + break; + } + } + } catch (Exception ex) { + LOGGER.log(Level.WARNING, "Unable to read available icons: Resource unavailable.", ex); + } + } + + /** + * Takes an icon name and generates a icon class name from it. + * + * @param icon the icon name + * @return the icon class name + */ + public static String getIconClassName(String icon) { + return String.format(ICON_CLASS_NAME_PATTERN, icon); + } + + /** + * Get all available icons provided by the ionicons-api-plugin. + * + * @return a sorted map of available icons with icon name as key and the icon + * class name as value. + */ + public static Map getAvailableIcons() { + return INSTANCE.availableIcons; + } + +} diff --git a/src/test/java/jenkins/plugins/ionicons/IoniconsTest.java b/src/test/java/jenkins/plugins/ionicons/IoniconsTest.java new file mode 100644 index 0000000..83313ab --- /dev/null +++ b/src/test/java/jenkins/plugins/ionicons/IoniconsTest.java @@ -0,0 +1,53 @@ +package jenkins.plugins.ionicons; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertIterableEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.File; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; +import org.junit.jupiter.api.Test; + +/** + * Test Class for {@link Ionicons}. + * + * @author strangelookingnerd + * + */ +class IoniconsTest { + + @Test + void testGetIconClassName() { + assertEquals("symbol-test plugin-ionicons-api", Ionicons.getIconClassName("test")); + } + + @Test + void testGetAvailableIcons() { + Map availableIcons = Ionicons.getAvailableIcons(); + + assertNotNull(availableIcons); + assertFalse(availableIcons.isEmpty()); + + Collection icons = FileUtils.listFiles(new File("./src/main/resources/images/symbols/"), + new String[] { "svg" }, false); + assertEquals(icons.size(), availableIcons.size()); + + List iconNames = icons.stream().map(icon -> StringUtils.removeEnd(icon.getName(), ".svg")) + .sorted(Comparator.naturalOrder()).collect(Collectors.toList()); + assertIterableEquals(iconNames, availableIcons.keySet()); + + for (Entry icon : availableIcons.entrySet()) { + assertEquals(icon.getValue(), Ionicons.getIconClassName(icon.getKey())); + } + } + +} From 7091b8f4928b3327c84e590ba90f9932b303df18 Mon Sep 17 00:00:00 2001 From: strangelookingnerd <49242855+strangelookingnerd@users.noreply.github.com> Date: Fri, 23 Sep 2022 09:12:11 +0200 Subject: [PATCH 2/3] Move package to io.jenkins.plugins --- src/main/java/{ => io}/jenkins/plugins/ionicons/Ionicons.java | 2 +- .../java/{ => io}/jenkins/plugins/ionicons/IoniconsTest.java | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) rename src/main/java/{ => io}/jenkins/plugins/ionicons/Ionicons.java (98%) rename src/test/java/{ => io}/jenkins/plugins/ionicons/IoniconsTest.java (94%) diff --git a/src/main/java/jenkins/plugins/ionicons/Ionicons.java b/src/main/java/io/jenkins/plugins/ionicons/Ionicons.java similarity index 98% rename from src/main/java/jenkins/plugins/ionicons/Ionicons.java rename to src/main/java/io/jenkins/plugins/ionicons/Ionicons.java index 22dcf58..b36942f 100644 --- a/src/main/java/jenkins/plugins/ionicons/Ionicons.java +++ b/src/main/java/io/jenkins/plugins/ionicons/Ionicons.java @@ -1,4 +1,4 @@ -package jenkins.plugins.ionicons; +package io.jenkins.plugins.ionicons; import java.io.File; import java.net.URL; diff --git a/src/test/java/jenkins/plugins/ionicons/IoniconsTest.java b/src/test/java/io/jenkins/plugins/ionicons/IoniconsTest.java similarity index 94% rename from src/test/java/jenkins/plugins/ionicons/IoniconsTest.java rename to src/test/java/io/jenkins/plugins/ionicons/IoniconsTest.java index 83313ab..e6a714c 100644 --- a/src/test/java/jenkins/plugins/ionicons/IoniconsTest.java +++ b/src/test/java/io/jenkins/plugins/ionicons/IoniconsTest.java @@ -1,4 +1,4 @@ -package jenkins.plugins.ionicons; +package io.jenkins.plugins.ionicons; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -17,6 +17,8 @@ import org.apache.commons.lang.StringUtils; import org.junit.jupiter.api.Test; +import io.jenkins.plugins.ionicons.Ionicons; + /** * Test Class for {@link Ionicons}. * From ccb448cc14bdea8b95a581a94bc5f8f12938a8cb Mon Sep 17 00:00:00 2001 From: strangelookingnerd <49242855+strangelookingnerd@users.noreply.github.com> Date: Fri, 23 Sep 2022 09:16:48 +0200 Subject: [PATCH 3/3] Remove unused import --- src/test/java/io/jenkins/plugins/ionicons/IoniconsTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/io/jenkins/plugins/ionicons/IoniconsTest.java b/src/test/java/io/jenkins/plugins/ionicons/IoniconsTest.java index e6a714c..0bd3424 100644 --- a/src/test/java/io/jenkins/plugins/ionicons/IoniconsTest.java +++ b/src/test/java/io/jenkins/plugins/ionicons/IoniconsTest.java @@ -17,8 +17,6 @@ import org.apache.commons.lang.StringUtils; import org.junit.jupiter.api.Test; -import io.jenkins.plugins.ionicons.Ionicons; - /** * Test Class for {@link Ionicons}. *