summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaven O'Neal <onealj@apache.org>2017-04-15 02:19:24 +0000
committerJaven O'Neal <onealj@apache.org>2017-04-15 02:19:24 +0000
commit5e81082b937db500ddbe47afc82dacefc98f1208 (patch)
tree9316d6a8b2734a9bbee1abbd92b2be17fbcadf1b
parentd572959cd33189b1c70b9069c7312753d979411d (diff)
downloadpoi-5e81082b937db500ddbe47afc82dacefc98f1208.tar.gz
poi-5e81082b937db500ddbe47afc82dacefc98f1208.zip
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
-rw-r--r--src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java13
-rw-r--r--src/testcases/org/apache/poi/POITestCase.java41
-rw-r--r--src/testcases/org/apache/poi/TestPOITestCase.java7
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 d82ac62054..0c0c7e8891 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 3664da686e..a5895c4f0e 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 314009604c..fd4000854a 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() {