From a46278005998a33b62a3ed0a3af66ba3f6596fb3 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Wed, 25 Nov 2015 23:22:43 +0100 Subject: [PATCH] Create tests for StringUtils#getStackTrace and StringUtils#getDifference - Create tests - Make StringUtils final with private constructor (utility class) --- .../fr/xephi/authme/util/StringUtils.java | 6 +++- .../fr/xephi/authme/util/StringUtilsTest.java | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/fr/xephi/authme/util/StringUtils.java b/src/main/java/fr/xephi/authme/util/StringUtils.java index 395e4db1b..d712e3adf 100644 --- a/src/main/java/fr/xephi/authme/util/StringUtils.java +++ b/src/main/java/fr/xephi/authme/util/StringUtils.java @@ -10,10 +10,14 @@ import java.io.StringWriter; /** * Utility class for String operations. */ -public class StringUtils { +public final class StringUtils { public static final String newline = System.getProperty("line.separator"); + private StringUtils() { + // Utility class + } + /** * Get the difference of two strings. * diff --git a/src/test/java/fr/xephi/authme/util/StringUtilsTest.java b/src/test/java/fr/xephi/authme/util/StringUtilsTest.java index 47d183979..e2270b361 100644 --- a/src/test/java/fr/xephi/authme/util/StringUtilsTest.java +++ b/src/test/java/fr/xephi/authme/util/StringUtilsTest.java @@ -6,7 +6,9 @@ import java.net.MalformedURLException; import java.util.Arrays; import java.util.List; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.stringContainsInOrder; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; @@ -97,4 +99,32 @@ public class StringUtilsTest { // then assertThat(result, equalTo("[MalformedURLException]: Unrecognized URL format")); } + + @Test + public void shouldPrintStackTrace() { + // given + MalformedURLException ex = new MalformedURLException("Unrecognized URL format"); + + // when + String result = StringUtils.getStackTrace(ex); + + // then + assertThat(result, stringContainsInOrder(getClass().getName())); + } + + @Test + public void shouldGetDifferenceWithNullString() { + // given/when/then + assertThat(StringUtils.getDifference(null, "test"), equalTo(1.0)); + assertThat(StringUtils.getDifference("test", null), equalTo(1.0)); + assertThat(StringUtils.getDifference(null, null), equalTo(1.0)); + } + + @Test + public void shouldGetDifferenceBetweenTwoString() { + // given/when/then + assertThat(StringUtils.getDifference("test", "taste"), equalTo(0.4)); + assertThat(StringUtils.getDifference("test", "bear"), equalTo(0.75)); + assertThat(StringUtils.getDifference("test", "something"), greaterThan(0.88)); + } }