Merge ListUtil into StringUtil; refactor HelpSyntaxHelper + create test

The HelpSyntaxHelper had suppressed warnings for string concatenation within StringBuilder - the point of the StringBuilder is that it is faster when you use it to concatenate many elements. If you still use string concatenation with + within these calls it beats the purpose.
(cherry picked from commit bb00be2)
This commit is contained in:
ljacqu
2015-11-21 11:57:04 +01:00
parent 115680a363
commit 4702a1b82d
6 changed files with 220 additions and 102 deletions
@@ -0,0 +1,38 @@
package fr.xephi.authme.command;
import org.junit.Test;
import java.util.Arrays;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Test for {@link CommandParts}.
*/
public class CommandPartsTest {
@Test
public void shouldPrintPartsForStringRepresentation() {
// given
CommandParts parts = new CommandParts(Arrays.asList("some", "parts", "for", "test"));
// when
String str = parts.toString();
// then
assertThat(str, equalTo("some parts for test"));
}
@Test
public void shouldPrintEmptyStringForNoArguments() {
// given
CommandParts parts = new CommandParts();
// when
String str = parts.toString();
// then
assertThat(str, equalTo(""));
}
}
@@ -0,0 +1,87 @@
package fr.xephi.authme.util;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/**
* Test for {@link StringUtils}.
*/
public class StringUtilsTest {
@Test
public void shouldFindContainedItem() {
// given
String text = "This is a test of containsAny()";
String piece = "test";
// when
boolean result = StringUtils.containsAny(text, "some", "words", "that", "do not", "exist", piece);
// then
assertThat(result, equalTo(true));
}
@Test
public void shouldReturnFalseIfNoneFound() {
// given
String text = "This is a test string";
// when
boolean result = StringUtils.containsAny(text, "some", "other", "words", null);
// then
assertThat(result, equalTo(false));
}
@Test
public void shouldReturnFalseForNullString() {
// given/when
boolean result = StringUtils.containsAny(null, "some", "words", "to", "check");
// then
assertThat(result, equalTo(false));
}
@Test
public void shouldCheckIsEmptyUtil() {
// Should be true for null/empty/whitespace
assertTrue(StringUtils.isEmpty(null));
assertTrue(StringUtils.isEmpty(""));
assertTrue(StringUtils.isEmpty(" \t"));
// Should be false if string has content
assertFalse(StringUtils.isEmpty("P"));
assertFalse(StringUtils.isEmpty(" test"));
}
@Test
public void shouldJoinString() {
// given
List<String> elements = Arrays.asList("test", "for", null, "join", "StringUtils");
// when
String result = StringUtils.join(", ", elements);
// then
assertThat(result, equalTo("test, for, join, StringUtils"));
}
@Test
public void shouldNotHaveDelimiter() {
// given
List<String> elements = Arrays.asList(" ", null, "\t", "hello", null);
// when
String result = StringUtils.join("-", elements);
// then
assertThat(result, equalTo("hello"));
}
}