poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From one...@apache.org
Subject svn commit: r1791440 - in /poi/trunk/src: ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java testcases/org/apache/poi/POITestCase.java testcases/org/apache/poi/TestPOITestCase.java
Date Sat, 15 Apr 2017 02:19:24 GMT
Author: onealj
Date: Sat Apr 15 02:19:24 2017
New Revision: 1791440

URL: http://svn.apache.org/viewvc?rev=1791440&view=rev
Log:
use assertStartsWith and assertEndsWith for better unit test error messages

Modified:
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java
    poi/trunk/src/testcases/org/apache/poi/POITestCase.java
    poi/trunk/src/testcases/org/apache/poi/TestPOITestCase.java

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java?rev=1791440&r1=1791439&r2=1791440&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java
(original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExcelExtractor.java
Sat Apr 15 02:19:24 2017
@@ -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 exte
 		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 exte
 		
 		// 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 exte
 		
 		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());			

Modified: poi/trunk/src/testcases/org/apache/poi/POITestCase.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/POITestCase.java?rev=1791440&r1=1791439&r2=1791440&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/POITestCase.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/POITestCase.java Sat Apr 15 02:19:24 2017
@@ -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) {

Modified: poi/trunk/src/testcases/org/apache/poi/TestPOITestCase.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/TestPOITestCase.java?rev=1791440&r1=1791439&r2=1791440&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/TestPOITestCase.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/TestPOITestCase.java Sat Apr 15 02:19:24 2017
@@ -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() {



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message