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

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