Use assertContains instead of assertTrue(text.contains) for better error messages on failure

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1791857 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2017-04-19 06:13:33 +00:00
parent 511c2544a8
commit d4d08d5ce8
3 changed files with 50 additions and 44 deletions

View File

@ -16,6 +16,8 @@
==================================================================== */ ==================================================================== */
package org.apache.poi; package org.apache.poi;
import static org.apache.poi.POITestCase.assertContains;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
@ -44,8 +46,8 @@ public final class TestXMLPropertiesTextExtractor extends TestCase {
String text = textExt.getText(); String text = textExt.getText();
String cText = textExt.getCorePropertiesText(); String cText = textExt.getCorePropertiesText();
assertTrue(text.contains("LastModifiedBy = Yury Batrakov")); assertContains(text, "LastModifiedBy = Yury Batrakov");
assertTrue(cText.contains("LastModifiedBy = Yury Batrakov")); assertContains(cText, "LastModifiedBy = Yury Batrakov");
textExt.close(); textExt.close();
ext.close(); ext.close();
@ -64,8 +66,8 @@ public final class TestXMLPropertiesTextExtractor extends TestCase {
String text = ext.getText(); String text = ext.getText();
String cText = ext.getCorePropertiesText(); String cText = ext.getCorePropertiesText();
assertTrue(text.contains("LastModifiedBy = Yury Batrakov")); assertContains(text, "LastModifiedBy = Yury Batrakov");
assertTrue(cText.contains("LastModifiedBy = Yury Batrakov")); assertContains(cText, "LastModifiedBy = Yury Batrakov");
ext.close(); ext.close();
} }
@ -83,10 +85,10 @@ public final class TestXMLPropertiesTextExtractor extends TestCase {
String text = ext.getText(); String text = ext.getText();
String eText = ext.getExtendedPropertiesText(); String eText = ext.getExtendedPropertiesText();
assertTrue(text.contains("Application = Microsoft Excel")); assertContains(text, "Application = Microsoft Excel");
assertTrue(text.contains("Company = Mera")); assertContains(text, "Company = Mera");
assertTrue(eText.contains("Application = Microsoft Excel")); assertContains(eText, "Application = Microsoft Excel");
assertTrue(eText.contains("Company = Mera")); assertContains(eText, "Company = Mera");
ext.close(); ext.close();
} }
@ -104,8 +106,8 @@ public final class TestXMLPropertiesTextExtractor extends TestCase {
String text = ext.getText(); String text = ext.getText();
String cText = ext.getCustomPropertiesText(); String cText = ext.getCustomPropertiesText();
assertTrue(text.contains("description = another value")); assertContains(text, "description = another value");
assertTrue(cText.contains("description = another value")); assertContains(cText, "description = another value");
ext.close(); ext.close();
} }
@ -125,8 +127,8 @@ public final class TestXMLPropertiesTextExtractor extends TestCase {
String text = ext.getText(); String text = ext.getText();
assertFalse(text.contains("Created =")); // With date is null assertFalse(text.contains("Created =")); // With date is null
assertTrue(text.contains("CreatedString = ")); // Via string is blank assertContains(text, "CreatedString = "); // Via string is blank
assertTrue(text.contains("LastModifiedBy = IT Client Services")); assertContains(text, "LastModifiedBy = IT Client Services");
ext.close(); ext.close();
} }

View File

