Skip to content

Commit 0e4011a

Browse files
authored
Add UriUtils; other minor tweaks (#36)
1 parent a2273a5 commit 0e4011a

File tree

6 files changed

+86
-2
lines changed

6 files changed

+86
-2
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>com.nordstrom.tools</groupId>
44
<artifactId>java-utils</artifactId>
5+
<version>3.2.2-SNAPSHOT</version>
56
<packaging>jar</packaging>
67

78
<name>Java Utils</name>
@@ -212,5 +213,4 @@
212213
</properties>
213214
</profile>
214215
</profiles>
215-
<version>3.2.2-SNAPSHOT</version>
216216
</project>

src/main/java/com/nordstrom/common/jar/JarUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
*/
3838
public class JarUtils {
3939

40+
private JarUtils() {
41+
throw new AssertionError("JarUtils is a static utility class that cannot be instantiated.");
42+
}
43+
4044
/**
4145
* Assemble a classpath string from the specified array of dependencies.
4246
* <p>

src/main/java/com/nordstrom/common/jdbc/Param.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class Param {
1919
* Constructor: Private, to discourage direct instantiation.
2020
*/
2121
private Param() {
22+
throw new AssertionError("Params is a static utility class that cannot be instantiated.");
2223
}
2324

2425
/**
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.nordstrom.common.uri;
2+
3+
import java.net.URI;
4+
import java.net.URISyntaxException;
5+
import java.net.URL;
6+
7+
public class UriUtils {
8+
9+
private UriUtils() {
10+
throw new AssertionError("UriUtils is a static utility class that cannot be instantiated.");
11+
}
12+
13+
/**
14+
* Assemble a URI for the specified path under the provided context. <br>
15+
* <b>NOTE</b>: The URI returned by this method uses the scheme, host, and port of the provided context URL
16+
* and specified path string.
17+
*
18+
* @param context context URL
19+
* @param path path component
20+
* @return URI for the specified path within the provided context
21+
*/
22+
public static URI uriForPath(final URL context, final String path) {
23+
return makeBasicURI(context.getProtocol(), context.getHost(), context.getPort(), path);
24+
}
25+
26+
/**
27+
* Assemble a basic URI from the specified components.
28+
*
29+
* @param scheme scheme name
30+
* @param host host name
31+
* @param port port number
32+
* @param path path
33+
* @return assembled basic URI
34+
*/
35+
public static URI makeBasicURI(final String scheme, final String host, final int port, final String path) {
36+
try {
37+
return new URI(scheme, null, host, port, path, null, null);
38+
} catch (URISyntaxException e) {
39+
throw new IllegalArgumentException(e.getMessage(), e);
40+
}
41+
}
42+
}

src/test/java/com/nordstrom/common/file/PathUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public void testFindExecutableOnSystemPath() {
148148

149149
@Test
150150
public void testFindExecutableByFullPath() {
151-
String javaPath = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
151+
String javaPath = ProcessHandle.current().info().command().get();
152152
String path = PathUtils.findExecutableOnSystemPath(javaPath);
153153
assertNotNull(path);
154154
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.nordstrom.common.uri;
2+
3+
import static org.testng.Assert.assertEquals;
4+
5+
import java.net.MalformedURLException;
6+
import java.net.URI;
7+
import java.net.URL;
8+
9+
import org.testng.annotations.Test;
10+
11+
public class UriUtilsTest {
12+
13+
@Test
14+
public void testUriForPath() throws MalformedURLException {
15+
URL context = URI.create("http://user:[email protected]:80/context/path?id=123#frag").toURL();
16+
URI pathURI = UriUtils.uriForPath(context, "/target");
17+
assertEquals(pathURI.getScheme(), "http", "Scheme mismatch");
18+
assertEquals(pathURI.getUserInfo(), null, "User info mismatch");
19+
assertEquals(pathURI.getHost(), "host.com", "Host mismatch");
20+
assertEquals(pathURI.getPort(), 80, "Post mismatch");
21+
assertEquals(pathURI.getPath(), "/target", "Path mismatch");
22+
assertEquals(pathURI.getQuery(), null, "Query mismatch");
23+
assertEquals(pathURI.getFragment(), null, "Fragment mismatch");
24+
}
25+
26+
@Test
27+
public void testMakeBasicURI() {
28+
URI basicURI = UriUtils.makeBasicURI("http", "host.com", 80, "/target");
29+
assertEquals(basicURI.getScheme(), "http", "Scheme mismatch");
30+
assertEquals(basicURI.getUserInfo(), null, "User info mismatch");
31+
assertEquals(basicURI.getHost(), "host.com", "Host mismatch");
32+
assertEquals(basicURI.getPort(), 80, "Post mismatch");
33+
assertEquals(basicURI.getPath(), "/target", "Path mismatch");
34+
assertEquals(basicURI.getQuery(), null, "Query mismatch");
35+
assertEquals(basicURI.getFragment(), null, "Fragment mismatch");
36+
}
37+
}

0 commit comments

Comments
 (0)