You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestBugs.java 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hwpf.usermodel;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertTrue;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.util.Arrays;
  25. import java.util.Collection;
  26. import java.util.List;
  27. import org.apache.commons.codec.digest.DigestUtils;
  28. import org.apache.poi.POIDataSamples;
  29. import org.apache.poi.hwpf.HWPFDocument;
  30. import org.apache.poi.hwpf.HWPFOldDocument;
  31. import org.apache.poi.hwpf.HWPFTestDataSamples;
  32. import org.apache.poi.hwpf.converter.AbstractWordUtils;
  33. import org.apache.poi.hwpf.converter.WordToTextConverter;
  34. import org.apache.poi.hwpf.extractor.Word6Extractor;
  35. import org.apache.poi.hwpf.extractor.WordExtractor;
  36. import org.apache.poi.hwpf.model.FieldsDocumentPart;
  37. import org.apache.poi.hwpf.model.FileInformationBlock;
  38. import org.apache.poi.hwpf.model.PlexOfField;
  39. import org.apache.poi.hwpf.model.SubdocumentType;
  40. import org.apache.poi.hwpf.model.io.HWPFOutputStream;
  41. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  42. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  43. import org.apache.poi.util.IOUtils;
  44. import org.apache.poi.util.POILogFactory;
  45. import org.apache.poi.util.POILogger;
  46. import org.junit.Test;
  47. import junit.framework.TestCase;
  48. /**
  49. * Test different problems reported in the Apache Bugzilla
  50. * against HWPF
  51. */
  52. public class TestBugs{
  53. private static final POILogger logger = POILogFactory.getLogger(TestBugs.class);
  54. public static void assertEqualsIgnoreNewline(String expected, String actual )
  55. {
  56. String newExpected = expected.replaceAll("\r\n", "\n" )
  57. .replaceAll("\r", "\n").trim();
  58. String newActual = actual.replaceAll("\r\n", "\n" )
  59. .replaceAll("\r", "\n").trim();
  60. TestCase.assertEquals(newExpected, newActual);
  61. }
  62. private static void assertTableStructures(Range expected, Range actual )
  63. {
  64. assertEquals(expected.numParagraphs(), actual.numParagraphs());
  65. for (int p = 0; p < expected.numParagraphs(); p++ )
  66. {
  67. Paragraph expParagraph = expected.getParagraph(p);
  68. Paragraph actParagraph = actual.getParagraph(p);
  69. assertEqualsIgnoreNewline(expParagraph.text(), actParagraph.text());
  70. assertEquals("Diffent isInTable flags for paragraphs #" + p
  71. + " -- " + expParagraph + " -- " + actParagraph + ".",
  72. expParagraph.isInTable(), actParagraph.isInTable());
  73. assertEquals(expParagraph.isTableRowEnd(),
  74. actParagraph.isTableRowEnd());
  75. if (expParagraph.isInTable() && actParagraph.isInTable() )
  76. {
  77. Table expTable, actTable;
  78. try
  79. {
  80. expTable = expected.getTable(expParagraph);
  81. actTable = actual.getTable(actParagraph);
  82. }
  83. catch (Exception exc )
  84. {
  85. continue;
  86. }
  87. assertEquals(expTable.numRows(), actTable.numRows());
  88. assertEquals(expTable.numParagraphs(),
  89. actTable.numParagraphs());
  90. }
  91. }
  92. }
  93. private String getText(String samplefile) throws IOException {
  94. HWPFDocument doc = HWPFTestDataSamples.openSampleFile(samplefile);
  95. WordExtractor extractor = new WordExtractor(doc);
  96. try {
  97. return extractor.getText();
  98. } finally {
  99. extractor.close();
  100. doc.close();
  101. }
  102. }
  103. private String getTextOldFile(String samplefile) throws IOException {
  104. HWPFOldDocument doc = HWPFTestDataSamples.openOldSampleFile(samplefile);
  105. Word6Extractor extractor = new Word6Extractor(doc);
  106. try {
  107. return extractor.getText();
  108. } finally {
  109. extractor.close();
  110. doc.close();
  111. }
  112. }
  113. /**
  114. * Bug 33519 - HWPF fails to read a file
  115. */
  116. @Test
  117. public void test33519() throws IOException
  118. {
  119. assertNotNull(getText("Bug33519.doc"));
  120. }
  121. /**
  122. * Bug 34898 - WordExtractor doesn't read the whole string from the file
  123. */
  124. @Test
  125. public void test34898() throws IOException
  126. {
  127. assertEqualsIgnoreNewline("\u30c7\u30a3\u30ec\u30af\u30c8\u30ea", getText("Bug34898.doc").trim());
  128. }
  129. /**
  130. * [RESOLVED INVALID] 41898 - Word 2003 pictures cannot be extracted
  131. */
  132. @Test
  133. public void test41898() throws IOException {
  134. HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug41898.doc");
  135. List<Picture> pics = doc.getPicturesTable().getAllPictures();
  136. assertNotNull(pics);
  137. assertEquals(1, pics.size());
  138. Picture pic = pics.get(0);
  139. assertNotNull(pic.suggestFileExtension());
  140. assertNotNull(pic.suggestFullFileName());
  141. assertNotNull(pic.getContent());
  142. assertNotNull(pic.getRawContent());
  143. /*
  144. * This is a file with empty EMF image, but present Office Drawing
  145. * --sergey
  146. */
  147. final Collection<OfficeDrawing> officeDrawings = doc
  148. .getOfficeDrawingsMain().getOfficeDrawings();
  149. assertNotNull(officeDrawings);
  150. assertEquals(1, officeDrawings.size());
  151. OfficeDrawing officeDrawing = officeDrawings.iterator().next();
  152. assertNotNull(officeDrawing);
  153. assertEquals(1044, officeDrawing.getShapeId());
  154. doc.close();
  155. }
  156. /**
  157. * Bug 44331 - HWPFDocument.write destroys fields
  158. */
  159. @SuppressWarnings("deprecation")
  160. @Test
  161. public void test44431() throws IOException
  162. {
  163. HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("Bug44431.doc");
  164. WordExtractor extractor1 = new WordExtractor(doc1);
  165. try {
  166. HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1);
  167. WordExtractor extractor2 = new WordExtractor(doc2);
  168. try {
  169. assertEqualsIgnoreNewline(extractor1.getFooterText(), extractor2.getFooterText());
  170. assertEqualsIgnoreNewline(extractor1.getHeaderText(), extractor2.getHeaderText());
  171. assertEqualsIgnoreNewline(Arrays.toString(extractor1.getParagraphText() ),
  172. Arrays.toString(extractor2.getParagraphText()));
  173. assertEqualsIgnoreNewline(extractor1.getText(), extractor2.getText());
  174. } finally {
  175. extractor2.close();
  176. }
  177. } finally {
  178. extractor1.close();
  179. doc1.close();
  180. }
  181. }
  182. /**
  183. * Bug 44331 - HWPFDocument.write destroys fields
  184. */
  185. @Test
  186. public void test44431_2() throws IOException
  187. {
  188. assertEqualsIgnoreNewline("File name=FieldsTest.doc\n" +
  189. "\n" +
  190. "\n" +
  191. "STYLEREF test\n" +
  192. "\n" +
  193. "\n" +
  194. "\n" +
  195. "TEST TABLE OF CONTENTS\n" +
  196. "\n" +
  197. "Heading paragraph in next page\t2\n" +
  198. "Another heading paragraph in further page\t3\n" +
  199. "Another heading paragraph in further page\t3\n" +
  200. "\n" +
  201. "\n" +
  202. "Heading paragraph in next page\n" +
  203. "Another heading paragraph in further page\n" +
  204. "\n" +
  205. "\n" +
  206. "\n" +
  207. "Page 3 of 3", getText("Bug44431.doc"));
  208. }
  209. /**
  210. * Bug 45473 - HWPF cannot read file after save
  211. */
  212. @Test
  213. public void test45473() throws IOException
  214. {
  215. // Fetch the current text
  216. HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("Bug45473.doc");
  217. WordExtractor wordExtractor = new WordExtractor(doc1);
  218. final String text1;
  219. try {
  220. text1 = wordExtractor.getText().trim();
  221. } finally {
  222. wordExtractor.close();
  223. doc1.close();
  224. }
  225. // Re-load, then re-save and re-check
  226. doc1 = HWPFTestDataSamples.openSampleFile("Bug45473.doc");
  227. HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1);
  228. WordExtractor wordExtractor2 = new WordExtractor(doc2);
  229. final String text2;
  230. try {
  231. text2 = wordExtractor2.getText().trim();
  232. } finally {
  233. wordExtractor2.close();
  234. doc1.close();
  235. }
  236. // the text in the saved document has some differences in line
  237. // separators but we tolerate that
  238. assertEqualsIgnoreNewline(text1.replaceAll("\n", "" ), text2.replaceAll("\n", ""));
  239. }
  240. /**
  241. * Bug 46220 - images are not properly extracted
  242. */
  243. @Test
  244. public void test46220() throws IOException {
  245. HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug46220.doc");
  246. // reference checksums as in Bugzilla
  247. String[] md5 = { "851be142bce6d01848e730cb6903f39e",
  248. "7fc6d8fb58b09ababd036d10a0e8c039",
  249. "a7dc644c40bc2fbf17b2b62d07f99248",
  250. "72d07b8db5fad7099d90bc4c304b4666" };
  251. List<Picture> pics = doc.getPicturesTable().getAllPictures();
  252. assertEquals(4, pics.size());
  253. for (int i = 0; i < pics.size(); i++ )
  254. {
  255. Picture pic = pics.get(i);
  256. byte[] data = pic.getRawContent();
  257. // use Apache Commons Codec utils to compute md5
  258. assertEqualsIgnoreNewline(md5[i], DigestUtils.md5Hex(data));
  259. }
  260. doc.close();
  261. }
  262. /**
  263. * [RESOLVED FIXED] Bug 46817 - Regression: Text from some table cells
  264. * missing
  265. */
  266. @Test
  267. public void test46817() throws IOException
  268. {
  269. String text = getText("Bug46817.doc").trim();
  270. assertTrue(text.contains("Nazwa wykonawcy"));
  271. assertTrue(text.contains("kujawsko-pomorskie"));
  272. assertTrue(text.contains("ekomel@ekomel.com.pl"));
  273. }
  274. /**
  275. * [FAILING] Bug 47286 - Word documents saves in wrong format if source
  276. * contains form elements
  277. */
  278. @SuppressWarnings("deprecation")
  279. @Test
  280. public void test47286() throws IOException
  281. {
  282. // Fetch the current text
  283. HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("Bug47286.doc");
  284. WordExtractor wordExtractor = new WordExtractor(doc1);
  285. final String text1;
  286. try {
  287. text1 = wordExtractor.getText().trim();
  288. } finally {
  289. wordExtractor.close();
  290. doc1.close();
  291. }
  292. // Re-load, then re-save and re-check
  293. doc1 = HWPFTestDataSamples.openSampleFile("Bug47286.doc");
  294. HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1);
  295. WordExtractor wordExtractor2 = new WordExtractor(doc2);
  296. final String text2;
  297. try {
  298. text2 = wordExtractor2.getText().trim();
  299. } finally {
  300. wordExtractor2.close();
  301. doc1.close();
  302. }
  303. // the text in the saved document has some differences in line
  304. // separators but we tolerate that
  305. assertEqualsIgnoreNewline(text1.replaceAll("\n", "" ), text2.replaceAll("\n", ""));
  306. assertEquals(doc1.getCharacterTable().getTextRuns().size(), doc2
  307. .getCharacterTable().getTextRuns().size());
  308. List<PlexOfField> expectedFields = doc1.getFieldsTables()
  309. .getFieldsPLCF(FieldsDocumentPart.MAIN);
  310. List<PlexOfField> actualFields = doc2.getFieldsTables().getFieldsPLCF(
  311. FieldsDocumentPart.MAIN);
  312. assertEquals(expectedFields.size(), actualFields.size());
  313. assertTableStructures(doc1.getRange(), doc2.getRange());
  314. }
  315. /**
  316. * [RESOLVED FIXED] Bug 47287 - StringIndexOutOfBoundsException in
  317. * CharacterRun.replaceText()
  318. */
  319. @Test
  320. public void test47287()
  321. {
  322. HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug47287.doc");
  323. String[] values = { "1-1", "1-2", "1-3", "1-4", "1-5", "1-6", "1-7",
  324. "1-8", "1-9", "1-10", "1-11", "1-12", "1-13", "1-14", "1-15", };
  325. int usedVal = 0;
  326. String PLACEHOLDER = "\u2002\u2002\u2002\u2002\u2002";
  327. Range r = doc.getRange();
  328. for (int x = 0; x < r.numSections(); x++ )
  329. {
  330. Section s = r.getSection(x);
  331. for (int y = 0; y < s.numParagraphs(); y++ )
  332. {
  333. Paragraph p = s.getParagraph(y);
  334. for (int z = 0; z < p.numCharacterRuns(); z++ )
  335. {
  336. boolean isFound = false;
  337. // character run
  338. CharacterRun run = p.getCharacterRun(z);
  339. // character run text
  340. String text = run.text();
  341. String oldText = text;
  342. int c = text.indexOf("FORMTEXT ");
  343. if (c < 0 )
  344. {
  345. int k = text.indexOf(PLACEHOLDER);
  346. if (k >= 0 )
  347. {
  348. text = text.substring(0, k ) + values[usedVal]
  349. + text.substring(k + PLACEHOLDER.length());
  350. usedVal++;
  351. isFound = true;
  352. }
  353. }
  354. else
  355. {
  356. for (; c >= 0; c = text.indexOf("FORMTEXT ", c
  357. + "FORMTEXT ".length() ) )
  358. {
  359. int k = text.indexOf(PLACEHOLDER, c);
  360. if (k >= 0 )
  361. {
  362. text = text.substring(0, k )
  363. + values[usedVal]
  364. + text.substring(k
  365. + PLACEHOLDER.length());
  366. usedVal++;
  367. isFound = true;
  368. }
  369. }
  370. }
  371. if (isFound )
  372. {
  373. run.replaceText(oldText, text, 0);
  374. }
  375. }
  376. }
  377. }
  378. String docText = r.text();
  379. assertTrue(docText.contains("1-1"));
  380. assertTrue(docText.contains("1-12"));
  381. assertFalse(docText.contains("1-13"));
  382. assertFalse(docText.contains("1-15"));
  383. }
  384. /**
  385. * [RESOLVED FIXED] Bug 47731 - Word Extractor considers text copied from
  386. * some website as an embedded object
  387. */
  388. @Test
  389. public void test47731() throws Exception
  390. {
  391. String foundText = getText("Bug47731.doc");
  392. assertTrue(foundText
  393. .contains("Soak the rice in water for three to four hours"));
  394. }
  395. /**
  396. * Bug 4774 - text extracted by WordExtractor is broken
  397. */
  398. @Test
  399. public void test47742() throws Exception
  400. {
  401. // (1) extract text from MS Word document via POI
  402. String foundText = getText("Bug47742.doc");
  403. // (2) read text from text document (retrieved by saving the word
  404. // document as text file using encoding UTF-8)
  405. InputStream is = POIDataSamples.getDocumentInstance()
  406. .openResourceAsStream("Bug47742-text.txt");
  407. try {
  408. byte[] expectedBytes = IOUtils.toByteArray(is);
  409. String expectedText = new String(expectedBytes, "utf-8" )
  410. .substring(1); // strip-off the unicode marker
  411. assertEqualsIgnoreNewline(expectedText, foundText);
  412. } finally {
  413. is.close();
  414. }
  415. }
  416. /**
  417. * Bug 47958 - Exception during Escher walk of pictures
  418. */
  419. @Test
  420. public void test47958()
  421. {
  422. HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug47958.doc");
  423. doc.getPicturesTable().getAllPictures();
  424. }
  425. /**
  426. * [RESOLVED FIXED] Bug 48065 - Problems with save output of HWPF (losing
  427. * formatting)
  428. */
  429. @Test
  430. public void test48065()
  431. {
  432. HWPFDocument doc1 = HWPFTestDataSamples.openSampleFile("Bug48065.doc");
  433. HWPFDocument doc2 = HWPFTestDataSamples.writeOutAndReadBack(doc1);
  434. Range expected = doc1.getRange();
  435. Range actual = doc2.getRange();
  436. assertEqualsIgnoreNewline(
  437. expected.text().replace("\r", "\n").replaceAll("\n\n", "\n" ),
  438. actual.text().replace("\r", "\n").replaceAll("\n\n", "\n"));
  439. assertTableStructures(expected, actual);
  440. }
  441. @Test
  442. public void test49933() throws IOException
  443. {
  444. String text = getTextOldFile("Bug49933.doc");
  445. assertTrue( text.contains( "best.wine.jump.ru" ) );
  446. }
  447. /**
  448. * Bug 50936 - Exception parsing MS Word 8.0 file
  449. */
  450. @Test
  451. public void test50936_1()
  452. {
  453. HWPFDocument hwpfDocument = HWPFTestDataSamples
  454. .openSampleFile("Bug50936_1.doc");
  455. hwpfDocument.getPicturesTable().getAllPictures();
  456. }
  457. /**
  458. * Bug 50936 - Exception parsing MS Word 8.0 file
  459. */
  460. @Test
  461. public void test50936_2()
  462. {
  463. HWPFDocument hwpfDocument = HWPFTestDataSamples
  464. .openSampleFile("Bug50936_2.doc");
  465. hwpfDocument.getPicturesTable().getAllPictures();
  466. }
  467. /**
  468. * Bug 50936 - Exception parsing MS Word 8.0 file
  469. */
  470. @Test
  471. public void test50936_3()
  472. {
  473. HWPFDocument hwpfDocument = HWPFTestDataSamples
  474. .openSampleFile("Bug50936_3.doc");
  475. hwpfDocument.getPicturesTable().getAllPictures();
  476. }
  477. /**
  478. * [RESOLVED FIXED] Bug 51604 - replace text fails for doc (poi 3.8 beta
  479. * release from download site )
  480. */
  481. @Test
  482. public void test51604()
  483. {
  484. HWPFDocument document = HWPFTestDataSamples
  485. .openSampleFile("Bug51604.doc");
  486. Range range = document.getRange();
  487. int numParagraph = range.numParagraphs();
  488. int counter = 0;
  489. for (int i = 0; i < numParagraph; i++ )
  490. {
  491. Paragraph paragraph = range.getParagraph(i);
  492. int numCharRuns = paragraph.numCharacterRuns();
  493. for (int j = 0; j < numCharRuns; j++ )
  494. {
  495. CharacterRun charRun = paragraph.getCharacterRun(j);
  496. String text = charRun.text();
  497. charRun.replaceText(text, "+" + (++counter));
  498. }
  499. }
  500. document = HWPFTestDataSamples.writeOutAndReadBack(document);
  501. String text = document.getDocumentText();
  502. assertEqualsIgnoreNewline("+1+2+3+4+5+6+7+8+9+10+11+12", text);
  503. }
  504. /**
  505. * [RESOLVED FIXED] Bug 51604 - replace text fails for doc (poi 3.8 beta
  506. * release from download site )
  507. */
  508. @Test
  509. public void test51604p2() throws Exception
  510. {
  511. HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug51604.doc");
  512. Range range = doc.getRange();
  513. int numParagraph = range.numParagraphs();
  514. replaceText(range, numParagraph);
  515. doc = HWPFTestDataSamples.writeOutAndReadBack(doc);
  516. final FileInformationBlock fileInformationBlock = doc
  517. .getFileInformationBlock();
  518. int totalLength = 0;
  519. for (SubdocumentType type : SubdocumentType.values() )
  520. {
  521. final int partLength = fileInformationBlock
  522. .getSubdocumentTextStreamLength(type);
  523. assert (partLength >= 0);
  524. totalLength += partLength;
  525. }
  526. assertEquals(doc.getText().length(), totalLength);
  527. }
  528. private void replaceText(Range range, int numParagraph) {
  529. for (int i = 0; i < numParagraph; i++ )
  530. {
  531. Paragraph paragraph = range.getParagraph(i);
  532. int numCharRuns = paragraph.numCharacterRuns();
  533. for (int j = 0; j < numCharRuns; j++ )
  534. {
  535. CharacterRun charRun = paragraph.getCharacterRun(j);
  536. String text = charRun.text();
  537. if (text.contains("Header" ) ) {
  538. charRun.replaceText(text, "added");
  539. }
  540. }
  541. }
  542. }
  543. /**
  544. * [RESOLVED FIXED] Bug 51604 - replace text fails for doc (poi 3.8 beta
  545. * release from download site )
  546. */
  547. @Test
  548. public void test51604p3() throws Exception
  549. {
  550. HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug51604.doc");
  551. byte[] originalData = new byte[doc.getFileInformationBlock()
  552. .getLcbDop()];
  553. System.arraycopy(doc.getTableStream(), doc.getFileInformationBlock()
  554. .getFcDop(), originalData, 0, originalData.length);
  555. HWPFOutputStream outputStream = new HWPFOutputStream();
  556. doc.getDocProperties().writeTo(outputStream);
  557. final byte[] oldData = outputStream.toByteArray();
  558. assertEqualsIgnoreNewline(Arrays.toString(originalData ),
  559. Arrays.toString(oldData));
  560. Range range = doc.getRange();
  561. int numParagraph = range.numParagraphs();
  562. replaceText(range, numParagraph);
  563. doc = HWPFTestDataSamples.writeOutAndReadBack(doc);
  564. outputStream = new HWPFOutputStream();
  565. doc.getDocProperties().writeTo(outputStream);
  566. final byte[] newData = outputStream.toByteArray();
  567. assertEqualsIgnoreNewline(Arrays.toString(oldData ), Arrays.toString(newData));
  568. }
  569. /**
  570. * [RESOLVED FIXED] Bug 51671 - HWPFDocument.write based on NPOIFSFileSystem
  571. * throws a NullPointerException
  572. */
  573. @Test
  574. public void test51671() throws Exception
  575. {
  576. InputStream is = POIDataSamples.getDocumentInstance()
  577. .openResourceAsStream("empty.doc");
  578. NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(is);
  579. try {
  580. HWPFDocument hwpfDocument = new HWPFDocument(
  581. npoifsFileSystem.getRoot());
  582. hwpfDocument.write(new ByteArrayOutputStream());
  583. hwpfDocument.close();
  584. } finally {
  585. npoifsFileSystem.close();
  586. }
  587. }
  588. /**
  589. * Bug 51678 - Extracting text from Bug51524.zip is slow Bug 51524 -
  590. * PapBinTable constructor is slow
  591. */
  592. @Test
  593. public void test51678And51524() throws IOException
  594. {
  595. // YK: the test will run only if the poi.test.remote system property is
  596. // set.
  597. // TODO: refactor into something nicer!
  598. if (System.getProperty("poi.test.remote" ) != null )
  599. {
  600. String href = "http://domex.nps.edu/corp/files/govdocs1/007/007488.doc";
  601. HWPFDocument hwpfDocument = HWPFTestDataSamples
  602. .openRemoteFile(href);
  603. WordExtractor wordExtractor = new WordExtractor(hwpfDocument);
  604. try {
  605. wordExtractor.getText();
  606. } finally {
  607. wordExtractor.close();
  608. }
  609. }
  610. }
  611. /**
  612. * [FIXED] Bug 51902 - Picture.fillRawImageContent -
  613. * ArrayIndexOutOfBoundsException
  614. */
  615. @Test
  616. public void testBug51890()
  617. {
  618. HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug51890.doc");
  619. for (Picture picture : doc.getPicturesTable().getAllPictures() )
  620. {
  621. PictureType pictureType = picture.suggestPictureType();
  622. logger.log(POILogger.DEBUG,
  623. "Picture at offset " + picture.getStartOffset()
  624. + " has type " + pictureType);
  625. }
  626. }
  627. /**
  628. * [RESOLVED FIXED] Bug 51834 - Opening and Writing .doc file results in
  629. * corrupt document
  630. */
  631. @Test
  632. public void testBug51834() throws Exception
  633. {
  634. /*
  635. * we don't have Java test for this file - it should be checked using
  636. * Microsoft BFF Validator. But check read-write-read anyway. -- sergey
  637. */
  638. HWPFTestDataSamples.openSampleFile("Bug51834.doc");
  639. HWPFTestDataSamples.writeOutAndReadBack(HWPFTestDataSamples
  640. .openSampleFile("Bug51834.doc"));
  641. }
  642. /**
  643. * Bug 51944 - PAPFormattedDiskPage.getPAPX - IndexOutOfBounds
  644. */
  645. @Test
  646. public void testBug51944() throws Exception
  647. {
  648. HWPFOldDocument doc = HWPFTestDataSamples.openOldSampleFile("Bug51944.doc");
  649. assertNotNull(WordToTextConverter.getText(doc));
  650. }
  651. /**
  652. * Bug 52032 - [BUG] & [partial-PATCH] HWPF - ArrayIndexOutofBoundsException
  653. * with no stack trace (broken after revision 1178063)
  654. */
  655. @Test
  656. public void testBug52032_1() throws Exception
  657. {
  658. assertNotNull(getText("Bug52032_1.doc"));
  659. }
  660. /**
  661. * Bug 52032 - [BUG] & [partial-PATCH] HWPF - ArrayIndexOutofBoundsException
  662. * with no stack trace (broken after revision 1178063)
  663. */
  664. @Test
  665. public void testBug52032_2() throws Exception
  666. {
  667. assertNotNull(getText("Bug52032_2.doc"));
  668. }
  669. /**
  670. * Bug 52032 - [BUG] & [partial-PATCH] HWPF - ArrayIndexOutofBoundsException
  671. * with no stack trace (broken after revision 1178063)
  672. */
  673. @Test
  674. public void testBug52032_3() throws Exception
  675. {
  676. assertNotNull(getText("Bug52032_3.doc"));
  677. }
  678. /**
  679. * Bug 53380 - ArrayIndexOutOfBounds Exception parsing word 97 document
  680. */
  681. @Test
  682. public void testBug53380_1() throws Exception
  683. {
  684. assertNotNull(getText("Bug53380_1.doc"));
  685. }
  686. /**
  687. * Bug 53380 - ArrayIndexOutOfBounds Exception parsing word 97 document
  688. */
  689. @Test
  690. public void testBug53380_2() throws Exception
  691. {
  692. assertNotNull(getText("Bug53380_2.doc"));
  693. }
  694. /**
  695. * Bug 53380 - ArrayIndexOutOfBounds Exception parsing word 97 document
  696. */
  697. @Test
  698. public void testBug53380_3() throws Exception
  699. {
  700. assertNotNull(getText("Bug53380_3.doc"));
  701. }
  702. /**
  703. * Bug 53380 - ArrayIndexOutOfBounds Exception parsing word 97 document
  704. */
  705. @Test
  706. public void testBug53380_4() throws Exception
  707. {
  708. assertNotNull(getText("Bug53380_4.doc"));
  709. }
  710. /**
  711. * java.lang.UnsupportedOperationException: Non-extended character
  712. * Pascal strings are not supported right now
  713. *
  714. * Disabled pending a fix for the bug
  715. */
  716. @Test
  717. public void test56880() throws Exception {
  718. HWPFDocument doc =
  719. HWPFTestDataSamples.openSampleFile("56880.doc");
  720. assertEqualsIgnoreNewline("Check Request", doc.getRange().text());
  721. }
  722. // These are the values the are expected to be read when the file
  723. // is checked.
  724. private final int section1LeftMargin = 1440;
  725. private final int section1RightMargin = 1440;
  726. private final int section1TopMargin = 1440;
  727. private final int section1BottomMargin = 1440;
  728. private final int section1NumColumns = 1;
  729. private int section2LeftMargin = 1440;
  730. private int section2RightMargin = 1440;
  731. private int section2TopMargin = 1440;
  732. private int section2BottomMargin = 1440;
  733. private final int section2NumColumns = 3;
  734. @Test
  735. @SuppressWarnings("SuspiciousNameCombination")
  736. public void testHWPFSections() {
  737. HWPFDocument document = HWPFTestDataSamples.openSampleFile("Bug53453Section.doc");
  738. Range overallRange = document.getOverallRange();
  739. int numParas = overallRange.numParagraphs();
  740. for(int i = 0; i < numParas; i++) {
  741. Paragraph para = overallRange.getParagraph(i);
  742. int numSections = para.numSections();
  743. for(int j = 0; j < numSections; j++) {
  744. Section section = para.getSection(j);
  745. if(para.text().trim().equals("Section1")) {
  746. assertSection1Margin(section);
  747. }
  748. else if(para.text().trim().equals("Section2")) {
  749. assertSection2Margin(section);
  750. // Change the margin widths
  751. this.section2BottomMargin = (int)(1.5 * AbstractWordUtils.TWIPS_PER_INCH);
  752. this.section2TopMargin = (int)(1.75 * AbstractWordUtils.TWIPS_PER_INCH);
  753. this.section2LeftMargin = (int)(0.5 * AbstractWordUtils.TWIPS_PER_INCH);
  754. this.section2RightMargin = (int)(0.75 * AbstractWordUtils.TWIPS_PER_INCH);
  755. section.setMarginBottom(this.section2BottomMargin);
  756. section.setMarginLeft(this.section2LeftMargin);
  757. section.setMarginRight(this.section2RightMargin);
  758. section.setMarginTop(this.section2TopMargin);
  759. }
  760. }
  761. }
  762. // Save away and re-read the document to prove the chages are permanent
  763. document = HWPFTestDataSamples.writeOutAndReadBack(document);
  764. overallRange = document.getOverallRange();
  765. numParas = overallRange.numParagraphs();
  766. for(int i = 0; i < numParas; i++) {
  767. Paragraph para = overallRange.getParagraph(i);
  768. int numSections = para.numSections();
  769. for(int j = 0; j < numSections; j++) {
  770. Section section = para.getSection(j);
  771. if(para.text().trim().equals("Section1")) {
  772. // No changes to the margins in Section1
  773. assertSection1Margin(section);
  774. }
  775. else if(para.text().trim().equals("Section2")) {
  776. // The margins in Section2 have kept the new settings.
  777. assertSection2Margin(section);
  778. }
  779. }
  780. }
  781. }
  782. @SuppressWarnings("Duplicates")
  783. private void assertSection1Margin(Section section) {
  784. assertEquals(section1BottomMargin, section.getMarginBottom());
  785. assertEquals(section1LeftMargin, section.getMarginLeft());
  786. assertEquals(section1RightMargin, section.getMarginRight());
  787. assertEquals(section1TopMargin, section.getMarginTop());
  788. assertEquals(section1NumColumns, section.getNumColumns());
  789. }
  790. @SuppressWarnings("Duplicates")
  791. private void assertSection2Margin(Section section) {
  792. assertEquals(section2BottomMargin, section.getMarginBottom());
  793. assertEquals(section2LeftMargin, section.getMarginLeft());
  794. assertEquals(section2RightMargin, section.getMarginRight());
  795. assertEquals(section2TopMargin, section.getMarginTop());
  796. assertEquals(section2NumColumns, section.getNumColumns());
  797. }
  798. @Test
  799. public void testRegressionIn315beta2() {
  800. HWPFDocument hwpfDocument = HWPFTestDataSamples.openSampleFile("cap.stanford.edu_profiles_viewbiosketch_facultyid=4009&name=m_maciver.doc");
  801. assertNotNull(hwpfDocument);
  802. }
  803. @Test(expected=ArrayIndexOutOfBoundsException.class)
  804. public void test57603SevenRowTable() throws Exception {
  805. HWPFDocument hwpfDocument = HWPFTestDataSamples.openSampleFile("57603-seven_columns.doc");
  806. HWPFDocument hwpfDocument2 = HWPFTestDataSamples.writeOutAndReadBack(hwpfDocument);
  807. assertNotNull(hwpfDocument2);
  808. hwpfDocument2.close();
  809. hwpfDocument.close();
  810. }
  811. @Test(expected=ArrayIndexOutOfBoundsException.class)
  812. public void test57843() throws IOException {
  813. File f = POIDataSamples.getDocumentInstance().getFile("57843.doc");
  814. POIFSFileSystem fs = new POIFSFileSystem(f, true);
  815. try {
  816. HWPFOldDocument doc = new HWPFOldDocument(fs);
  817. assertNotNull(doc);
  818. doc.close();
  819. } finally {
  820. fs.close();
  821. }
  822. }
  823. @Test
  824. public void testCommonCrawlRegression() throws IOException {
  825. HWPFDocument document = HWPFTestDataSamples.openSampleFile("ca.kwsymphony.www_education_School_Concert_Seat_Booking_Form_2011-12.doc");
  826. document.close();
  827. }
  828. }