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;
public void testGetSimpleText() throws IOException {
// a very simple file
XSSFExcelExtractor extractor = getExtractor("sample.xlsx");
- extractor.getText();
String text = extractor.getText();
assertTrue(text.length() > 0);
public void testGetComplexText() throws IOException {
// A fairly complex file
XSSFExcelExtractor extractor = getExtractor("AverageTaxRates.xlsx");
- extractor.getText();
String text = extractor.getText();
assertTrue(text.length() > 0);
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();
}
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();
}
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();
}
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();
}
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;
*/
@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) {
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)));
}
/**