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 java.io.*;
022:
023: import org.apache.poi.hslf.HSLFSlideShow;
024: import org.apache.poi.hslf.usermodel.RichTextRun;
025: import org.apache.poi.hslf.usermodel.SlideShow;
026: import org.apache.poi.poifs.filesystem.*;
027:
028: /**
029: * Tests that if we load something up, get a TextRun, set the text
030: * to be the same as it was before, and write it all back out again,
031: * that we don't break anything in the process.
032: *
033: * @author Nick Burch (nick at torchbox dot com)
034: */
035: public class TestTextRunReWrite extends TestCase {
036: // HSLFSlideShow primed on the test data
037: private HSLFSlideShow hss;
038: // HSLFSlideShow primed on the test data
039: private SlideShow ss;
040: // POIFS primed on the test data
041: private POIFSFileSystem pfs;
042:
043: /**
044: * Load up a test PPT file with rich data
045: */
046: public void setUp() throws Exception {
047: String dirname = System.getProperty("HSLF.testdata.path");
048: String filename = dirname
049: + "/Single_Coloured_Page_With_Fonts_and_Alignments.ppt";
050: FileInputStream fis = new FileInputStream(filename);
051: pfs = new POIFSFileSystem(fis);
052: hss = new HSLFSlideShow(pfs);
053: ss = new SlideShow(hss);
054: }
055:
056: public void testWritesOutTheSameNonRich() throws Exception {
057: // Grab the first text run on the first sheet
058: TextRun tr1 = ss.getSlides()[0].getTextRuns()[0];
059: TextRun tr2 = ss.getSlides()[0].getTextRuns()[1];
060:
061: // Ensure the text lengths are as we'd expect to start with
062: assertEquals(1, ss.getSlides().length);
063: assertEquals(2, ss.getSlides()[0].getTextRuns().length);
064: assertEquals(30, tr1.getText().length());
065: assertEquals(179, tr2.getText().length());
066:
067: assertEquals(1, tr1.getRichTextRuns().length);
068: assertEquals(30, tr1.getRichTextRuns()[0].getLength());
069: assertEquals(30, tr1.getRichTextRuns()[0].getText().length());
070: assertEquals(31, tr1.getRichTextRuns()[0]
071: ._getRawCharacterStyle().getCharactersCovered());
072: assertEquals(31, tr1.getRichTextRuns()[0]
073: ._getRawParagraphStyle().getCharactersCovered());
074:
075: // Set the text to be as it is now
076: tr1.setText(tr1.getText());
077:
078: // Check the text lengths are still right
079: assertEquals(30, tr1.getText().length());
080: assertEquals(179, tr2.getText().length());
081:
082: assertEquals(1, tr1.getRichTextRuns().length);
083: assertEquals(30, tr1.getRichTextRuns()[0].getLength());
084: assertEquals(30, tr1.getRichTextRuns()[0].getText().length());
085: assertEquals(31, tr1.getRichTextRuns()[0]
086: ._getRawCharacterStyle().getCharactersCovered());
087: assertEquals(31, tr1.getRichTextRuns()[0]
088: ._getRawParagraphStyle().getCharactersCovered());
089:
090: // Write the slideshow out to a byte array
091: ByteArrayOutputStream baos = new ByteArrayOutputStream();
092: ss.write(baos);
093:
094: // Build an input stream of it
095: ByteArrayInputStream bais = new ByteArrayInputStream(baos
096: .toByteArray());
097:
098: // Use POIFS to query that lot
099: POIFSFileSystem npfs = new POIFSFileSystem(bais);
100:
101: // Check that the "PowerPoint Document" sections have the same size
102: DocumentEntry oProps = (DocumentEntry) pfs.getRoot().getEntry(
103: "PowerPoint Document");
104: DocumentEntry nProps = (DocumentEntry) npfs.getRoot().getEntry(
105: "PowerPoint Document");
106: assertEquals(oProps.getSize(), nProps.getSize());
107:
108: // Check that they contain the same data
109: byte[] _oData = new byte[oProps.getSize()];
110: byte[] _nData = new byte[nProps.getSize()];
111: pfs.createDocumentInputStream("PowerPoint Document").read(
112: _oData);
113: npfs.createDocumentInputStream("PowerPoint Document").read(
114: _nData);
115: for (int i = 0; i < _oData.length; i++) {
116: System.out.println(i + "\t" + Integer.toHexString(i));
117: assertEquals(_oData[i], _nData[i]);
118: }
119: }
120:
121: public void testWritesOutTheSameRich() throws Exception {
122: // Grab the first text run on the first sheet
123: TextRun tr1 = ss.getSlides()[0].getTextRuns()[0];
124:
125: // Get the first rich text run
126: RichTextRun rtr1 = tr1.getRichTextRuns()[0];
127:
128: // Check that the text sizes are as expected
129: assertEquals(1, tr1.getRichTextRuns().length);
130: assertEquals(30, tr1.getText().length());
131: assertEquals(30, tr1.getRichTextRuns()[0].getText().length());
132: assertEquals(30, rtr1.getLength());
133: assertEquals(30, rtr1.getText().length());
134: assertEquals(31, rtr1._getRawCharacterStyle()
135: .getCharactersCovered());
136: assertEquals(31, rtr1._getRawParagraphStyle()
137: .getCharactersCovered());
138:
139: // Set the text to be as it is now
140: rtr1.setText(rtr1.getText());
141: rtr1 = tr1.getRichTextRuns()[0];
142:
143: // Check that the text sizes are still as expected
144: assertEquals(1, tr1.getRichTextRuns().length);
145: assertEquals(30, tr1.getText().length());
146: assertEquals(30, tr1.getRichTextRuns()[0].getText().length());
147: assertEquals(30, rtr1.getLength());
148: assertEquals(30, rtr1.getText().length());
149: assertEquals(31, rtr1._getRawCharacterStyle()
150: .getCharactersCovered());
151: assertEquals(31, rtr1._getRawParagraphStyle()
152: .getCharactersCovered());
153:
154: // Write the slideshow out to a byte array
155: ByteArrayOutputStream baos = new ByteArrayOutputStream();
156: ss.write(baos);
157:
158: // Build an input stream of it
159: ByteArrayInputStream bais = new ByteArrayInputStream(baos
160: .toByteArray());
161:
162: // Use POIFS to query that lot
163: POIFSFileSystem npfs = new POIFSFileSystem(bais);
164:
165: // Check that the "PowerPoint Document" sections have the same size
166: DocumentEntry oProps = (DocumentEntry) pfs.getRoot().getEntry(
167: "PowerPoint Document");
168: DocumentEntry nProps = (DocumentEntry) npfs.getRoot().getEntry(
169: "PowerPoint Document");
170: assertEquals(oProps.getSize(), nProps.getSize());
171:
172: // Check that they contain the same data
173: byte[] _oData = new byte[oProps.getSize()];
174: byte[] _nData = new byte[nProps.getSize()];
175: pfs.createDocumentInputStream("PowerPoint Document").read(
176: _oData);
177: npfs.createDocumentInputStream("PowerPoint Document").read(
178: _nData);
179: for (int i = 0; i < _oData.length; i++) {
180: System.out.println(i + "\t" + Integer.toHexString(i) + "\t"
181: + _oData[i]);
182: assertEquals(_oData[i], _nData[i]);
183: }
184: }
185: }
|