Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TestBugs.java 32KB

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