001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017: package org.apache.poi.hslf.usermodel;
018:
019: import junit.framework.TestCase;
020: import org.apache.poi.hslf.HSLFSlideShow;
021: import org.apache.poi.hslf.model.*;
022: import org.apache.poi.hslf.model.Shape;
023:
024: import java.io.*;
025: import java.util.HashSet;
026: import java.util.HashMap;
027: import java.util.ArrayList;
028: import java.awt.*;
029:
030: /**
031: * Testcases for bugs entered in bugzilla
032: * the Test name contains the bugzilla bug id
033: *
034: * @author Yegor Kozlov
035: */
036: public class TestBugs extends TestCase {
037: protected String cwd = System.getProperty("HSLF.testdata.path");
038:
039: /**
040: * Bug 41384: Array index wrong in record creation
041: */
042: public void test41384() throws Exception {
043: FileInputStream is = new FileInputStream(new File(cwd,
044: "41384.ppt"));
045: HSLFSlideShow hslf = new HSLFSlideShow(is);
046: is.close();
047:
048: SlideShow ppt = new SlideShow(hslf);
049: assertTrue("No Exceptions while reading file", true);
050:
051: assertEquals(1, ppt.getSlides().length);
052:
053: PictureData[] pict = ppt.getPictureData();
054: assertEquals(2, pict.length);
055: assertEquals(Picture.JPEG, pict[0].getType());
056: assertEquals(Picture.JPEG, pict[1].getType());
057: }
058:
059: /**
060: * First fix from Bug 42474: NPE in RichTextRun.isBold()
061: * when the RichTextRun comes from a Notes model object
062: */
063: public void test42474_1() throws Exception {
064: FileInputStream is = new FileInputStream(new File(cwd,
065: "42474-1.ppt"));
066: HSLFSlideShow hslf = new HSLFSlideShow(is);
067: is.close();
068:
069: SlideShow ppt = new SlideShow(hslf);
070: assertTrue("No Exceptions while reading file", true);
071: assertEquals(2, ppt.getSlides().length);
072:
073: TextRun txrun;
074: Notes notes;
075:
076: notes = ppt.getSlides()[0].getNotesSheet();
077: assertNotNull(notes);
078: txrun = notes.getTextRuns()[0];
079: assertEquals("Notes-1", txrun.getRawText());
080: assertEquals(false, txrun.getRichTextRuns()[0].isBold());
081:
082: //notes for the second slide are in bold
083: notes = ppt.getSlides()[1].getNotesSheet();
084: assertNotNull(notes);
085: txrun = notes.getTextRuns()[0];
086: assertEquals("Notes-2", txrun.getRawText());
087: assertEquals(true, txrun.getRichTextRuns()[0].isBold());
088:
089: }
090:
091: /**
092: * Second fix from Bug 42474: Incorrect matching of notes to slides
093: */
094: public void test42474_2() throws Exception {
095: FileInputStream is = new FileInputStream(new File(cwd,
096: "42474-2.ppt"));
097: HSLFSlideShow hslf = new HSLFSlideShow(is);
098: is.close();
099:
100: SlideShow ppt = new SlideShow(hslf);
101:
102: //map slide number and starting phrase of its notes
103: HashMap notesMap = new HashMap();
104: notesMap.put(new Integer(4), "For decades before calculators");
105: notesMap.put(new Integer(5), "Several commercial applications");
106: notesMap
107: .put(new Integer(6),
108: "There are three variations of LNS that are discussed here");
109: notesMap.put(new Integer(7),
110: "Although multiply and square root are easier");
111: notesMap.put(new Integer(8),
112: "The bus Z is split into Z_H and Z_L");
113:
114: Slide[] slide = ppt.getSlides();
115: for (int i = 0; i < slide.length; i++) {
116: Integer slideNumber = new Integer(slide[i].getSlideNumber());
117: Notes notes = slide[i].getNotesSheet();
118: if (notesMap.containsKey(slideNumber)) {
119: assertNotNull(notes);
120: String text = notes.getTextRuns()[0].getRawText();
121: String startingPhrase = (String) notesMap
122: .get(slideNumber);
123: assertTrue("Notes for slide " + slideNumber
124: + " must start with " + startingPhrase, text
125: .startsWith(startingPhrase));
126: }
127: }
128: }
129:
130: /**
131: * Bug 42485: All TextBoxes inside ShapeGroups have null TextRuns
132: */
133: public void test42485() throws Exception {
134: FileInputStream is = new FileInputStream(new File(cwd,
135: "42485.ppt"));
136: HSLFSlideShow hslf = new HSLFSlideShow(is);
137: is.close();
138:
139: SlideShow ppt = new SlideShow(hslf);
140: Shape[] shape = ppt.getSlides()[0].getShapes();
141: for (int i = 0; i < shape.length; i++) {
142: if (shape[i] instanceof ShapeGroup) {
143: ShapeGroup group = (ShapeGroup) shape[i];
144: Shape[] sh = group.getShapes();
145: for (int j = 0; j < sh.length; j++) {
146: if (sh[j] instanceof TextBox) {
147: TextBox txt = (TextBox) sh[j];
148: assertNotNull(txt.getTextRun());
149: }
150: }
151: }
152: }
153: }
154:
155: /**
156: * Bug 42484: NullPointerException from ShapeGroup.getAnchor()
157: */
158: public void test42484() throws Exception {
159: FileInputStream is = new FileInputStream(new File(cwd,
160: "42485.ppt")); //test file is the same as for bug 42485
161: HSLFSlideShow hslf = new HSLFSlideShow(is);
162: is.close();
163:
164: SlideShow ppt = new SlideShow(hslf);
165: Shape[] shape = ppt.getSlides()[0].getShapes();
166: for (int i = 0; i < shape.length; i++) {
167: if (shape[i] instanceof ShapeGroup) {
168: ShapeGroup group = (ShapeGroup) shape[i];
169: assertNotNull(group.getAnchor());
170: Shape[] sh = group.getShapes();
171: for (int j = 0; j < sh.length; j++) {
172: assertNotNull(sh[j].getAnchor());
173: }
174: }
175: }
176: assertTrue("No Exceptions while reading file", true);
177: }
178:
179: /**
180: * Bug 41381: Exception from Slide.getMasterSheet() on a seemingly valid PPT file
181: */
182: public void test41381() throws Exception {
183: FileInputStream is = new FileInputStream(new File(cwd,
184: "alterman_security.ppt"));
185: HSLFSlideShow hslf = new HSLFSlideShow(is);
186: is.close();
187:
188: SlideShow ppt = new SlideShow(hslf);
189: assertTrue("No Exceptions while reading file", true);
190:
191: assertEquals(1, ppt.getSlidesMasters().length);
192: assertEquals(1, ppt.getTitleMasters().length);
193: Slide[] slide = ppt.getSlides();
194: for (int i = 0; i < slide.length; i++) {
195: MasterSheet master = slide[i].getMasterSheet();
196: if (i == 0)
197: assertTrue(master instanceof TitleMaster); //the first slide follows TitleMaster
198: else
199: assertTrue(master instanceof SlideMaster);
200: }
201: }
202:
203: /**
204: * Bug 42486: Failure parsing a seemingly valid PPT
205: */
206: public void test42486() throws Exception {
207: FileInputStream is = new FileInputStream(new File(cwd,
208: "42486.ppt"));
209: HSLFSlideShow hslf = new HSLFSlideShow(is);
210: is.close();
211:
212: SlideShow ppt = new SlideShow(hslf);
213: Slide[] slide = ppt.getSlides();
214: for (int i = 0; i < slide.length; i++) {
215: Shape[] shape = slide[i].getShapes();
216: }
217: assertTrue("No Exceptions while reading file", true);
218:
219: }
220:
221: /**
222: * Bug 42524: NPE in Shape.getShapeType()
223: */
224: public void test42524() throws Exception {
225: FileInputStream is = new FileInputStream(new File(cwd,
226: "42486.ppt")); //test file is the same as for Bug 42486
227: HSLFSlideShow hslf = new HSLFSlideShow(is);
228: is.close();
229:
230: SlideShow ppt = new SlideShow(hslf);
231: //walk down the tree and see if there were no errors while reading
232: Slide[] slide = ppt.getSlides();
233: for (int i = 0; i < slide.length; i++) {
234: Shape[] shape = slide[i].getShapes();
235: for (int j = 0; j < shape.length; j++) {
236: assertNotNull(shape[j].getShapeName());
237: if (shape[j] instanceof ShapeGroup) {
238: ShapeGroup group = (ShapeGroup) shape[j];
239: Shape[] comps = group.getShapes();
240: for (int k = 0; k < comps.length; k++) {
241: assertNotNull(comps[k].getShapeName());
242: }
243: }
244: }
245:
246: }
247: assertTrue("No Exceptions while reading file", true);
248:
249: }
250:
251: /**
252: * Bug 42520: NPE in Picture.getPictureData()
253: */
254: public void test42520() throws Exception {
255: FileInputStream is = new FileInputStream(new File(cwd,
256: "42520.ppt")); //test file is the same as for Bug 42486
257: HSLFSlideShow hslf = new HSLFSlideShow(is);
258: is.close();
259:
260: SlideShow ppt = new SlideShow(hslf);
261:
262: //test case from the bug report
263: ShapeGroup shapeGroup = (ShapeGroup) ppt.getSlides()[11]
264: .getShapes()[10];
265: Picture picture = (Picture) shapeGroup.getShapes()[0];
266: picture.getPictureData();
267:
268: //walk down the tree and see if there were no errors while reading
269: Slide[] slide = ppt.getSlides();
270: for (int i = 0; i < slide.length; i++) {
271: Shape[] shape = slide[i].getShapes();
272: for (int j = 0; j < shape.length; j++) {
273: if (shape[j] instanceof ShapeGroup) {
274: ShapeGroup group = (ShapeGroup) shape[j];
275: Shape[] comps = group.getShapes();
276: for (int k = 0; k < comps.length; k++) {
277: Shape comp = comps[k];
278: if (comp instanceof Picture) {
279: PictureData pict = ((Picture) comp)
280: .getPictureData();
281: }
282: }
283: }
284: }
285:
286: }
287: assertTrue("No Exceptions while reading file", true);
288:
289: }
290:
291: /**
292: * Bug 38256: RuntimeException: Couldn't instantiate the class for type with id 0.
293: * ( also fixed followup: getTextRuns() returns no text )
294: */
295: public void test38256() throws Exception {
296: FileInputStream is = new FileInputStream(new File(cwd,
297: "38256.ppt"));
298: SlideShow ppt = new SlideShow(is);
299: is.close();
300:
301: assertTrue("No Exceptions while reading file", true);
302:
303: Slide[] slide = ppt.getSlides();
304: assertEquals(1, slide.length);
305: TextRun[] runs = slide[0].getTextRuns();
306: assertEquals(4, runs.length);
307:
308: HashSet txt = new HashSet();
309: txt.add("\u201CHAPPY BIRTHDAY SCOTT\u201D");
310: txt.add("Have a HAPPY DAY");
311: txt.add("PS Nobody is allowed to hassle Scott TODAY\u2026");
312: txt
313: .add("Drinks will be in the Boardroom at 5pm today to celebrate Scott\u2019s B\u2019Day\u2026 See you all there!");
314:
315: for (int i = 0; i < runs.length; i++) {
316: String text = runs[i].getRawText();
317: assertTrue(text, txt.contains(text));
318: }
319:
320: }
321:
322: /**
323: * Bug 38256: RuntimeException: Couldn't instantiate the class for type with id 0.
324: * ( also fixed followup: getTextRuns() returns no text )
325: */
326: public void test43781() throws Exception {
327: FileInputStream is = new FileInputStream(new File(cwd,
328: "43781.ppt"));
329: SlideShow ppt = new SlideShow(is);
330: is.close();
331:
332: assertTrue("No Exceptions while reading file", true);
333:
334: Slide slide = ppt.getSlides()[0];
335: TextRun[] tr1 = slide.getTextRuns();
336:
337: ArrayList lst = new ArrayList();
338: Shape[] shape = slide.getShapes();
339: for (int i = 0; i < shape.length; i++) {
340: if (shape[i] instanceof TextBox) {
341: TextRun textRun = ((TextBox) shape[i]).getTextRun();
342: if (textRun != null)
343: lst.add(textRun);
344: }
345:
346: }
347: TextRun[] tr2 = new TextRun[lst.size()];
348: lst.toArray(tr2);
349:
350: assertEquals(tr1.length, tr2.length);
351: for (int i = 0; i < tr1.length; i++) {
352: assertEquals(tr1[i].getText(), tr2[i].getText());
353: }
354: }
355: }
|