use assertStartsWith and assertEndsWith for better unit test error messages

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1791440 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2017-04-15 02:19:24 +00:00
parent 70ae27f96d
commit 7977026f96
3 changed files with 53 additions and 8 deletions

View File

@ -17,6 +17,9 @@
package org.apache.poi.xssf.extractor;
import static org.apache.poi.POITestCase.assertStartsWith;
import static org.apache.poi.POITestCase.assertEndsWith;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -48,8 +51,8 @@ public class TestXSSFExcelExtractor extends TestCase {
assertTrue(text.length() > 0);
// Check sheet names
assertTrue(text.startsWith("Sheet1"));
assertTrue(text.endsWith("Sheet3\n"));
assertStartsWith(text, "Sheet1");
assertEndsWith(text, "Sheet3\n");
// Now without, will have text
extractor.setIncludeSheetNames(false);
@ -109,10 +112,10 @@ public class TestXSSFExcelExtractor extends TestCase {
// Might not have all formatting it should do!
// TODO decide if we should really have the "null" in there
assertTrue(text.startsWith(
assertStartsWith(text,
"Avgtxfull\n" +
"null\t(iii) AVERAGE TAX RATES ON ANNUAL"
));
);
extractor.close();
}
@ -135,7 +138,7 @@ public class TestXSSFExcelExtractor extends TestCase {
for (POITextExtractor extractor : extractors) {
String text = extractor.getText().replaceAll("[\r\t]", "");
assertTrue(text.startsWith("First Sheet\nTest spreadsheet\n2nd row2nd row 2nd column\n"));
assertStartsWith(text, "First Sheet\nTest spreadsheet\n2nd row2nd row 2nd column\n");
Pattern pattern = Pattern.compile(".*13(\\.0+)?\\s+Sheet3.*", Pattern.DOTALL);
Matcher m = pattern.matcher(text);
assertTrue(m.matches());

View File

@ -44,12 +44,47 @@ import org.apache.poi.util.Internal;
*/
@Internal
public final class POITestCase {
/*
* Returns the first {@code length} characters from the string {@code s}
*/
private static String head(String s, int length) {
if (length <= 0) {
return "";
}
if (s.length() <= length) {
return s;
}
final StringBuilder sb = new StringBuilder(s.substring(0, length));
sb.append("... (length=").append(s.length()).append(")");
return sb.toString();
}
/*
* Returns the last {@code length} characters from the string {@code s}
*/
private static String tail(String s, int length) {
if (length <= 0) {
return "";
}
if (s.length() <= length) {
return s;
}
final StringBuilder sb = new StringBuilder();
sb.append("(length=").append(s.length()).append(") ...");
sb.append(s.substring(s.length() - length));
return sb.toString();
}
public static void assertStartsWith(String string, String prefix) {
assertNotNull(string);
assertNotNull(prefix);
assertEquals("string does not start with prefix", prefix, head(string, prefix.length()+5));
}
final int len = Math.min(string.length(), prefix.length());
assertEquals("string does not start with prefix", prefix, string.substring(0, len));
public static void assertEndsWith(String string, String suffix) {
assertNotNull(string);
assertNotNull(suffix);
assertEquals("string does not end with suffix", suffix, tail(string, suffix.length()+5));
}
public static void assertContains(String haystack, String needle) {

View File

@ -40,6 +40,13 @@ public final class TestPOITestCase {
POITestCase.assertStartsWith("Apache POI", "Apache POI");
}
@Test
public void assertEndsWith() {
POITestCase.assertEndsWith("Apache POI", "");
POITestCase.assertEndsWith("Apache POI", "POI");
POITestCase.assertEndsWith("Apache POI", "Apache POI");
}
@Test
public void assertContains() {
POITestCase.assertContains("There is a needle in this haystack", "needle");