Increase test coverage

CharRangeTest -> 100%
RandomUtilsTest -> 100%
StringEscapeUtilsTest -> 100%
StringUtils -> 99%
This commit is contained in:
Andy Klimczak 2017-01-21 02:50:50 -05:00
parent 86a082e8be
commit 00bf35cdd9
4 changed files with 129 additions and 0 deletions

View File

@ -397,4 +397,11 @@ public class CharRangeTest {
assertEquals(range, SerializationUtils.clone(range)); assertEquals(range, SerializationUtils.clone(range));
} }
//-----------------------------------------------------------------------
@Test(expected = UnsupportedOperationException.class)
public void testIteratorRemove() {
CharRange a = CharRange.is('a');
final Iterator<Character> aIt = a.iterator();
aIt.remove();
}
} }

View File

@ -16,12 +16,17 @@
*/ */
package org.apache.commons.lang3; package org.apache.commons.lang3;
import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import org.junit.Test; import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
/** /**
* Tests for {@link RandomUtils} * Tests for {@link RandomUtils}
*/ */
@ -32,6 +37,15 @@ public class RandomUtilsTest {
*/ */
private static final double DELTA = 1e-5; private static final double DELTA = 1e-5;
@Test
public void testConstructor() {
assertNotNull(new RandomUtils());
final Constructor<?>[] cons = RandomUtils.class.getDeclaredConstructors();
assertEquals(1, cons.length);
assertTrue(Modifier.isPublic(cons[0].getModifiers()));
assertTrue(Modifier.isPublic(RandomUtils.class.getModifiers()));
assertFalse(Modifier.isFinal(RandomUtils.class.getModifiers()));
}
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testNextBytesNegative() throws Exception { public void testNextBytesNegative() throws Exception {
@ -78,6 +92,15 @@ public class RandomUtilsTest {
RandomUtils.nextFloat(2, 1); RandomUtils.nextFloat(2, 1);
} }
/**
* Tests next boolean
*/
@Test
public void testBoolean() {
boolean result = RandomUtils.nextBoolean();
assertTrue(result == true || result == false);
}
/** /**
* Tests a zero byte array length. * Tests a zero byte array length.
*/ */

View File

@ -199,6 +199,29 @@ public class StringEscapeUtilsTest {
StringEscapeUtils.escapeEcmaScript("document.getElementById(\"test\").value = '<script>alert('aaa');</script>';")); StringEscapeUtils.escapeEcmaScript("document.getElementById(\"test\").value = '<script>alert('aaa');</script>';"));
} }
@Test
public void testUnescapeEcmaScript() {
assertEquals(null, StringEscapeUtils.escapeEcmaScript(null));
try {
StringEscapeUtils.UNESCAPE_ECMASCRIPT.translate(null, null);
fail();
} catch (final IOException ex) {
fail();
} catch (final IllegalArgumentException ex) {
}
try {
StringEscapeUtils.UNESCAPE_ECMASCRIPT.translate("", null);
fail();
} catch (final IOException ex) {
fail();
} catch (final IllegalArgumentException ex) {
}
assertEquals("He didn't say, \"stop!\"", StringEscapeUtils.unescapeEcmaScript("He didn\\'t say, \\\"stop!\\\""));
assertEquals("document.getElementById(\"test\").value = '<script>alert('aaa');</script>';",
StringEscapeUtils.unescapeEcmaScript("document.getElementById(\\\"test\\\").value = \\'<script>alert(\\'aaa\\');<\\/script>\\';"));
}
// HTML and XML // HTML and XML
//-------------------------------------------------------------- //--------------------------------------------------------------
@ -470,6 +493,12 @@ public class StringEscapeUtilsTest {
} }
} }
@Test(expected = IllegalStateException.class)
public void testEscapeCsvIllegalStateException() throws IOException {
final StringWriter writer = new StringWriter();
StringEscapeUtils.ESCAPE_CSV.translate("foo", -1, writer);
}
@Test @Test
public void testUnescapeCsvString() throws Exception { public void testUnescapeCsvString() throws Exception {
assertEquals("foo.bar", StringEscapeUtils.unescapeCsv("foo.bar")); assertEquals("foo.bar", StringEscapeUtils.unescapeCsv("foo.bar"));
@ -508,6 +537,12 @@ public class StringEscapeUtilsTest {
} }
} }
@Test(expected = IllegalStateException.class)
public void testUnescapeCsvIllegalStateException() throws IOException {
final StringWriter writer = new StringWriter();
StringEscapeUtils.UNESCAPE_CSV.translate("foo", -1, writer);
}
/** /**
* Tests // https://issues.apache.org/jira/browse/LANG-480 * Tests // https://issues.apache.org/jira/browse/LANG-480
*/ */
@ -613,4 +648,29 @@ public class StringEscapeUtilsTest {
assertEquals(expected, StringEscapeUtils.escapeJson(input)); assertEquals(expected, StringEscapeUtils.escapeJson(input));
} }
@Test
public void testUnescapeJson() {
assertEquals(null, StringEscapeUtils.unescapeJson(null));
try {
StringEscapeUtils.UNESCAPE_JSON.translate(null, null);
fail();
} catch (final IOException ex) {
fail();
} catch (final IllegalArgumentException ex) {
}
try {
StringEscapeUtils.UNESCAPE_JSON.translate("", null);
fail();
} catch (final IOException ex) {
fail();
} catch (final IllegalArgumentException ex) {
}
assertEquals("He didn't say, \"stop!\"", StringEscapeUtils.unescapeJson("He didn't say, \\\"stop!\\\""));
final String expected ="\"foo\" isn't \"bar\". specials: \b\r\n\f\t\\/";
final String input = "\\\"foo\\\" isn't \\\"bar\\\". specials: \\b\\r\\n\\f\\t\\\\\\/";
assertEquals(expected, StringEscapeUtils.unescapeJson(input));
}
} }

