From 7977026f964cde98eaa8902528a79874613b564e Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Sat, 15 Apr 2017 02:19:24 +0000 Subject: [PATCH] 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 --- .../extractor/TestXSSFExcelExtractor.java | 13 +++--- src/testcases/org/apache/poi/POITestCase.java | 41 +++++++++++++++++-- .../org/apache/poi/TestPOITestCase.java | 7 ++++ 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java index d82ac6205..0c0c7e889 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java @@ -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()); diff --git a/src/testcases/org/apache/poi/POITestCase.java b/src/testcases/org/apache/poi/POITestCase.java index 3664da686..a5895c4f0 100644 --- a/src/testcases/org/apache/poi/POITestCase.java +++ b/src/testcases/org/apache/poi/POITestCase.java @@ -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); - - final int len = Math.min(string.length(), prefix.length()); - assertEquals("string does not start with prefix", prefix, string.substring(0, len)); + assertEquals("string does not start with prefix", prefix, head(string, prefix.length()+5)); + } + + 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) { diff --git a/src/testcases/org/apache/poi/TestPOITestCase.java b/src/testcases/org/apache/poi/TestPOITestCase.java index 314009604..fd4000854 100644 --- a/src/testcases/org/apache/poi/TestPOITestCase.java +++ b/src/testcases/org/apache/poi/TestPOITestCase.java @@ -39,6 +39,13 @@ public final class TestPOITestCase { POITestCase.assertStartsWith("Apache POI", "Apache"); 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() {