FileDocCategorySizeDatePackage
TestTextRunReWrite.javaAPI DocApache Poi 3.0.17035Sun Mar 11 12:59:30 GMT 2007org.apache.poi.hslf.model

TestTextRunReWrite

public class TestTextRunReWrite extends TestCase
Tests that if we load something up, get a TextRun, set the text to be the same as it was before, and write it all back out again, that we don't break anything in the process.
author
Nick Burch (nick at torchbox dot com)

Fields Summary
private HSLFSlideShow
hss
private SlideShow
ss
private POIFSFileSystem
pfs
Constructors Summary
Methods Summary
public voidsetUp()
Load up a test PPT file with rich data

		String dirname = System.getProperty("HSLF.testdata.path");
		String filename = dirname + "/Single_Coloured_Page_With_Fonts_and_Alignments.ppt";
		FileInputStream fis = new FileInputStream(filename);
		pfs = new POIFSFileSystem(fis);
		hss = new HSLFSlideShow(pfs);
		ss = new SlideShow(hss);
    
public voidtestWritesOutTheSameNonRich()

    	// Grab the first text run on the first sheet
    	TextRun tr1 = ss.getSlides()[0].getTextRuns()[0];
    	TextRun tr2 = ss.getSlides()[0].getTextRuns()[1];
    	
    	// Ensure the text lengths are as we'd expect to start with
    	assertEquals(1, ss.getSlides().length);
    	assertEquals(2, ss.getSlides()[0].getTextRuns().length);
    	assertEquals(30, tr1.getText().length());
    	assertEquals(179, tr2.getText().length());
    	
    	assertEquals(1, tr1.getRichTextRuns().length);
    	assertEquals(30, tr1.getRichTextRuns()[0].getLength());
    	assertEquals(30, tr1.getRichTextRuns()[0].getText().length());
    	assertEquals(31, tr1.getRichTextRuns()[0]._getRawCharacterStyle().getCharactersCovered());
    	assertEquals(31, tr1.getRichTextRuns()[0]._getRawParagraphStyle().getCharactersCovered());
    	
    	// Set the text to be as it is now
    	tr1.setText( tr1.getText() );
    	
    	// Check the text lengths are still right
    	assertEquals(30, tr1.getText().length());
    	assertEquals(179, tr2.getText().length());
    	
    	assertEquals(1, tr1.getRichTextRuns().length);
    	assertEquals(30, tr1.getRichTextRuns()[0].getLength());
    	assertEquals(30, tr1.getRichTextRuns()[0].getText().length());
    	assertEquals(31, tr1.getRichTextRuns()[0]._getRawCharacterStyle().getCharactersCovered());
    	assertEquals(31, tr1.getRichTextRuns()[0]._getRawParagraphStyle().getCharactersCovered());
    	
    	
		// Write the slideshow out to a byte array
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ss.write(baos);

		// Build an input stream of it
		ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

		// Use POIFS to query that lot
		POIFSFileSystem npfs = new POIFSFileSystem(bais);

		// Check that the "PowerPoint Document" sections have the same size
		DocumentEntry oProps = (DocumentEntry)pfs.getRoot().getEntry("PowerPoint Document");
		DocumentEntry nProps = (DocumentEntry)npfs.getRoot().getEntry("PowerPoint Document");
		assertEquals(oProps.getSize(),nProps.getSize());

		// Check that they contain the same data
		byte[] _oData = new byte[oProps.getSize()];
		byte[] _nData = new byte[nProps.getSize()];
		pfs.createDocumentInputStream("PowerPoint Document").read(_oData);
		npfs.createDocumentInputStream("PowerPoint Document").read(_nData);
		for(int i=0; i<_oData.length; i++) {
			System.out.println(i + "\t" + Integer.toHexString(i));
			assertEquals(_oData[i], _nData[i]);
		}
	
public voidtestWritesOutTheSameRich()

    	// Grab the first text run on the first sheet
    	TextRun tr1 = ss.getSlides()[0].getTextRuns()[0];
    	
    	// Get the first rich text run
    	RichTextRun rtr1 = tr1.getRichTextRuns()[0];
    	
    	
    	// Check that the text sizes are as expected
    	assertEquals(1, tr1.getRichTextRuns().length);
    	assertEquals(30, tr1.getText().length());
    	assertEquals(30, tr1.getRichTextRuns()[0].getText().length());
    	assertEquals(30, rtr1.getLength());
    	assertEquals(30, rtr1.getText().length());
    	assertEquals(31, rtr1._getRawCharacterStyle().getCharactersCovered());
    	assertEquals(31, rtr1._getRawParagraphStyle().getCharactersCovered());
    	
    	// Set the text to be as it is now
    	rtr1.setText( rtr1.getText() );
    	rtr1 = tr1.getRichTextRuns()[0];
    	
    	// Check that the text sizes are still as expected
    	assertEquals(1, tr1.getRichTextRuns().length);
    	assertEquals(30, tr1.getText().length());
    	assertEquals(30, tr1.getRichTextRuns()[0].getText().length());
    	assertEquals(30, rtr1.getLength());
    	assertEquals(30, rtr1.getText().length());
    	assertEquals(31, rtr1._getRawCharacterStyle().getCharactersCovered());
    	assertEquals(31, rtr1._getRawParagraphStyle().getCharactersCovered());
    	
    	
		// Write the slideshow out to a byte array
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ss.write(baos);

		// Build an input stream of it
		ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

		// Use POIFS to query that lot
		POIFSFileSystem npfs = new POIFSFileSystem(bais);

		// Check that the "PowerPoint Document" sections have the same size
		DocumentEntry oProps = (DocumentEntry)pfs.getRoot().getEntry("PowerPoint Document");
		DocumentEntry nProps = (DocumentEntry)npfs.getRoot().getEntry("PowerPoint Document");
		assertEquals(oProps.getSize(),nProps.getSize());

		// Check that they contain the same data
		byte[] _oData = new byte[oProps.getSize()];
		byte[] _nData = new byte[nProps.getSize()];
		pfs.createDocumentInputStream("PowerPoint Document").read(_oData);
		npfs.createDocumentInputStream("PowerPoint Document").read(_nData);
		for(int i=0; i<_oData.length; i++) {
			System.out.println(i + "\t" + Integer.toHexString(i) + "\t" + _oData[i]);
			assertEquals(_oData[i], _nData[i]);
		}