View File

@ -220,6 +220,7 @@ public class StringUtilsTest {
final String test = "This String contains a TitleCase character: \u01C8"; final String test = "This String contains a TitleCase character: \u01C8";
final String expect = "tHIS sTRING CONTAINS A tITLEcASE CHARACTER: \u01C9"; final String expect = "tHIS sTRING CONTAINS A tITLEcASE CHARACTER: \u01C9";
assertEquals(expect, WordUtils.swapCase(test)); assertEquals(expect, WordUtils.swapCase(test));
assertEquals(expect, StringUtils.swapCase(test));
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@ -255,6 +256,7 @@ public class StringUtilsTest {
assertEquals(";;foo", StringUtils.join(MIXED_ARRAY_LIST, SEPARATOR_CHAR)); assertEquals(";;foo", StringUtils.join(MIXED_ARRAY_LIST, SEPARATOR_CHAR));
assertEquals("foo;2", StringUtils.join(MIXED_TYPE_LIST, SEPARATOR_CHAR)); assertEquals("foo;2", StringUtils.join(MIXED_TYPE_LIST, SEPARATOR_CHAR));
assertNull(StringUtils.join((Object[]) null, ',', 0, 1));
assertEquals("/", StringUtils.join(MIXED_ARRAY_LIST, '/', 0, MIXED_ARRAY_LIST.length - 1)); assertEquals("/", StringUtils.join(MIXED_ARRAY_LIST, '/', 0, MIXED_ARRAY_LIST.length - 1));
assertEquals("foo", StringUtils.join(MIXED_TYPE_LIST, '/', 0, 1)); assertEquals("foo", StringUtils.join(MIXED_TYPE_LIST, '/', 0, 1));
assertEquals("null", StringUtils.join(NULL_TO_STRING_LIST, '/', 0, 1)); assertEquals("null", StringUtils.join(NULL_TO_STRING_LIST, '/', 0, 1));
@ -611,6 +613,43 @@ public class StringUtilsTest {
} }
} }
@Test
public void testSplitByWholeSeparatorPreserveAllTokens_StringString() {
assertArrayEquals(null, StringUtils.splitByWholeSeparatorPreserveAllTokens(null, "."));
assertEquals(0, StringUtils.splitByWholeSeparatorPreserveAllTokens("", ".").length);
// test whitespace
String input = "ab de fg";
String[] expected = new String[]{"ab", "", "", "de", "fg"};
String[] actual = StringUtils.splitByWholeSeparatorPreserveAllTokens(input, null);
assertEquals(expected.length, actual.length);
for (int i = 0; i < actual.length; i += 1) {
assertEquals(expected[i], actual[i]);
}
// test delimiter singlechar
input = "1::2:::3::::4";
expected = new String[]{"1", "", "2", "", "", "3", "", "", "", "4"};
actual = StringUtils.splitByWholeSeparatorPreserveAllTokens(input, ":");
assertEquals(expected.length, actual.length);
for (int i = 0; i < actual.length; i += 1) {
assertEquals(expected[i], actual[i]);
}
// test delimiter multichar
input = "1::2:::3::::4";
expected = new String[]{"1", "2", ":3", "", "4"};
actual = StringUtils.splitByWholeSeparatorPreserveAllTokens(input, "::");
assertEquals(expected.length, actual.length);
for (int i = 0; i < actual.length; i += 1) {
assertEquals(expected[i], actual[i]);
}
}
@Test @Test
public void testSplitByWholeSeparatorPreserveAllTokens_StringStringInt() { public void testSplitByWholeSeparatorPreserveAllTokens_StringStringInt() {
assertArrayEquals(null, StringUtils.splitByWholeSeparatorPreserveAllTokens(null, ".", -1)); assertArrayEquals(null, StringUtils.splitByWholeSeparatorPreserveAllTokens(null, ".", -1));