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:
018: package org.apache.poi.hslf.model;
019:
020: import junit.framework.TestCase;
021: import org.apache.poi.hslf.HSLFSlideShow;
022: import org.apache.poi.hslf.usermodel.SlideShow;
023:
024: import java.io.ByteArrayOutputStream;
025: import java.io.ByteArrayInputStream;
026:
027: /**
028: * Test adding new slides to a ppt.
029: *
030: * Note - uses the same empty PPT file as the core "new Slideshow"
031: * stuff does
032: * @author Yegor Kozlov
033: */
034: public class TestSlides extends TestCase {
035:
036: /**
037: * Add 1 slide to an empty ppt.
038: * @throws Exception
039: */
040: public void testAddSlides1() throws Exception {
041: SlideShow ppt = new SlideShow(
042: new HSLFSlideShow(
043: TestSlides.class
044: .getResourceAsStream("/org/apache/poi/hslf/data/empty.ppt")));
045: assertTrue(ppt.getSlides().length == 0);
046:
047: Slide s1 = ppt.createSlide();
048: assertTrue(ppt.getSlides().length == 1);
049: assertEquals(3, s1._getSheetRefId());
050: assertEquals(256, s1._getSheetNumber());
051: assertEquals(1, s1.getSlideNumber());
052:
053: //serialize and read again
054: ByteArrayOutputStream out = new ByteArrayOutputStream();
055: ppt.write(out);
056: out.close();
057:
058: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
059: out.toByteArray())));
060: assertTrue(ppt.getSlides().length == 1);
061: }
062:
063: /**
064: * Add 2 slides to an empty ppt
065: * @throws Exception
066: */
067: public void testAddSlides2() throws Exception {
068: SlideShow ppt = new SlideShow(
069: new HSLFSlideShow(
070: TestSlides.class
071: .getResourceAsStream("/org/apache/poi/hslf/data/empty.ppt")));
072: assertTrue(ppt.getSlides().length == 0);
073:
074: Slide s1 = ppt.createSlide();
075: assertTrue(ppt.getSlides().length == 1);
076: assertEquals(3, s1._getSheetRefId());
077: assertEquals(256, s1._getSheetNumber());
078: assertEquals(1, s1.getSlideNumber());
079:
080: Slide s2 = ppt.createSlide();
081: assertTrue(ppt.getSlides().length == 2);
082: assertEquals(4, s2._getSheetRefId());
083: assertEquals(257, s2._getSheetNumber());
084: assertEquals(2, s2.getSlideNumber());
085:
086: //serialize and read again
087: ByteArrayOutputStream out = new ByteArrayOutputStream();
088: ppt.write(out);
089: out.close();
090:
091: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
092: out.toByteArray())));
093: assertTrue(ppt.getSlides().length == 2);
094: }
095:
096: /**
097: * Add 3 slides to an empty ppt
098: * @throws Exception
099: */
100: public void testAddSlides3() throws Exception {
101: SlideShow ppt = new SlideShow(
102: new HSLFSlideShow(
103: TestSlides.class
104: .getResourceAsStream("/org/apache/poi/hslf/data/empty.ppt")));
105: assertTrue(ppt.getSlides().length == 0);
106:
107: Slide s1 = ppt.createSlide();
108: assertTrue(ppt.getSlides().length == 1);
109: assertEquals(3, s1._getSheetRefId());
110: assertEquals(256, s1._getSheetNumber());
111: assertEquals(1, s1.getSlideNumber());
112:
113: Slide s2 = ppt.createSlide();
114: assertTrue(ppt.getSlides().length == 2);
115: assertEquals(4, s2._getSheetRefId());
116: assertEquals(257, s2._getSheetNumber());
117: assertEquals(2, s2.getSlideNumber());
118:
119: Slide s3 = ppt.createSlide();
120: assertTrue(ppt.getSlides().length == 3);
121: assertEquals(5, s3._getSheetRefId());
122: assertEquals(258, s3._getSheetNumber());
123: assertEquals(3, s3.getSlideNumber());
124:
125: //serialize and read again
126: ByteArrayOutputStream out = new ByteArrayOutputStream();
127: ppt.write(out);
128: out.close();
129:
130: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
131: out.toByteArray())));
132: assertTrue(ppt.getSlides().length == 3);
133:
134: // Check IDs are still right
135: s1 = ppt.getSlides()[0];
136: assertEquals(256, s1._getSheetNumber());
137: assertEquals(3, s1._getSheetRefId());
138: s2 = ppt.getSlides()[1];
139: assertEquals(257, s2._getSheetNumber());
140: assertEquals(4, s2._getSheetRefId());
141: s3 = ppt.getSlides()[2];
142: ;
143: assertTrue(ppt.getSlides().length == 3);
144: assertEquals(258, s3._getSheetNumber());
145: assertEquals(5, s3._getSheetRefId());
146: }
147:
148: /**
149: * Add slides to ppt which already has two slides
150: */
151: public void testAddSlides2to3() throws Exception {
152: String dirname = System.getProperty("HSLF.testdata.path");
153: SlideShow ppt = new SlideShow(new HSLFSlideShow(dirname
154: + "/basic_test_ppt_file.ppt"));
155:
156: assertTrue(ppt.getSlides().length == 2);
157:
158: // First slide is 256 / 4
159: Slide s1 = ppt.getSlides()[0];
160: assertEquals(256, s1._getSheetNumber());
161: assertEquals(4, s1._getSheetRefId());
162:
163: // Last slide is 257 / 6
164: Slide s2 = ppt.getSlides()[1];
165: assertEquals(257, s2._getSheetNumber());
166: assertEquals(6, s2._getSheetRefId());
167:
168: // Add another slide, goes in at the end
169: Slide s3 = ppt.createSlide();
170: assertTrue(ppt.getSlides().length == 3);
171: assertEquals(258, s3._getSheetNumber());
172: assertEquals(8, s3._getSheetRefId());
173:
174: // Serialize and read again
175: ByteArrayOutputStream out = new ByteArrayOutputStream();
176: ppt.write(out);
177: out.close();
178:
179: ppt = new SlideShow(new HSLFSlideShow(new ByteArrayInputStream(
180: out.toByteArray())));
181: assertTrue(ppt.getSlides().length == 3);
182:
183: // Check IDs are still right
184: s1 = ppt.getSlides()[0];
185: assertEquals(256, s1._getSheetNumber());
186: assertEquals(4, s1._getSheetRefId());
187: s2 = ppt.getSlides()[1];
188: assertEquals(257, s2._getSheetNumber());
189: assertEquals(6, s2._getSheetRefId());
190: s3 = ppt.getSlides()[2];
191: ;
192: assertTrue(ppt.getSlides().length == 3);
193: assertEquals(258, s3._getSheetNumber());
194: assertEquals(8, s3._getSheetRefId());
195: }
196:
197: }
|