From 832613591a9b5876b2f9df7571ab426c103112e2 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Tue, 19 Jul 2022 20:33:54 -0500 Subject: [PATCH 1/2] All tests passing. No failures, errors, or skipped tests. This commit satisfies the existing unit tests "as given". --- pom.xml | 10 +++++ src/main/java/com/github/archarithms/App.java | 42 +++++++++++++++++-- 2 files changed, 48 insertions(+), 4 deletions(-) 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..fc8f5a3 100644 --- a/src/main/java/com/github/archarithms/App.java +++ b/src/main/java/com/github/archarithms/App.java @@ -1,15 +1,49 @@ 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 = StringUtils.lowerCase(result); + result = WordUtils.capitalizeFully(result); + + return result; } - public static String convertUnixToDateString(final Long inpUnixSeconds) + public static String convertUnixToDateString(final Long inpUnixSeconds) throws NullPointerException { - return String.valueOf(inpUnixSeconds); + + LocalDate dt = LocalDate.now(); + DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMMM d, yyyy").withZone(ZoneId.systemDefault()); + String result = null; + + // Return the current date if the input is null. + if (inpUnixSeconds == null){ + result = dt.format(fmt).toString(); + throw new NullPointerException(); + } + + // Otherwise convert the unix epoch seconds to a datetime. + Instant inst = Instant.ofEpochSecond(inpUnixSeconds); + result = fmt.format(inst); + return result; } } From a2e2630f0fa4a9b1180444cafb64fab47aa66b7e Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Tue, 19 Jul 2022 21:39:13 -0500 Subject: [PATCH 2/2] All tests passing. Simplified return statements. Added test cases. Note for Challenge 2: There was request for a function that detects an incorrect input type. Java is a strongly typed language so this kind of error will be caught at compile time. Therefore I did not see a need to implement it within the function. --- src/main/java/com/github/archarithms/App.java | 23 ++++++++++------ .../archarithms/test/DateStringTests.java | 26 +++++++++++++++++++ .../archarithms/test/TitleCaseTests.java | 9 +++++++ 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/github/archarithms/App.java b/src/main/java/com/github/archarithms/App.java index fc8f5a3..9674cbf 100644 --- a/src/main/java/com/github/archarithms/App.java +++ b/src/main/java/com/github/archarithms/App.java @@ -22,28 +22,35 @@ public static String convertToTitleCase(final String inpStr) throws NullPointerE result = StringUtils.stripEnd(result, " "); // Set Correct Case - //result = StringUtils.lowerCase(result); 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) throws NullPointerException { - - LocalDate dt = LocalDate.now(); + // Specify the desired date format. DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MMMM d, yyyy").withZone(ZoneId.systemDefault()); - String result = null; - // Return the current date if the input is null. + // Throw exception if the input is null. if (inpUnixSeconds == null){ - result = dt.format(fmt).toString(); throw new NullPointerException(); } // Otherwise convert the unix epoch seconds to a datetime. Instant inst = Instant.ofEpochSecond(inpUnixSeconds); - result = fmt.format(inst); - return result; + 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 "))); + } }