Skip to content

Commit

Permalink
EnvParser Initial Implementation, JavaDoc and Unit Test Start
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkarth committed Dec 5, 2023
1 parent 4680a41 commit 5305d1b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ build/
dependency-reduced-pom.xml
pom.xml.versionsBackup
javadoc
/release.properties
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>pt.codeforge</groupId>
<artifactId>tooler-tools</artifactId>
<version>0.0.5</version>
<version>0.0.5-SNAPSHOT</version>
<packaging>jar</packaging>
<name>tooler-tools</name>
<description>Versatile library for backend and CLI developers, offers a growing collection of
Expand Down Expand Up @@ -156,6 +156,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>3.0.1</version><!-- Use the latest version available -->
<configuration>
<!-- Additional configuration options go here -->
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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.
*
* <p><b>Note:</b> 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<String> inputCopy = new AtomicReference<>(input);
List<String> 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<String> inputCopy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}

}

0 comments on commit 5305d1b

Please sign in to comment.