@ -27,6 +27,9 @@ import junit.framework.TestCase;
import org.apache.poi.util.StringUtil; import org.apache.poi.util.StringUtil;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFDocument;
import static org.apache.poi.POITestCase.assertContains;
import static org.apache.poi.POITestCase.assertEndsWith;
import static org.apache.poi.POITestCase.assertStartsWith;
/** /**
* Tests for HXFWordExtractor * Tests for HXFWordExtractor
@ -46,12 +49,12 @@ public class TestXWPFWordExtractor extends TestCase {
assertTrue(text.length() > 0); assertTrue(text.length() > 0);
// Check contents // Check contents
assertTrue(text.startsWith( assertStartsWith(text,
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nunc at risus vel erat tempus posuere. Aenean non ante. Suspendisse vehicula dolor sit amet odio." "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nunc at risus vel erat tempus posuere. Aenean non ante. Suspendisse vehicula dolor sit amet odio."
)); );
assertTrue(text.endsWith( assertEndsWith(text,
"Phasellus ultricies mi nec leo. Sed tempus. In sit amet lorem at velit faucibus vestibulum.\n" "Phasellus ultricies mi nec leo. Sed tempus. In sit amet lorem at velit faucibus vestibulum.\n"
)); );
// Check number of paragraphs by counting number of newlines // Check number of paragraphs by counting number of newlines
int numberOfParagraphs = StringUtil.countMatches(text, '\n'); int numberOfParagraphs = StringUtil.countMatches(text, '\n');
@ -76,15 +79,15 @@ public class TestXWPFWordExtractor extends TestCase {
// System.err.println("'"+text.substring(text.length() - 40) + "'"); // System.err.println("'"+text.substring(text.length() - 40) + "'");
// Check contents // Check contents
assertTrue(text.startsWith( assertStartsWith(text,
" \n(V) ILLUSTRATIVE CASES\n\n" " \n(V) ILLUSTRATIVE CASES\n\n"
)); );
assertTrue(text.contains( assertContains(text,
"As well as gaining " + euro + "90 from child benefit increases, he will also receive the early childhood supplement of " + euro + "250 per quarter for Vincent for the full four quarters of the year.\n\n\n\n"// \n\n\n" "As well as gaining " + euro + "90 from child benefit increases, he will also receive the early childhood supplement of " + euro + "250 per quarter for Vincent for the full four quarters of the year.\n\n\n\n"// \n\n\n"
)); );
assertTrue(text.endsWith( assertEndsWith(text,
"11.4%\t\t90\t\t\t\t\t250\t\t1,310\t\n\n \n\n\n" "11.4%\t\t90\t\t\t\t\t250\t\t1,310\t\n\n \n\n\n"
)); );
// Check number of paragraphs by counting number of newlines // Check number of paragraphs by counting number of newlines
int numberOfParagraphs = StringUtil.countMatches(text, '\n'); int numberOfParagraphs = StringUtil.countMatches(text, '\n');
@ -173,8 +176,8 @@ public class TestXWPFWordExtractor extends TestCase {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("footnotes.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("footnotes.docx");
XWPFWordExtractor extractor = new XWPFWordExtractor(doc); XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
String text = extractor.getText(); String text = extractor.getText();
assertTrue(text.contains("snoska")); assertContains(text,"snoska");
assertTrue(text.contains("Eto ochen prostoy[footnoteRef:1] text so snoskoy")); assertContains(text,"Eto ochen prostoy[footnoteRef:1] text so snoskoy");
extractor.close(); extractor.close();
} }
@ -184,7 +187,7 @@ public class TestXWPFWordExtractor extends TestCase {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("table_footnotes.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("table_footnotes.docx");
XWPFWordExtractor extractor = new XWPFWordExtractor(doc); XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
assertTrue(extractor.getText().contains("snoska")); assertContains(extractor.getText(),"snoska");
extractor.close(); extractor.close();
} }
@ -194,8 +197,8 @@ public class TestXWPFWordExtractor extends TestCase {
XWPFWordExtractor extractor = new XWPFWordExtractor(doc); XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
String text = extractor.getText(); String 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();
} }
@ -204,8 +207,8 @@ public class TestXWPFWordExtractor extends TestCase {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("endnotes.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("endnotes.docx");
XWPFWordExtractor extractor = new XWPFWordExtractor(doc); XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
String text = extractor.getText(); String text = extractor.getText();
assertTrue(text.contains("XXX")); assertContains(text,"XXX");
assertTrue(text.contains("tilaka [endnoteRef:2]or 'tika'")); assertContains(text,"tilaka [endnoteRef:2]or 'tika'");
extractor.close(); extractor.close();
} }
@ -214,8 +217,8 @@ public class TestXWPFWordExtractor extends TestCase {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("delins.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("delins.docx");
XWPFWordExtractor extractor = new XWPFWordExtractor(doc); XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
assertTrue(extractor.getText().contains("pendant worn")); assertContains(extractor.getText(),"pendant worn");
assertTrue(extractor.getText().contains("extremely well")); assertContains(extractor.getText(),"extremely well");
extractor.close(); extractor.close();
} }
@ -224,9 +227,9 @@ public class TestXWPFWordExtractor extends TestCase {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Headers.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Headers.docx");
XWPFWordExtractor extractor = new XWPFWordExtractor(doc); XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
assertTrue(extractor.getText().contains("Section 1")); assertContains(extractor.getText(),"Section 1");
assertTrue(extractor.getText().contains("Section 2")); assertContains(extractor.getText(),"Section 2");
assertTrue(extractor.getText().contains("Section 3")); assertContains(extractor.getText(),"Section 3");
extractor.close(); extractor.close();
} }
@ -241,9 +244,9 @@ public class TestXWPFWordExtractor extends TestCase {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("45690.docm"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("45690.docm");
XWPFWordExtractor extractor = new XWPFWordExtractor(doc); XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
assertTrue(extractor.getText().contains("2004")); assertContains(extractor.getText(),"2004");
assertTrue(extractor.getText().contains("2008")); assertContains(extractor.getText(),"2008");
assertTrue(extractor.getText().contains("(120 ")); assertContains(extractor.getText(),"(120 ");
extractor.close(); extractor.close();
} }
@ -260,12 +263,12 @@ public class TestXWPFWordExtractor extends TestCase {
XWPFWordExtractor extractor = new XWPFWordExtractor(doc); XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
// Check bits // Check bits
assertTrue(extractor.getText().contains("a")); assertContains(extractor.getText(),"a");
assertTrue(extractor.getText().contains("\t")); assertContains(extractor.getText(),"\t");
assertTrue(extractor.getText().contains("b")); assertContains(extractor.getText(),"b");
// Now check the first paragraph in total // Now check the first paragraph in total
assertTrue(extractor.getText().contains("a\tb\n")); assertContains(extractor.getText(),"a\tb\n");
extractor.close(); extractor.close();
} }
@ -298,7 +301,7 @@ public class TestXWPFWordExtractor extends TestCase {
XWPFWordExtractor extractor = new XWPFWordExtractor(doc); XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
String text = extractor.getText(); String text = extractor.getText();
assertTrue(text.length() > 0); assertTrue(text.length() > 0);
assertTrue(text.contains("FldSimple.docx")); assertContains(text,"FldSimple.docx");
extractor.close(); extractor.close();
} }

View File

@ -20,6 +20,7 @@ import java.io.IOException;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
import static org.apache.poi.POITestCase.assertContains;
/** /**
* Tests for reading SmartTags from Word docx. * Tests for reading SmartTags from Word docx.
@ -31,8 +32,8 @@ public final class TestXWPFSmartTag extends TestCase {
public void testSmartTags() throws IOException { public void testSmartTags() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("smarttag-snippet.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("smarttag-snippet.docx");
XWPFParagraph p = doc.getParagraphArray(0); XWPFParagraph p = doc.getParagraphArray(0);
assertTrue(p.getText().contains("Carnegie Mellon University School of Computer Science")); assertContains(p.getText(), "Carnegie Mellon University School of Computer Science");
p = doc.getParagraphArray(2); p = doc.getParagraphArray(2);
assertTrue(p.getText().contains("Alice's Adventures")); assertContains(p.getText(), "Alice's Adventures");
} }
} }