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 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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.hslf.usermodel;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertTrue;
  19. import java.io.File;
  20. import java.io.InputStream;
  21. import java.util.ArrayList;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import junit.framework.AssertionFailedError;
  29. import org.apache.poi.POIDataSamples;
  30. import org.apache.poi.hslf.HSLFSlideShow;
  31. import org.apache.poi.hslf.HSLFTestDataSamples;
  32. import org.apache.poi.hslf.exceptions.OldPowerPointFormatException;
  33. import org.apache.poi.hslf.model.Background;
  34. import org.apache.poi.hslf.model.Fill;
  35. import org.apache.poi.hslf.model.HeadersFooters;
  36. import org.apache.poi.hslf.model.MasterSheet;
  37. import org.apache.poi.hslf.model.Notes;
  38. import org.apache.poi.hslf.model.Picture;
  39. import org.apache.poi.hslf.model.Shape;
  40. import org.apache.poi.hslf.model.ShapeGroup;
  41. import org.apache.poi.hslf.model.Slide;
  42. import org.apache.poi.hslf.model.SlideMaster;
  43. import org.apache.poi.hslf.model.TextBox;
  44. import org.apache.poi.hslf.model.TextRun;
  45. import org.apache.poi.hslf.model.TextShape;
  46. import org.apache.poi.hslf.model.TitleMaster;
  47. import org.junit.Test;
  48. /**
  49. * Testcases for bugs entered in bugzilla
  50. * the Test name contains the bugzilla bug id
  51. *
  52. * @author Yegor Kozlov
  53. */
  54. public final class TestBugs {
  55. private static POIDataSamples _slTests = POIDataSamples.getSlideShowInstance();
  56. /**
  57. * Bug 41384: Array index wrong in record creation
  58. */
  59. @Test
  60. public void bug41384() throws Exception {
  61. HSLFSlideShow hslf = new HSLFSlideShow(_slTests.openResourceAsStream("41384.ppt"));
  62. SlideShow ppt = new SlideShow(hslf);
  63. assertTrue("No Exceptions while reading file", true);
  64. assertEquals(1, ppt.getSlides().length);
  65. PictureData[] pict = ppt.getPictureData();
  66. assertEquals(2, pict.length);
  67. assertEquals(Picture.JPEG, pict[0].getType());
  68. assertEquals(Picture.JPEG, pict[1].getType());
  69. }
  70. /**
  71. * First fix from Bug 42474: NPE in RichTextRun.isBold()
  72. * when the RichTextRun comes from a Notes model object
  73. */
  74. @Test
  75. public void bug42474_1() throws Exception {
  76. HSLFSlideShow hslf = new HSLFSlideShow(_slTests.openResourceAsStream("42474-1.ppt"));
  77. SlideShow ppt = new SlideShow(hslf);
  78. assertTrue("No Exceptions while reading file", true);
  79. assertEquals(2, ppt.getSlides().length);
  80. TextRun txrun;
  81. Notes notes;
  82. notes = ppt.getSlides()[0].getNotesSheet();
  83. assertNotNull(notes);
  84. txrun = notes.getTextRuns()[0];
  85. assertEquals("Notes-1", txrun.getRawText());
  86. assertEquals(false, txrun.getRichTextRuns()[0].isBold());
  87. //notes for the second slide are in bold
  88. notes = ppt.getSlides()[1].getNotesSheet();
  89. assertNotNull(notes);
  90. txrun = notes.getTextRuns()[0];
  91. assertEquals("Notes-2", txrun.getRawText());
  92. assertEquals(true, txrun.getRichTextRuns()[0].isBold());
  93. }
  94. /**
  95. * Second fix from Bug 42474: Incorrect matching of notes to slides
  96. */
  97. @Test
  98. public void bug42474_2() throws Exception {
  99. HSLFSlideShow hslf = new HSLFSlideShow(_slTests.openResourceAsStream("42474-2.ppt"));
  100. SlideShow ppt = new SlideShow(hslf);
  101. //map slide number and starting phrase of its notes
  102. Map<Integer, String> notesMap = new HashMap<Integer, String>();
  103. notesMap.put(Integer.valueOf(4), "For decades before calculators");
  104. notesMap.put(Integer.valueOf(5), "Several commercial applications");
  105. notesMap.put(Integer.valueOf(6), "There are three variations of LNS that are discussed here");
  106. notesMap.put(Integer.valueOf(7), "Although multiply and square root are easier");
  107. notesMap.put(Integer.valueOf(8), "The bus Z is split into Z_H and Z_L");
  108. Slide[] slide = ppt.getSlides();
  109. for (int i = 0; i < slide.length; i++) {
  110. Integer slideNumber = Integer.valueOf(slide[i].getSlideNumber());
  111. Notes notes = slide[i].getNotesSheet();
  112. if (notesMap.containsKey(slideNumber)){
  113. assertNotNull(notes);
  114. String text = notes.getTextRuns()[0].getRawText();
  115. String startingPhrase = notesMap.get(slideNumber);
  116. assertTrue("Notes for slide " + slideNumber + " must start with " +
  117. startingPhrase , text.startsWith(startingPhrase));
  118. }
  119. }
  120. }
  121. /**
  122. * Bug 42485: All TextBoxes inside ShapeGroups have null TextRuns
  123. */
  124. @Test
  125. public void bug42485 () throws Exception {
  126. HSLFSlideShow hslf = new HSLFSlideShow(_slTests.openResourceAsStream("42485.ppt"));
  127. SlideShow ppt = new SlideShow(hslf);
  128. Shape[] shape = ppt.getSlides()[0].getShapes();
  129. for (int i = 0; i < shape.length; i++) {
  130. if(shape[i] instanceof ShapeGroup){
  131. ShapeGroup group = (ShapeGroup)shape[i];
  132. Shape[] sh = group.getShapes();
  133. for (int j = 0; j < sh.length; j++) {
  134. if( sh[j] instanceof TextBox){
  135. TextBox txt = (TextBox)sh[j];
  136. assertNotNull(txt.getTextRun());
  137. }
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. * Bug 42484: NullPointerException from ShapeGroup.getAnchor()
  144. */
  145. @Test
  146. public void bug42484 () throws Exception {
  147. HSLFSlideShow hslf = new HSLFSlideShow(_slTests.openResourceAsStream("42485.ppt"));
  148. SlideShow ppt = new SlideShow(hslf);
  149. Shape[] shape = ppt.getSlides()[0].getShapes();
  150. for (int i = 0; i < shape.length; i++) {
  151. if(shape[i] instanceof ShapeGroup){
  152. ShapeGroup group = (ShapeGroup)shape[i];
  153. assertNotNull(group.getAnchor());
  154. Shape[] sh = group.getShapes();
  155. for (int j = 0; j < sh.length; j++) {
  156. assertNotNull(sh[j].getAnchor());
  157. }
  158. }
  159. }
  160. assertTrue("No Exceptions while reading file", true);
  161. }
  162. /**
  163. * Bug 41381: Exception from Slide.getMasterSheet() on a seemingly valid PPT file
  164. */
  165. @Test
  166. public void bug41381() throws Exception {
  167. HSLFSlideShow hslf = new HSLFSlideShow(_slTests.openResourceAsStream("alterman_security.ppt"));
  168. SlideShow ppt = new SlideShow(hslf);
  169. assertTrue("No Exceptions while reading file", true);
  170. assertEquals(1, ppt.getSlidesMasters().length);
  171. assertEquals(1, ppt.getTitleMasters().length);
  172. Slide[] slide = ppt.getSlides();
  173. for (int i = 0; i < slide.length; i++) {
  174. MasterSheet master = slide[i].getMasterSheet();
  175. if (i == 0) assertTrue(master instanceof TitleMaster); //the first slide follows TitleMaster
  176. else assertTrue(master instanceof SlideMaster);
  177. }
  178. }
  179. /**
  180. * Bug 42486: Failure parsing a seemingly valid PPT
  181. */
  182. @Test
  183. public void bug42486 () throws Exception {
  184. HSLFSlideShow hslf = new HSLFSlideShow(_slTests.openResourceAsStream("42486.ppt"));
  185. SlideShow ppt = new SlideShow(hslf);
  186. Slide[] slide = ppt.getSlides();
  187. for (int i = 0; i < slide.length; i++) {
  188. @SuppressWarnings("unused")
  189. Shape[] shape = slide[i].getShapes();
  190. }
  191. assertTrue("No Exceptions while reading file", true);
  192. }
  193. /**
  194. * Bug 42524: NPE in Shape.getShapeType()
  195. */
  196. @Test
  197. public void bug42524 () throws Exception {
  198. HSLFSlideShow hslf = new HSLFSlideShow(_slTests.openResourceAsStream("42486.ppt"));
  199. SlideShow ppt = new SlideShow(hslf);
  200. //walk down the tree and see if there were no errors while reading
  201. Slide[] slide = ppt.getSlides();
  202. for (int i = 0; i < slide.length; i++) {
  203. Shape[] shape = slide[i].getShapes();
  204. for (int j = 0; j < shape.length; j++) {
  205. assertNotNull(shape[j].getShapeName());
  206. if (shape[j] instanceof ShapeGroup){
  207. ShapeGroup group = (ShapeGroup)shape[j];
  208. Shape[] comps = group.getShapes();
  209. for (int k = 0; k < comps.length; k++) {
  210. assertNotNull(comps[k].getShapeName());
  211. }
  212. }
  213. }
  214. }
  215. assertTrue("No Exceptions while reading file", true);
  216. }
  217. /**
  218. * Bug 42520: NPE in Picture.getPictureData()
  219. */
  220. @Test
  221. public void bug42520 () throws Exception {
  222. HSLFSlideShow hslf = new HSLFSlideShow(_slTests.openResourceAsStream("42520.ppt"));
  223. SlideShow ppt = new SlideShow(hslf);
  224. //test case from the bug report
  225. ShapeGroup shapeGroup = (ShapeGroup)ppt.getSlides()[11].getShapes()[10];
  226. Picture picture = (Picture)shapeGroup.getShapes()[0];
  227. picture.getPictureData();
  228. //walk down the tree and see if there were no errors while reading
  229. Slide[] slide = ppt.getSlides();
  230. for (int i = 0; i < slide.length; i++) {
  231. Shape[] shape = slide[i].getShapes();
  232. for (int j = 0; j < shape.length; j++) {
  233. if (shape[j] instanceof ShapeGroup){
  234. ShapeGroup group = (ShapeGroup)shape[j];
  235. Shape[] comps = group.getShapes();
  236. for (int k = 0; k < comps.length; k++) {
  237. Shape comp = comps[k];
  238. if (comp instanceof Picture){
  239. @SuppressWarnings("unused")
  240. PictureData pict = ((Picture)comp).getPictureData();
  241. }
  242. }
  243. }
  244. }
  245. }
  246. assertTrue("No Exceptions while reading file", true);
  247. }
  248. /**
  249. * Bug 38256: RuntimeException: Couldn't instantiate the class for type with id 0.
  250. * ( also fixed followup: getTextRuns() returns no text )
  251. */
  252. @Test
  253. public void bug38256 () throws Exception {
  254. SlideShow ppt = new SlideShow(_slTests.openResourceAsStream("38256.ppt"));
  255. assertTrue("No Exceptions while reading file", true);
  256. Slide[] slide = ppt.getSlides();
  257. assertEquals(1, slide.length);
  258. TextRun[] runs = slide[0].getTextRuns();
  259. assertEquals(4, runs.length);
  260. Set<String> txt = new HashSet<String>();
  261. txt.add("\u201CHAPPY BIRTHDAY SCOTT\u201D");
  262. txt.add("Have a HAPPY DAY");
  263. txt.add("PS Nobody is allowed to hassle Scott TODAY\u2026");
  264. txt.add("Drinks will be in the Boardroom at 5pm today to celebrate Scott\u2019s B\u2019Day\u2026 See you all there!");
  265. for (int i = 0; i < runs.length; i++) {
  266. String text = runs[i].getRawText();
  267. assertTrue(text, txt.contains(text));
  268. }
  269. }
  270. /**
  271. * Bug 38256: RuntimeException: Couldn't instantiate the class for type with id 0.
  272. * ( also fixed followup: getTextRuns() returns no text )
  273. */
  274. @Test
  275. public void bug43781 () throws Exception {
  276. SlideShow ppt = new SlideShow(_slTests.openResourceAsStream("43781.ppt"));
  277. assertTrue("No Exceptions while reading file", true);
  278. Slide slide = ppt.getSlides()[0];
  279. TextRun[] tr1 = slide.getTextRuns();
  280. List<TextRun> lst = new ArrayList<TextRun>();
  281. Shape[] shape = slide.getShapes();
  282. for (int i = 0; i < shape.length; i++) {
  283. if( shape[i] instanceof TextShape){
  284. TextRun textRun = ((TextShape)shape[i]).getTextRun();
  285. if(textRun != null) {
  286. lst.add(textRun);
  287. }
  288. }
  289. }
  290. TextRun[] tr2 = new TextRun[lst.size()];
  291. lst.toArray(tr2);
  292. assertEquals(tr1.length, tr2.length);
  293. for (int i = 0; i < tr1.length; i++) {
  294. assertEquals(tr1[i].getText(), tr2[i].getText());
  295. }
  296. }
  297. /**
  298. * Bug 44296: HSLF Not Extracting Slide Background Image
  299. */
  300. @Test
  301. public void bug44296 () throws Exception {
  302. SlideShow ppt = new SlideShow(_slTests.openResourceAsStream("44296.ppt"));
  303. Slide slide = ppt.getSlides()[0];
  304. Background b = slide.getBackground();
  305. Fill f = b.getFill();
  306. assertEquals(Fill.FILL_PICTURE, f.getFillType());
  307. PictureData pict = f.getPictureData();
  308. assertNotNull(pict);
  309. assertEquals(Picture.JPEG, pict.getType());
  310. }
  311. /**
  312. * Bug 44770: java.lang.RuntimeException: Couldn't instantiate the class for type with id 1036 on class class org.apache.poi.hslf.record.PPDrawing
  313. */
  314. @Test
  315. public void bug44770() throws Exception {
  316. try {
  317. new SlideShow(_slTests.openResourceAsStream("44770.ppt"));
  318. } catch (RuntimeException e) {
  319. if (e.getMessage().equals("Couldn't instantiate the class for type with id 1036 on class class org.apache.poi.hslf.record.PPDrawing")) {
  320. throw new AssertionFailedError("Identified bug 44770");
  321. }
  322. throw e;
  323. }
  324. }
  325. /**
  326. * Bug 41071: Will not extract text from Powerpoint TextBoxes
  327. */
  328. @Test
  329. public void bug41071() throws Exception {
  330. SlideShow ppt = new SlideShow(_slTests.openResourceAsStream("41071.ppt"));
  331. Slide slide = ppt.getSlides()[0];
  332. Shape[] sh = slide.getShapes();
  333. assertEquals(1, sh.length);
  334. assertTrue(sh[0] instanceof TextShape);
  335. TextShape tx = (TextShape)sh[0];
  336. assertEquals("Fundera, planera och involvera.", tx.getTextRun().getText());
  337. TextRun[] run = slide.getTextRuns();
  338. assertEquals(1, run.length);
  339. assertEquals("Fundera, planera och involvera.", run[0].getText());
  340. }
  341. /**
  342. * PowerPoint 95 files should throw a more helpful exception
  343. * @throws Exception
  344. */
  345. @Test(expected=OldPowerPointFormatException.class)
  346. public void bug41711() throws Exception {
  347. // New file is fine
  348. new SlideShow(_slTests.openResourceAsStream("SampleShow.ppt"));
  349. // PowerPoint 95 gives an old format exception
  350. new SlideShow(_slTests.openResourceAsStream("PPT95.ppt"));
  351. }
  352. /**
  353. * Changing text from Ascii to Unicode
  354. */
  355. @Test
  356. public void bug49648() throws Exception {
  357. SlideShow ppt = new SlideShow(_slTests.openResourceAsStream("49648.ppt"));
  358. for(Slide slide : ppt.getSlides()) {
  359. for(TextRun run : slide.getTextRuns()) {
  360. String text = run.getRawText();
  361. text.replace("{txtTot}", "With \u0123\u1234\u5678 unicode");
  362. run.setRawText(text);
  363. }
  364. }
  365. }
  366. /**
  367. * Bug 41246: AIOOB with illegal note references
  368. */
  369. @Test
  370. public void bug41246a() throws Exception {
  371. InputStream fis = _slTests.openResourceAsStream("41246-1.ppt");
  372. HSLFSlideShow hslf = new HSLFSlideShow(fis);
  373. fis.close();
  374. SlideShow ppt = new SlideShow(hslf);
  375. assertTrue("No Exceptions while reading file", true);
  376. ppt = HSLFTestDataSamples.writeOutAndReadBack(ppt);
  377. assertTrue("No Exceptions while rewriting file", true);
  378. }
  379. @Test
  380. public void bug41246b() throws Exception {
  381. InputStream fis = _slTests.openResourceAsStream("41246-2.ppt");
  382. HSLFSlideShow hslf = new HSLFSlideShow(fis);
  383. fis.close();
  384. SlideShow ppt = new SlideShow(hslf);
  385. assertTrue("No Exceptions while reading file", true);
  386. ppt = HSLFTestDataSamples.writeOutAndReadBack(ppt);
  387. assertTrue("No Exceptions while rewriting file", true);
  388. }
  389. /**
  390. * Bug 45776: Fix corrupt file problem using TextRun.setText
  391. */
  392. @Test
  393. public void bug45776() throws Exception {
  394. InputStream is = _slTests.openResourceAsStream("45776.ppt");
  395. SlideShow ppt = new SlideShow(new HSLFSlideShow(is));
  396. is.close();
  397. // get slides
  398. for (Slide slide : ppt.getSlides()) {
  399. for (Shape shape : slide.getShapes()) {
  400. if (!(shape instanceof TextBox)) continue;
  401. TextBox tb = (TextBox) shape;
  402. // work with TextBox
  403. String str = tb.getText();
  404. if (!str.contains("$$DATE$$")) continue;
  405. str = str.replace("$$DATE$$", new Date().toString());
  406. tb.setText(str);
  407. TextRun tr = tb.getTextRun();
  408. assertEquals(str.length()+1,tr.getStyleTextPropAtom().getParagraphStyles().getFirst().getCharactersCovered());
  409. assertEquals(str.length()+1,tr.getStyleTextPropAtom().getCharacterStyles().getFirst().getCharactersCovered());
  410. }
  411. }
  412. }
  413. @Test
  414. public void bug55732() throws Exception {
  415. File file = _slTests.getFile("bug55732.ppt");
  416. HSLFSlideShow ss = new HSLFSlideShow(file.getAbsolutePath());
  417. SlideShow _show = new SlideShow(ss);
  418. Slide[] _slides = _show.getSlides();
  419. /* Iterate over slides and extract text */
  420. for( Slide slide : _slides ) {
  421. HeadersFooters hf = slide.getHeadersFooters();
  422. boolean visible = hf.isHeaderVisible(); // exception happens here
  423. }
  424. assertTrue("No Exceptions while reading headers", true);
  425. }
  426. }