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

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1792009 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2017-04-20 01:28:40 +00:00
parent fc23277dd4
commit 5189011805
12 changed files with 93 additions and 101 deletions

View File

@ -18,6 +18,7 @@ package org.apache.poi.xslf.extractor;
import static org.apache.poi.POITestCase.assertContains;
import static org.apache.poi.POITestCase.assertNotContained;
import static org.apache.poi.POITestCase.assertStartsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@ -64,7 +65,7 @@ public class TestXSLFPowerPointExtractor {
assertTrue(text.length() > 0);
// Check Basics
assertTrue(text.startsWith("Lorem ipsum dolor sit amet\n"));
assertStartsWith(text, "Lorem ipsum dolor sit amet\n");
assertContains(text, "amet\n\n");
// Our placeholder master text
@ -221,7 +222,7 @@ public class TestXSLFPowerPointExtractor {
assertTrue(text.length() > 0);
// Check comments are there
assertTrue("Unable to find expected word in text\n" + text, text.contains("TEST"));
assertContains(text, "TEST");
extractor.close();
xml.close();
@ -246,31 +247,16 @@ public class TestXSLFPowerPointExtractor {
String text = extractor.getText();
if (extension.equals("thmx")) {
// Theme file doesn't have any textual content
assertEquals(0, text.length());
assertEquals(filename, 0, text.length());
continue;
}
assertTrue(text.length() > 0);
assertTrue(
"Text missing for " + filename + "\n" + text,
text.contains("Attachment Test")
);
assertTrue(
"Text missing for " + filename + "\n" + text,
text.contains("This is a test file data with the same content")
);
assertTrue(
"Text missing for " + filename + "\n" + text,
text.contains("content parsing")
);
assertTrue(
"Text missing for " + filename + "\n" + text,
text.contains("Different words to test against")
);
assertTrue(
"Text missing for " + filename + "\n" + text,
text.contains("Mystery")
);
assertTrue(filename, text.length() > 0);
assertContains(filename, text, "Attachment Test");
assertContains(filename, text, "This is a test file data with the same content");
assertContains(filename, text, "content parsing");
assertContains(filename, text, "Different words to test against");
assertContains(filename, text, "Mystery");
extractor.close();
xml.close();

View File

@ -18,6 +18,8 @@
package org.apache.poi.xssf.extractor;
import static org.apache.poi.POITestCase.assertContains;
import static org.apache.poi.POITestCase.assertStartsWith;
import static org.apache.poi.POITestCase.assertEndsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@ -49,8 +51,8 @@ public class TestXSSFBEventBasedExcelExtractor {
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);

View File

@ -18,6 +18,8 @@
package org.apache.poi.xssf.extractor;
import static org.apache.poi.POITestCase.assertContains;
import static org.apache.poi.POITestCase.assertStartsWith;
import static org.apache.poi.POITestCase.assertEndsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@ -55,8 +57,8 @@ public class TestXSSFEventBasedExcelExtractor {
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);
@ -116,10 +118,10 @@ public class TestXSSFEventBasedExcelExtractor {
assertTrue(text.length() > 0);
// Might not have all formatting it should do!
assertTrue(text.startsWith(
assertStartsWith(text,
"Avgtxfull\n" +
"(iii) AVERAGE TAX RATES ON ANNUAL"
));
);
extractor.close();
}
@ -166,7 +168,7 @@ public class TestXSSFEventBasedExcelExtractor {
new POITextExtractor[] { ooxmlExtractor, ole2Extractor };
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());

View File

@ -19,6 +19,8 @@
package org.apache.poi.xssf.streaming;
import static org.apache.poi.POITestCase.assertStartsWith;
import static org.apache.poi.POITestCase.assertEndsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@ -245,8 +247,8 @@ public final class TestSXSSFWorkbook extends BaseTestXWorkbook {
SheetDataWriter wr = sh.getSheetDataWriter();
assertTrue(wr.getClass() == SheetDataWriter.class);
File tmp = wr.getTempFile();
assertTrue(tmp.getName().startsWith("poi-sxssf-sheet"));
assertTrue(tmp.getName().endsWith(".xml"));
assertStartsWith(tmp.getName(), "poi-sxssf-sheet");
assertEndsWith(tmp.getName(), ".xml");
assertTrue(wb.dispose());
wb.close();
@ -256,8 +258,8 @@ public final class TestSXSSFWorkbook extends BaseTestXWorkbook {
wr = sh.getSheetDataWriter();
assertTrue(wr.getClass() == GZIPSheetDataWriter.class);
tmp = wr.getTempFile();
assertTrue(tmp.getName().startsWith("poi-sxssf-sheet-xml"));
assertTrue(tmp.getName().endsWith(".gz"));
assertStartsWith(tmp.getName(), "poi-sxssf-sheet-xml");
assertEndsWith(tmp.getName(), ".gz");
assertTrue(wb.dispose());
wb.close();

View File

@ -19,6 +19,7 @@ package org.apache.poi.xwpf.usermodel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.apache.poi.POITestCase.assertContains;
import java.io.IOException;
import java.util.ArrayList;
@ -96,20 +97,20 @@ public final class TestXWPFSDT {
List<AbstractXWPFSDT> sdts = extractAllSDTs(doc);
String text = sdts.get(0).getContent().getText();
assertEquals(2, sdts.size());
assertTrue(text.indexOf("Test") > -1);
assertContains(text, "Test");
text = sdts.get(1).getContent().getText();
assertTrue(text.indexOf("Test Subtitle") > -1);
assertTrue(text.indexOf("Test User") > -1);
assertContains(text, "Test Subtitle");
assertContains(text, "Test User");
assertTrue(text.indexOf("Test") < text.indexOf("Test Subtitle"));
doc = XWPFTestDataSamples.openSampleDocument("Bug54771b.docx");
sdts = extractAllSDTs(doc);
assertEquals(3, sdts.size());
assertTrue(sdts.get(0).getContent().getText().indexOf("Test") > -1);
assertContains(sdts.get(0).getContent().getText(), "Test");
assertTrue(sdts.get(1).getContent().getText().indexOf("Test Subtitle") > -1);
assertTrue(sdts.get(2).getContent().getText().indexOf("Test User") > -1);
assertContains(sdts.get(1).getContent().getText(), "Test Subtitle");
assertContains(sdts.get(2).getContent().getText(), "Test User");
}

View File

@ -17,9 +17,9 @@
package org.apache.poi.hpbf.extractor;
import static org.apache.poi.POITestCase.assertEndsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.InputStream;
@ -179,9 +179,9 @@ public final class TestPublisherTextExtractor {
ext.close();
sample.close();
assertTrue(text.endsWith("<http://poi.apache.org/>\n"
assertEndsWith(text, "<http://poi.apache.org/>\n"
+ "<C:\\Documents and Settings\\Nick\\My Documents\\Booleans.xlsx>\n"
+ "<>\n" + "<mailto:dev@poi.apache.org?subject=HPBF>\n"
+ "<mailto:dev@poi.apache.org?subject=HPBF>\n"));
+ "<mailto:dev@poi.apache.org?subject=HPBF>\n");
}
}

View File

@ -17,6 +17,8 @@
package org.apache.poi.hpbf.model;
import static org.apache.poi.POITestCase.assertStartsWith;
import static org.apache.poi.POITestCase.assertEndsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
@ -81,8 +83,8 @@ public final class TestQuillContents {
QCTextBit text = (QCTextBit)qc.getBits()[0];
String t = text.getText();
assertTrue(t.startsWith("This is some text on the first page"));
assertTrue(t.endsWith("Within doc to page 1\r"));
assertStartsWith(t, "This is some text on the first page");
assertEndsWith(t, "Within doc to page 1\r");
doc.close();
}

View File

@ -432,7 +432,7 @@ public final class TestExtractor {
"Row 3, Cell 1\tRow 3, Cell 2\tRow 3, Cell 3\tRow 3, Cell 4\n"+
"Row 4, Cell 1\tRow 4, Cell 2\tRow 4, Cell 3\tRow 4, Cell 4\n"+
"Row 5, Cell 1\tRow 5, Cell 2\tRow 5, Cell 3\tRow 5, Cell 4\n";
assertTrue(text1.contains(target1));
assertContains(text1, target1);
ppe1.close();
PowerPointExtractor ppe2 = openExtractor("54722.ppt");
@ -440,7 +440,7 @@ public final class TestExtractor {
String target2 = "this\tText\tis\twithin\ta\n" +
"table\t1\t2\t3\t4";
assertTrue(text2.contains(target2));
assertContains(text2, target2);
ppe2.close();
}

View File

@ -18,6 +18,7 @@
package org.apache.poi.hslf.usermodel;
import static org.apache.poi.POITestCase.assertContains;
import static org.apache.poi.POITestCase.assertStartsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@ -162,8 +163,8 @@ public final class TestBugs {
assertNotNull(notes);
String text = HSLFTextParagraph.getRawText(notes.getTextParagraphs().get(0));
String startingPhrase = notesMap.get(slideNumber);
assertTrue("Notes for slide " + slideNumber + " must start with " +
startingPhrase , text.startsWith(startingPhrase));
assertStartsWith("Notes for slide " + slideNumber + " must start with starting phrase",
text, startingPhrase);
}
}
@ -306,15 +307,15 @@ public final class TestBugs {
List<List<HSLFTextParagraph>> paras = slide.get(0).getTextParagraphs();
assertEquals(4, paras.size());
Set<String> txt = new HashSet<String>();
txt.add("\u201CHAPPY BIRTHDAY SCOTT\u201D");
txt.add("Have a HAPPY DAY");
txt.add("PS Nobody is allowed to hassle Scott TODAY\u2026");
txt.add("Drinks will be in the Boardroom at 5pm today to celebrate Scott\u2019s B\u2019Day\u2026 See you all there!");
Set<String> expected = new HashSet<String>();
expected.add("\u201CHAPPY BIRTHDAY SCOTT\u201D");
expected.add("Have a HAPPY DAY");
expected.add("PS Nobody is allowed to hassle Scott TODAY\u2026");
expected.add("Drinks will be in the Boardroom at 5pm today to celebrate Scott\u2019s B\u2019Day\u2026 See you all there!");
for (List<HSLFTextParagraph> para : paras) {
String text = HSLFTextParagraph.getRawText(para);
assertTrue(text, txt.contains(text));
assertTrue(text, expected.contains(text));
}
ppt.close();

View File

@ -19,6 +19,7 @@ package org.apache.poi.hwmf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.apache.poi.POITestCase.assertContains;
import javax.imageio.ImageIO;
import java.awt.Dimension;
@ -218,8 +219,8 @@ public class TestHwmfParsing {
}
}
String txt = sb.toString();
assertTrue(txt.contains("\u041E\u0431\u0449\u043E"));
assertTrue(txt.contains("\u0411\u0430\u043B\u0430\u043D\u0441"));
assertContains(txt, "\u041E\u0431\u0449\u043E");
assertContains(txt, "\u0411\u0430\u043B\u0430\u043D\u0441");
}
@Test
@ -246,6 +247,6 @@ public class TestHwmfParsing {
}
}
String txt = sb.toString();
assertTrue(txt.contains("\u822A\u7A7A\u60C5\u5831\u696D\u52D9\u3078\u306E\uFF27\uFF29\uFF33"));
assertContains(txt, "\u822A\u7A7A\u60C5\u5831\u696D\u52D9\u3078\u306E\uFF27\uFF29\uFF33");
}
}

