diff --git a/pom.xml b/pom.xml index 8f6b076..5559e69 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,16 @@ + + org.apache.commons + commons-text + 1.9 + + + org.apache.commons + commons-lang3 + 3.12.0 + org.junit.jupiter junit-jupiter-api diff --git a/src/main/java/com/github/archarithms/App.java b/src/main/java/com/github/archarithms/App.java index dcb6723..9674cbf 100644 --- a/src/main/java/com/github/archarithms/App.java +++ b/src/main/java/com/github/archarithms/App.java @@ -1,15 +1,56 @@ package com.github.archarithms; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.text.WordUtils; +import java.time.*; +import java.time.format.DateTimeFormatter; public class App { - public static String convertToTitleCase(final String inpStr) + public static String convertToTitleCase(final String inpStr) throws NullPointerException { - return inpStr; + // Check for null input + if (inpStr == null){ + throw new NullPointerException(); + } + + // Remove non-alphanumeric characters and replace them with spaces + String result = inpStr; + result = result.replaceAll("[^A-Za-z0-9]", " "); + + // Remove trailing spaces + result = StringUtils.stripEnd(result, " "); + + // Set Correct Case + result = WordUtils.capitalizeFully(result); + + return result; + } + + public static String convertUnixToDateString() + { + // Get the current date. + LocalDate dt = LocalDate.now(); + + // Specify the desired date format. + DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMMM d, yyyy").withZone(ZoneId.systemDefault()); + + // Return the current date. + return dt.format(fmt).toString(); } - public static String convertUnixToDateString(final Long inpUnixSeconds) + public static String convertUnixToDateString(final Long inpUnixSeconds) throws NullPointerException { - return String.valueOf(inpUnixSeconds); + // Specify the desired date format. + DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMMM d, yyyy").withZone(ZoneId.systemDefault()); + + // Throw exception if the input is null. + if (inpUnixSeconds == null){ + throw new NullPointerException(); + } + + // Otherwise convert the unix epoch seconds to a datetime. + Instant inst = Instant.ofEpochSecond(inpUnixSeconds); + return fmt.format(inst); } } diff --git a/src/test/java/com/github/archarithms/test/DateStringTests.java b/src/test/java/com/github/archarithms/test/DateStringTests.java index 04e9943..0c5e8e0 100644 --- a/src/test/java/com/github/archarithms/test/DateStringTests.java +++ b/src/test/java/com/github/archarithms/test/DateStringTests.java @@ -3,6 +3,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.time.LocalDate; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; + import org.junit.jupiter.api.Test; import com.github.archarithms.App; @@ -33,4 +37,26 @@ public void testConvertUnixToDateString() { public void testNullCase() { assertThrows(Exception.class, () -> App.convertUnixToDateString(null)); } + + + /** + * Test the testNoInput method + */ + @Test + public void testNoInput() { + LocalDate dt = LocalDate.now(); + DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMMM d, yyyy").withZone(ZoneId.systemDefault()); + String testStr = dt.format(fmt).toString(); + assertTrue(testStr.equals(App.convertUnixToDateString())); + } + + /** + * Test the testWrongType method + * Note: Java is a strongly typed language. As such the compiler will catch mismatches at compile time. + * This can be tested by uncommenting the test case below: + */ +/* @Test + public void testWrongType() { + assertThrows(Exception.class, () -> App.convertUnixToDateString("149914400L")); + } */ } diff --git a/src/test/java/com/github/archarithms/test/TitleCaseTests.java b/src/test/java/com/github/archarithms/test/TitleCaseTests.java index bd3bb2d..b96b6f3 100644 --- a/src/test/java/com/github/archarithms/test/TitleCaseTests.java +++ b/src/test/java/com/github/archarithms/test/TitleCaseTests.java @@ -42,4 +42,13 @@ public void testOtherChars() { String testStr = "Truth Track"; assertTrue(testStr.equals(App.convertToTitleCase("TRUTH-TRACK"))); } + + /** + * Test the testEndSpaces method + */ + @Test + public void testEndSpaces() { + String testStr = "No End Spaces"; + assertTrue(testStr.equals(App.convertToTitleCase("NO END SPACES "))); + } }