From 5305d1b63aebc40830f567ee82100be8848cc8ac Mon Sep 17 00:00:00 2001 From: rikkarth Date: Tue, 5 Dec 2023 22:48:30 +0000 Subject: [PATCH] EnvParser Initial Implementation, JavaDoc and Unit Test Start --- .gitignore | 1 + pom.xml | 10 +++++- ...ystemEnvParser.java => EnvPathParser.java} | 35 ++++++++++++++++--- .../toolertools/props/PropertiesLoader.java | 4 +-- .../pathfinder/EnvPathParserTest.java | 27 ++++++++++++++ 5 files changed, 70 insertions(+), 7 deletions(-) rename src/main/java/pt/codeforge/toolertools/pathfinder/{SystemEnvParser.java => EnvPathParser.java} (52%) create mode 100644 src/test/java/pt/codeforge/toolertools/pathfinder/EnvPathParserTest.java diff --git a/.gitignore b/.gitignore index 8a3b414..a1a8e95 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,4 @@ build/ dependency-reduced-pom.xml pom.xml.versionsBackup javadoc +/release.properties diff --git a/pom.xml b/pom.xml index 71832a1..346cbdd 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ pt.codeforge tooler-tools - 0.0.5 + 0.0.5-SNAPSHOT jar tooler-tools Versatile library for backend and CLI developers, offers a growing collection of @@ -156,6 +156,14 @@ + + org.apache.maven.plugins + maven-release-plugin + 3.0.1 + + + + diff --git a/src/main/java/pt/codeforge/toolertools/pathfinder/SystemEnvParser.java b/src/main/java/pt/codeforge/toolertools/pathfinder/EnvPathParser.java similarity index 52% rename from src/main/java/pt/codeforge/toolertools/pathfinder/SystemEnvParser.java rename to src/main/java/pt/codeforge/toolertools/pathfinder/EnvPathParser.java index d8ea560..419320d 100644 --- a/src/main/java/pt/codeforge/toolertools/pathfinder/SystemEnvParser.java +++ b/src/main/java/pt/codeforge/toolertools/pathfinder/EnvPathParser.java @@ -1,21 +1,48 @@ package pt.codeforge.toolertools.pathfinder; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicReference; -public class SystemEnvParser { +/** + * Utility class for parsing and expanding environment variables in file paths. This class provides static methods for + * parsing input paths, replacing environment variable placeholders with their respective values on both Windows and + * Unix-like systems. + * + *

Note: This class should not be instantiated as it consists of static utility methods only. + * + * @see EnvPathParser#getEnvPath(String) + * @see EnvPathParser#parseIfMys(AtomicReference, List) + * @see EnvPathParser#parseIfUnix(AtomicReference, List) + */ +public class EnvPathParser { - private SystemEnvParser() { - throw new AssertionError("SystemEnvParser should not be instantiated."); + private EnvPathParser() { + throw new AssertionError("EnvPathParser should not be instantiated."); } + /** + * Parses the input path by replacing environment variable placeholders with their respective values. Environment + * variable placeholders are denoted by percent signs ('%') on both ends for Windows and by a dollar sign ('$') at + * the beginning for Unix-like systems. The method handles both styles. After parsing, the method returns the + * normalized path as a String. + * + * @param input The input path to be parsed. Must not be null. + * @return The parsed and normalized path as a String. + * @throws IllegalArgumentException If the input parameter is null. + */ public static String getEnvPath(String input) { + if (input == null) { + throw new IllegalArgumentException("Input cannot be null."); + } + AtomicReference inputCopy = new AtomicReference<>(input); List pathComponentsList = Arrays.asList((inputCopy.get()).split("[/\\\\:-]")); parseIfMys(inputCopy, pathComponentsList); parseIfUnix(inputCopy, pathComponentsList); - return inputCopy.get(); + + return Paths.get(inputCopy.get()).toString(); } private static void parseIfMys(AtomicReference inputCopy, diff --git a/src/main/java/pt/codeforge/toolertools/props/PropertiesLoader.java b/src/main/java/pt/codeforge/toolertools/props/PropertiesLoader.java index c9ac490..97f59e2 100644 --- a/src/main/java/pt/codeforge/toolertools/props/PropertiesLoader.java +++ b/src/main/java/pt/codeforge/toolertools/props/PropertiesLoader.java @@ -8,7 +8,7 @@ import org.apache.commons.configuration2.ex.ConfigurationException; import pt.codeforge.toolertools.internal.exceptions.PropertiesGenerationException; import pt.codeforge.toolertools.internal.exceptions.PropertiesLoadingException; -import pt.codeforge.toolertools.pathfinder.SystemEnvParser; +import pt.codeforge.toolertools.pathfinder.EnvPathParser; public class PropertiesLoader { @@ -21,7 +21,7 @@ public Properties loadProperties(String propName) throws PropertiesGenerationExc String filePath = this.getOptionalFilePath(propName).orElseThrow( () -> new PropertiesLoadingException(getPropertiesLoadingErrorMsg(propName))); - String propsPath = SystemEnvParser.getEnvPath(filePath); + String propsPath = EnvPathParser.getEnvPath(filePath); return this.getOptionalProperties(propsPath).orElseThrow( () -> new PropertiesGenerationException(getInvalidPathToPropErrorMsg(propName, propsPath))); diff --git a/src/test/java/pt/codeforge/toolertools/pathfinder/EnvPathParserTest.java b/src/test/java/pt/codeforge/toolertools/pathfinder/EnvPathParserTest.java new file mode 100644 index 0000000..6dee7cf --- /dev/null +++ b/src/test/java/pt/codeforge/toolertools/pathfinder/EnvPathParserTest.java @@ -0,0 +1,27 @@ +package pt.codeforge.toolertools.pathfinder; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +class EnvPathParserTest { + + @BeforeEach + void setup(){ + System.setProperty("$HOME", "test"); + } + + @ParameterizedTest + @ValueSource(strings = "$HOMEDRIVE/$HOMEPATH") + void testGetEnvPath(String input){ + + System.out.println(EnvPathParser.getEnvPath(input)); + } + + @Test + void testBadGetEnvPath(){ + System.out.println(EnvPathParser.getEnvPath(null)); + } + +}