View File

@ -17,6 +17,7 @@
package org.apache.poi.hwpf.model;
import static org.apache.poi.POITestCase.assertStartsWith;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@ -100,8 +101,7 @@ public final class TestTextPieceTable {
assertEquals(339, tp.getEnd());
assertEquals(339, tp.characterLength());
assertEquals(339, tp.bytesLength());
assertTrue(tp.getStringBuilder().toString()
.startsWith("This is a sample word document"));
assertStartsWith(tp.getStringBuilder().toString(), "This is a sample word document");
// Save and re-load
HWPFDocument docB = saveAndReload(doc);
@ -114,8 +114,7 @@ public final class TestTextPieceTable {
assertEquals(339, tp.getEnd());
assertEquals(339, tp.characterLength());
assertEquals(339, tp.bytesLength());
assertTrue(tp.getStringBuilder().toString()
.startsWith("This is a sample word document"));
assertStartsWith(tp.getStringBuilder().toString(), "This is a sample word document");
}
/**

View File

@ -17,6 +17,8 @@
package org.apache.poi.hpsf.extractor;
import static org.apache.poi.POITestCase.assertContains;
import java.io.IOException;
import junit.framework.TestCase;
@ -35,23 +37,21 @@ public final class TestHPSFPropertiesExtractor extends TestCase {
POIFSFileSystem fs = new POIFSFileSystem(_samples.openResourceAsStream("TestMickey.doc"));
HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(fs);
try {
ext.getText();
// Check each bit in turn
String sinfText = ext.getSummaryInformationText();
String dinfText = ext.getDocumentSummaryInformationText();
String summary = ext.getSummaryInformationText();
String docsummary = ext.getDocumentSummaryInformationText();
assertTrue(sinfText.indexOf("TEMPLATE = Normal") > -1);
assertTrue(sinfText.indexOf("SUBJECT = sample subject") > -1);
assertTrue(dinfText.indexOf("MANAGER = sample manager") > -1);
assertTrue(dinfText.indexOf("COMPANY = sample company") > -1);
assertContains(summary, "TEMPLATE = Normal");
assertContains(summary, "SUBJECT = sample subject");
assertContains(docsummary, "MANAGER = sample manager");
assertContains(docsummary, "COMPANY = sample company");
// Now overall
String text = ext.getText();
assertTrue(text.indexOf("TEMPLATE = Normal") > -1);
assertTrue(text.indexOf("SUBJECT = sample subject") > -1);
assertTrue(text.indexOf("MANAGER = sample manager") > -1);
assertTrue(text.indexOf("COMPANY = sample company") > -1);
assertContains(text, "TEMPLATE = Normal");
assertContains(text, "SUBJECT = sample subject");
assertContains(text, "MANAGER = sample manager");
assertContains(text, "COMPANY = sample company");
} finally {
ext.close();
}
@ -61,23 +61,21 @@ public final class TestHPSFPropertiesExtractor extends TestCase {
POIFSFileSystem fs = new POIFSFileSystem(_samples.openResourceAsStream("TestUnicode.xls"));
HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(fs);
try {
ext.getText();
// Check each bit in turn
String sinfText = ext.getSummaryInformationText();
String dinfText = ext.getDocumentSummaryInformationText();
String summary = ext.getSummaryInformationText();
String docsummary = ext.getDocumentSummaryInformationText();
assertTrue(sinfText.indexOf("AUTHOR = marshall") > -1);
assertTrue(sinfText.indexOf("TITLE = Titel: \u00c4h") > -1);
assertTrue(dinfText.indexOf("COMPANY = Schreiner") > -1);
assertTrue(dinfText.indexOf("SCALE = false") > -1);
assertContains(summary, "AUTHOR = marshall");
assertContains(summary, "TITLE = Titel: \u00c4h");
assertContains(docsummary, "COMPANY = Schreiner");
assertContains(docsummary, "SCALE = false");
// Now overall
String text = ext.getText();
assertTrue(text.indexOf("AUTHOR = marshall") > -1);
assertTrue(text.indexOf("TITLE = Titel: \u00c4h") > -1);
assertTrue(text.indexOf("COMPANY = Schreiner") > -1);
assertTrue(text.indexOf("SCALE = false") > -1);
assertContains(text, "AUTHOR = marshall");
assertContains(text, "TITLE = Titel: \u00c4h");
assertContains(text, "COMPANY = Schreiner");
assertContains(text, "SCALE = false");
} finally {
ext.close();
}
@ -91,12 +89,12 @@ public final class TestHPSFPropertiesExtractor extends TestCase {
try {
// Custom properties are part of the document info stream
String dinfText = ext.getDocumentSummaryInformationText();
assertTrue(dinfText.indexOf("Client = sample client") > -1);
assertTrue(dinfText.indexOf("Division = sample division") > -1);
assertContains(dinfText, "Client = sample client");
assertContains(dinfText, "Division = sample division");
String text = ext.getText();
assertTrue(text.indexOf("Client = sample client") > -1);
assertTrue(text.indexOf("Division = sample division") > -1);
assertContains(text, "Client = sample client");
assertContains(text, "Division = sample division");
} finally {
ext.close();
}
@ -138,26 +136,24 @@ public final class TestHPSFPropertiesExtractor extends TestCase {
eeText = eeExt.getText();
} finally {
eeExt.close();
wb.close();
}
assertEquals(fsText, hwText);
assertEquals(fsText, eeText);
assertTrue(fsText.indexOf("AUTHOR = marshall") > -1);
assertTrue(fsText.indexOf("TITLE = Titel: \u00c4h") > -1);
// Finally tidy
wb.close();
assertContains(fsText, "AUTHOR = marshall");
assertContains(fsText, "TITLE = Titel: \u00c4h");
}
public void test42726() throws IOException {
HPSFPropertiesExtractor ext = new HPSFPropertiesExtractor(HSSFTestDataSamples.openSampleWorkbook("42726.xls"));
try {
String txt = ext.getText();
assertTrue(txt.indexOf("PID_AUTHOR") != -1);
assertTrue(txt.indexOf("PID_EDITTIME") != -1);
assertTrue(txt.indexOf("PID_REVNUMBER") != -1);
assertTrue(txt.indexOf("PID_THUMBNAIL") != -1);
assertContains(txt, "PID_AUTHOR");
assertContains(txt, "PID_EDITTIME");
assertContains(txt, "PID_REVNUMBER");
assertContains(txt, "PID_THUMBNAIL");
} finally {
ext.close();
}