use hamcrest matchers to reduce custom code

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1791443 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2017-04-15 02:50:46 +00:00
parent 7977026f96
commit e7ca1594da
2 changed files with 29 additions and 57 deletions

View File

@ -19,6 +19,8 @@ package org.apache.poi.xssf.extractor;
import static org.apache.poi.POITestCase.assertStartsWith;
import static org.apache.poi.POITestCase.assertEndsWith;
import static org.apache.poi.POITestCase.assertContains;
import static org.apache.poi.POITestCase.assertNotContained;
import java.io.IOException;
import java.util.regex.Matcher;
@ -45,7 +47,6 @@ public class TestXSSFExcelExtractor extends TestCase {
public void testGetSimpleText() throws IOException {
// a very simple file
XSSFExcelExtractor extractor = getExtractor("sample.xlsx");
extractor.getText();
String text = extractor.getText();
assertTrue(text.length() > 0);
@ -105,7 +106,6 @@ public class TestXSSFExcelExtractor extends TestCase {
public void testGetComplexText() throws IOException {
// A fairly complex file
XSSFExcelExtractor extractor = getExtractor("AverageTaxRates.xlsx");
extractor.getText();
String text = extractor.getText();
assertTrue(text.length() > 0);
@ -162,7 +162,7 @@ public class TestXSSFExcelExtractor extends TestCase {
String text = extractor.getText();
assertTrue("Unable to find expected word in text from " + sampleName + "\n" + text, text.contains("testdoc"));
assertTrue("Unable to find expected word in text\n" + text, text.contains("test phrase"));
assertContains(text, "test phrase");
extractor.close();
}
@ -177,14 +177,14 @@ public class TestXSSFExcelExtractor extends TestCase {
String text = extractor.getText();
// No comments there yet
assertFalse("Unable to find expected word in text\n" + text, text.contains("testdoc"));
assertFalse("Unable to find expected word in text\n" + text, text.contains("test phrase"));
assertNotContained(text, "testdoc");
assertNotContained(text, "test phrase");
// Turn on comment extraction, will then be
extractor.setIncludeCellComments(true);
text = extractor.getText();
assertTrue("Unable to find expected word in text\n" + text, text.contains("testdoc"));
assertTrue("Unable to find expected word in text\n" + text, text.contains("test phrase"));
assertContains(text, "testdoc");
assertContains(text, "test phrase");
extractor.close();
}
@ -195,20 +195,20 @@ public class TestXSSFExcelExtractor extends TestCase {
String text = extractor.getText();
// Numbers
assertTrue("Unable to find expected word in text\n" + text, text.contains("43"));
assertTrue("Unable to find expected word in text\n" + text, text.contains("22"));
assertContains(text, "43");
assertContains(text, "22");
// Strings
assertTrue("Unable to find expected word in text\n" + text, text.contains("ABCDE"));
assertTrue("Unable to find expected word in text\n" + text, text.contains("Long Text"));
assertContains(text, "ABCDE");
assertContains(text, "Long Text");
// Inline Strings
assertTrue("Unable to find expected word in text\n" + text, text.contains("1st Inline String"));
assertTrue("Unable to find expected word in text\n" + text, text.contains("And More"));
assertContains(text, "1st Inline String");
assertContains(text, "And More");
// Formulas
assertTrue("Unable to find expected word in text\n" + text, text.contains("A2"));
assertTrue("Unable to find expected word in text\n" + text, text.contains("A5-A$2"));
assertContains(text, "A2");
assertContains(text, "A5-A$2");
extractor.close();
}
@ -233,10 +233,10 @@ public class TestXSSFExcelExtractor extends TestCase {
XSSFExcelExtractor extractor = getExtractor("51519.xlsx");
try {
String text = extractor.getText();
assertTrue(text.contains("\u8C4A\u7530"));
assertContains(text, "\u8C4A\u7530");
//this shows up only as a phonetic run and should not appear
//in the extracted text
assertFalse(text.contains("\u30CB\u30DB\u30F3"));
assertNotContained(text, "\u30CB\u30DB\u30F3");
} finally {
extractor.close();
}

View File

@ -22,9 +22,15 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.not;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@ -44,55 +50,23 @@ 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));
assertThat(string, startsWith(prefix));
}
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));
assertThat(string, endsWith(suffix));
}
public static void assertContains(String haystack, String needle) {
assertNotNull(haystack);
assertTrue(
"Unable to find expected text '" + needle + "' in text:\n" + haystack,
haystack.contains(needle)
);
assertNotNull(needle);
assertThat(haystack, containsString(needle));
}
public static void assertContainsIgnoreCase(String haystack, String needle, Locale locale) {
@ -110,10 +84,8 @@ public final class POITestCase {
public static void assertNotContained(String haystack, String needle) {
assertNotNull(haystack);
assertFalse(
"Unexpectedly found text '" + needle + "' in text:\n" + haystack,
haystack.contains(needle)
);
assertNotNull(needle);
assertThat(haystack, not(containsString(needle)));
}
/**