diff --git a/pom.xml b/pom.xml index cec4ca1..28b8e51 100644 --- a/pom.xml +++ b/pom.xml @@ -2,6 +2,7 @@ 4.0.0 com.nordstrom.tools java-utils + 3.2.2-SNAPSHOT jar Java Utils @@ -212,5 +213,4 @@ - 3.2.2-SNAPSHOT diff --git a/src/main/java/com/nordstrom/common/jar/JarUtils.java b/src/main/java/com/nordstrom/common/jar/JarUtils.java index 40ae326..6212e4f 100644 --- a/src/main/java/com/nordstrom/common/jar/JarUtils.java +++ b/src/main/java/com/nordstrom/common/jar/JarUtils.java @@ -37,6 +37,10 @@ */ public class JarUtils { + private JarUtils() { + throw new AssertionError("JarUtils is a static utility class that cannot be instantiated."); + } + /** * Assemble a classpath string from the specified array of dependencies. *

diff --git a/src/main/java/com/nordstrom/common/jdbc/Param.java b/src/main/java/com/nordstrom/common/jdbc/Param.java index 4b98325..227a060 100644 --- a/src/main/java/com/nordstrom/common/jdbc/Param.java +++ b/src/main/java/com/nordstrom/common/jdbc/Param.java @@ -19,6 +19,7 @@ public class Param { * Constructor: Private, to discourage direct instantiation. */ private Param() { + throw new AssertionError("Params is a static utility class that cannot be instantiated."); } /** diff --git a/src/main/java/com/nordstrom/common/uri/UriUtils.java b/src/main/java/com/nordstrom/common/uri/UriUtils.java new file mode 100644 index 0000000..8a11423 --- /dev/null +++ b/src/main/java/com/nordstrom/common/uri/UriUtils.java @@ -0,0 +1,42 @@ +package com.nordstrom.common.uri; + +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; + +public class UriUtils { + + private UriUtils() { + throw new AssertionError("UriUtils is a static utility class that cannot be instantiated."); + } + + /** + * Assemble a URI for the specified path under the provided context.
+ * NOTE: The URI returned by this method uses the scheme, host, and port of the provided context URL + * and specified path string. + * + * @param context context URL + * @param path path component + * @return URI for the specified path within the provided context + */ + public static URI uriForPath(final URL context, final String path) { + return makeBasicURI(context.getProtocol(), context.getHost(), context.getPort(), path); + } + + /** + * Assemble a basic URI from the specified components. + * + * @param scheme scheme name + * @param host host name + * @param port port number + * @param path path + * @return assembled basic URI + */ + public static URI makeBasicURI(final String scheme, final String host, final int port, final String path) { + try { + return new URI(scheme, null, host, port, path, null, null); + } catch (URISyntaxException e) { + throw new IllegalArgumentException(e.getMessage(), e); + } + } +} diff --git a/src/test/java/com/nordstrom/common/file/PathUtilsTest.java b/src/test/java/com/nordstrom/common/file/PathUtilsTest.java index 7780fcc..b4ea689 100644 --- a/src/test/java/com/nordstrom/common/file/PathUtilsTest.java +++ b/src/test/java/com/nordstrom/common/file/PathUtilsTest.java @@ -148,7 +148,7 @@ public void testFindExecutableOnSystemPath() { @Test public void testFindExecutableByFullPath() { - String javaPath = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"; + String javaPath = ProcessHandle.current().info().command().get(); String path = PathUtils.findExecutableOnSystemPath(javaPath); assertNotNull(path); } diff --git a/src/test/java/com/nordstrom/common/uri/UriUtilsTest.java b/src/test/java/com/nordstrom/common/uri/UriUtilsTest.java new file mode 100644 index 0000000..01f6c8a --- /dev/null +++ b/src/test/java/com/nordstrom/common/uri/UriUtilsTest.java @@ -0,0 +1,37 @@ +package com.nordstrom.common.uri; + +import static org.testng.Assert.assertEquals; + +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; + +import org.testng.annotations.Test; + +public class UriUtilsTest { + + @Test + public void testUriForPath() throws MalformedURLException { + URL context = URI.create("http://user:pswd@host.com:80/context/path?id=123#frag").toURL(); + URI pathURI = UriUtils.uriForPath(context, "/target"); + assertEquals(pathURI.getScheme(), "http", "Scheme mismatch"); + assertEquals(pathURI.getUserInfo(), null, "User info mismatch"); + assertEquals(pathURI.getHost(), "host.com", "Host mismatch"); + assertEquals(pathURI.getPort(), 80, "Post mismatch"); + assertEquals(pathURI.getPath(), "/target", "Path mismatch"); + assertEquals(pathURI.getQuery(), null, "Query mismatch"); + assertEquals(pathURI.getFragment(), null, "Fragment mismatch"); + } + + @Test + public void testMakeBasicURI() { + URI basicURI = UriUtils.makeBasicURI("http", "host.com", 80, "/target"); + assertEquals(basicURI.getScheme(), "http", "Scheme mismatch"); + assertEquals(basicURI.getUserInfo(), null, "User info mismatch"); + assertEquals(basicURI.getHost(), "host.com", "Host mismatch"); + assertEquals(basicURI.getPort(), 80, "Post mismatch"); + assertEquals(basicURI.getPath(), "/target", "Path mismatch"); + assertEquals(basicURI.getQuery(), null, "Query mismatch"); + assertEquals(basicURI.getFragment(), null, "Fragment mismatch"); + } +}