diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81a66a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/target/ +/.settings/ +/.classpath +/.project +/build/ +/.idea/ +*.iml + diff --git a/README.md b/README.md new file mode 100644 index 0000000..ed551d1 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +## URL Utils ## + +A collection of useful URL utilities. + +Encode + +```java +String encoded = UrlUtils.encode(url); +``` + +Decode + +```java +String decoded = UrlUtils.decode(url); +``` + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..3920bf1 --- /dev/null +++ b/pom.xml @@ -0,0 +1,62 @@ + + 4.0.0 + + io.ljunggren + url-utils + 0.0.1-SNAPSHOT + jar + + url-utils + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + + + + + junit + junit + 4.13.2 + test + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + nexus + http://nexus.ljunggren.io/repository/maven-releases/ + + + nexus + http://nexus.ljunggren.io/repository/maven-snapshots/ + + + + + https://gitbucket.ljunggren.io/git/aljunggren/url-utils.git + scm:git:https://gitbucket.ljunggren.io/git/aljunggren/url-utils.git + scm:git:https://gitbucket.ljunggren.io/git/aljunggren/url-utils.git + HEAD + + + diff --git a/src/main/java/io/ljunggren/url/utils/UrlUtils.java b/src/main/java/io/ljunggren/url/utils/UrlUtils.java new file mode 100644 index 0000000..75d0364 --- /dev/null +++ b/src/main/java/io/ljunggren/url/utils/UrlUtils.java @@ -0,0 +1,24 @@ +package io.ljunggren.url.utils; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + +public class UrlUtils { + + public static String encode(String string) throws UnsupportedEncodingException { + if (string == null) { + return null; + } + return URLEncoder.encode(string, StandardCharsets.UTF_8.toString()); + } + + public static String decode(String string) throws UnsupportedEncodingException { + if (string == null) { + return null; + } + return URLDecoder.decode(string, StandardCharsets.UTF_8.toString()); + } + +} diff --git a/src/test/java/io/ljunggren/url/utils/UrlUtilsTest.java b/src/test/java/io/ljunggren/url/utils/UrlUtilsTest.java new file mode 100644 index 0000000..3ce803b --- /dev/null +++ b/src/test/java/io/ljunggren/url/utils/UrlUtilsTest.java @@ -0,0 +1,37 @@ +package io.ljunggren.url.utils; + +import static org.junit.Assert.*; + +import java.io.UnsupportedEncodingException; + +import org.junit.Test; + +public class UrlUtilsTest { + + @Test + public void encodeTest() throws UnsupportedEncodingException { + String expected = "https%3A%2F%2Fljunggren.io"; + String actual = UrlUtils.encode("https://ljunggren.io"); + assertEquals(expected, actual); + } + + @Test + public void encodeNullTest() throws UnsupportedEncodingException { + String actual = UrlUtils.encode(null); + assertNull(actual); + } + + @Test + public void decodeTest() throws UnsupportedEncodingException { + String expected = "https://ljunggren.io"; + String actual = UrlUtils.decode("https%3A%2F%2Fljunggren.io"); + assertEquals(expected, actual); + } + + @Test + public void decodeNullTest() throws UnsupportedEncodingException { + String actual = UrlUtils.decode(null); + assertNull(actual); + } + +}