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;
019:
020: import junit.framework.TestCase;
021: import java.io.*;
022:
023: import org.apache.poi.hslf.usermodel.SlideShow;
024: import org.apache.poi.poifs.filesystem.*;
025:
026: /**
027: * Tests that HSLFSlideShow writes the powerpoint bit of data back out
028: * correctly. Currently, that means being the same as what it read in
029: *
030: * @author Nick Burch (nick at torchbox dot com)
031: */
032: public class TestReWrite extends TestCase {
033: // HSLFSlideShow primed on the test data
034: private HSLFSlideShow hssA;
035: private HSLFSlideShow hssB;
036: private HSLFSlideShow hssC;
037: // POIFS primed on the test data
038: private POIFSFileSystem pfsA;
039: private POIFSFileSystem pfsB;
040: private POIFSFileSystem pfsC;
041:
042: public void setUp() throws Exception {
043: String dirname = System.getProperty("HSLF.testdata.path");
044:
045: String filenameA = dirname + "/basic_test_ppt_file.ppt";
046: FileInputStream fisA = new FileInputStream(filenameA);
047: pfsA = new POIFSFileSystem(fisA);
048: hssA = new HSLFSlideShow(pfsA);
049:
050: String filenameB = dirname
051: + "/ParagraphStylesShorterThanCharStyles.ppt";
052: FileInputStream fisB = new FileInputStream(filenameB);
053: pfsB = new POIFSFileSystem(fisB);
054: hssB = new HSLFSlideShow(pfsB);
055:
056: String filenameC = dirname + "/WithMacros.ppt";
057: FileInputStream fisC = new FileInputStream(filenameC);
058: pfsC = new POIFSFileSystem(fisC);
059: hssC = new HSLFSlideShow(pfsC);
060: }
061:
062: public void testWritesOutTheSame() throws Exception {
063: assertWritesOutTheSame(hssA, pfsA);
064: assertWritesOutTheSame(hssB, pfsB);
065: }
066:
067: public void assertWritesOutTheSame(HSLFSlideShow hss,
068: POIFSFileSystem pfs) throws Exception {
069: // Write out to a byte array
070: ByteArrayOutputStream baos = new ByteArrayOutputStream();
071: hss.write(baos);
072:
073: // Build an input stream of it
074: ByteArrayInputStream bais = new ByteArrayInputStream(baos
075: .toByteArray());
076:
077: // Use POIFS to query that lot
078: POIFSFileSystem npfs = new POIFSFileSystem(bais);
079:
080: // Check that the "PowerPoint Document" sections have the same size
081: DocumentEntry oProps = (DocumentEntry) pfs.getRoot().getEntry(
082: "PowerPoint Document");
083: DocumentEntry nProps = (DocumentEntry) npfs.getRoot().getEntry(
084: "PowerPoint Document");
085: assertEquals(oProps.getSize(), nProps.getSize());
086:
087: // Check that they contain the same data
088: byte[] _oData = new byte[oProps.getSize()];
089: byte[] _nData = new byte[nProps.getSize()];
090: pfs.createDocumentInputStream("PowerPoint Document").read(
091: _oData);
092: npfs.createDocumentInputStream("PowerPoint Document").read(
093: _nData);
094: for (int i = 0; i < _oData.length; i++) {
095: //System.out.println(i + "\t" + Integer.toHexString(i));
096: assertEquals(_oData[i], _nData[i]);
097: }
098: }
099:
100: public void testWithMacroStreams() throws Exception {
101: // Check that they're apparently the same
102: assertSlideShowWritesOutTheSame(hssC, pfsC);
103:
104: // Currently has a Macros stream
105: assertNotNull(pfsC.getRoot().getEntry("Macros"));
106:
107: // Write out normally, will loose the macro stream
108: ByteArrayOutputStream baos = new ByteArrayOutputStream();
109: hssC.write(baos);
110: POIFSFileSystem pfsNew = new POIFSFileSystem(
111: new ByteArrayInputStream(baos.toByteArray()));
112:
113: try {
114: pfsNew.getRoot().getEntry("Macros");
115: fail();
116: } catch (FileNotFoundException e) {
117: // Good, as expected
118: }
119:
120: // But if we write out with nodes preserved, will be there
121: baos = new ByteArrayOutputStream();
122: hssC.write(baos, true);
123: pfsNew = new POIFSFileSystem(new ByteArrayInputStream(baos
124: .toByteArray()));
125: assertNotNull(pfsNew.getRoot().getEntry("Macros"));
126: }
127:
128: /**
129: * Ensure that simply opening a slideshow (usermodel) view of it
130: * doesn't change things
131: */
132: public void testSlideShowWritesOutTheSame() throws Exception {
133: assertSlideShowWritesOutTheSame(hssA, pfsA);
134:
135: // Some bug in StyleTextPropAtom rewriting means this will fail
136: // We need to identify and fix that first
137: //assertSlideShowWritesOutTheSame(hssB, pfsB);
138: }
139:
140: public void assertSlideShowWritesOutTheSame(HSLFSlideShow hss,
141: POIFSFileSystem pfs) throws Exception {
142: // Create a slideshow covering it
143: SlideShow ss = new SlideShow(hss);
144: ss.getSlides();
145: ss.getNotes();
146:
147: // Now write out to a byte array
148: ByteArrayOutputStream baos = new ByteArrayOutputStream();
149: hss.write(baos);
150:
151: // Build an input stream of it
152: ByteArrayInputStream bais = new ByteArrayInputStream(baos
153: .toByteArray());
154:
155: // Use POIFS to query that lot
156: POIFSFileSystem npfs = new POIFSFileSystem(bais);
157:
158: // Check that the "PowerPoint Document" sections have the same size
159: DocumentEntry oProps = (DocumentEntry) pfs.getRoot().getEntry(
160: "PowerPoint Document");
161: DocumentEntry nProps = (DocumentEntry) npfs.getRoot().getEntry(
162: "PowerPoint Document");
163: assertEquals(oProps.getSize(), nProps.getSize());
164:
165: // Check that they contain the same data
166: byte[] _oData = new byte[oProps.getSize()];
167: byte[] _nData = new byte[nProps.getSize()];
168: pfs.createDocumentInputStream("PowerPoint Document").read(
169: _oData);
170: npfs.createDocumentInputStream("PowerPoint Document").read(
171: _nData);
172: for (int i = 0; i < _oData.length; i++) {
173: if (_oData[i] != _nData[i])
174: System.out.println(i + "\t" + Integer.toHexString(i));
175: assertEquals(_oData[i], _nData[i]);
176: }
177: }
178: }
|