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.assertStartsWith;
import static org.apache.poi.POITestCase.assertEndsWith; 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.io.IOException;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -45,7 +47,6 @@ public class TestXSSFExcelExtractor extends TestCase {
public void testGetSimpleText() throws IOException { public void testGetSimpleText() throws IOException {
// a very simple file // a very simple file
XSSFExcelExtractor extractor = getExtractor("sample.xlsx"); XSSFExcelExtractor extractor = getExtractor("sample.xlsx");
extractor.getText();
String text = extractor.getText(); String text = extractor.getText();
assertTrue(text.length() > 0); assertTrue(text.length() > 0);
@ -105,7 +106,6 @@ public class TestXSSFExcelExtractor extends TestCase {
public void testGetComplexText() throws IOException { public void testGetComplexText() throws IOException {
// A fairly complex file // A fairly complex file
XSSFExcelExtractor extractor = getExtractor("AverageTaxRates.xlsx"); XSSFExcelExtractor extractor = getExtractor("AverageTaxRates.xlsx");
extractor.getText();
String text = extractor.getText(); String text = extractor.getText();
assertTrue(text.length() > 0); assertTrue(text.length() > 0);
@ -162,7 +162,7 @@ public class TestXSSFExcelExtractor extends TestCase {
String text = extractor.getText(); 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 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(); extractor.close();
} }
@ -177,14 +177,14 @@ public class TestXSSFExcelExtractor extends TestCase {
String text = extractor.getText(); String text = extractor.getText();
// No comments there yet // No comments there yet
assertFalse("Unable to find expected word in text\n" + text, text.contains("testdoc")); assertNotContained(text, "testdoc");
assertFalse("Unable to find expected word in text\n" + text, text.contains("test phrase")); assertNotContained(text, "test phrase");
// Turn on comment extraction, will then be // Turn on comment extraction, will then be
extractor.setIncludeCellComments(true); extractor.setIncludeCellComments(true);
text = extractor.getText(); text = extractor.getText();
assertTrue("Unable to find expected word in text\n" + text, text.contains("testdoc")); assertContains(text, "testdoc");
assertTrue("Unable to find expected word in text\n" + text, text.contains("test phrase")); assertContains(text, "test phrase");
extractor.close(); extractor.close();
} }
@ -195,20 +195,20 @@ public class TestXSSFExcelExtractor extends TestCase {
String text = extractor.getText(); String text = extractor.getText();
// Numbers // Numbers
assertTrue("Unable to find expected word in text\n" + text, text.contains("43")); assertContains(text, "43");
assertTrue("Unable to find expected word in text\n" + text, text.contains("22")); assertContains(text, "22");
// Strings // Strings
assertTrue("Unable to find expected word in text\n" + text, text.contains("ABCDE")); assertContains(text, "ABCDE");
assertTrue("Unable to find expected word in text\n" + text, text.contains("Long Text")); assertContains(text, "Long Text");
// Inline Strings // Inline Strings
assertTrue("Unable to find expected word in text\n" + text, text.contains("1st Inline String")); assertContains(text, "1st Inline String");
assertTrue("Unable to find expected word in text\n" + text, text.contains("And More")); assertContains(text, "And More");
// Formulas // Formulas
assertTrue("Unable to find expected word in text\n" + text, text.contains("A2")); assertContains(text, "A2");
assertTrue("Unable to find expected word in text\n" + text, text.contains("A5-A$2")); assertContains(text, "A5-A$2");
extractor.close(); extractor.close();
} }
@ -233,10 +233,10 @@ public class TestXSSFExcelExtractor extends TestCase {
XSSFExcelExtractor extractor = getExtractor("51519.xlsx"); XSSFExcelExtractor extractor = getExtractor("51519.xlsx");
try { try {
String text = extractor.getText(); 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 //this shows up only as a phonetic run and should not appear
//in the extracted text //in the extracted text
assertFalse(text.contains("\u30CB\u30DB\u30F3")); assertNotContained(text, "\u30CB\u30DB\u30F3");
} finally { } finally {
extractor.close(); 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.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue; 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.AccessibleObject;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -44,55 +50,23 @@ import org.apache.poi.util.Internal;
*/ */
@Internal @Internal
public final class POITestCase { 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) { public static void assertStartsWith(String string, String prefix) {
assertNotNull(string); assertNotNull(string);
assertNotNull(prefix); 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) { public static void assertEndsWith(String string, String suffix) {
assertNotNull(string); assertNotNull(string);
assertNotNull(suffix); 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) { public static void assertContains(String haystack, String needle) {
assertNotNull(haystack); assertNotNull(haystack);
assertTrue( assertNotNull(needle);
"Unable to find expected text '" + needle + "' in text:\n" + haystack, assertThat(haystack, containsString(needle));
haystack.contains(needle)
);
} }
public static void assertContainsIgnoreCase(String haystack, String needle, Locale locale) { 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) { public static void assertNotContained(String haystack, String needle) {
assertNotNull(haystack); assertNotNull(haystack);
assertFalse( assertNotNull(needle);
"Unexpectedly found text '" + needle + "' in text:\n" + haystack, assertThat(haystack, not(containsString(needle)));
haystack.contains(needle)
);
} }
/** /**