Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
</properties>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
49 changes: 45 additions & 4 deletions src/main/java/com/github/archarithms/App.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 26 additions & 0 deletions src/test/java/com/github/archarithms/test/DateStringTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
} */
}
9 changes: 9 additions & 0 deletions src/test/java/com/github/archarithms/test/TitleCaseTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ")));
}
}