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;
019:
020: import junit.framework.TestCase;
021: import java.io.*;
022:
023: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
024: import org.apache.poi.poifs.filesystem.*;
025:
026: /**
027: * Tests that POIDocument correctly loads and saves the common
028: * (hspf) Document Properties.
029: *
030: * This is part 1 of 2 of the tests - it only does the POIDocuments
031: * which are part of the Main (not scratchpad)
032: *
033: * @author Nick Burch (nick at torchbox dot com)
034: */
035: public class TestPOIDocumentMain extends TestCase {
036: // The POI Documents to work on
037: private POIDocument doc;
038: private POIDocument doc2;
039: // POIFS primed on the test (two different hssf) data
040: private POIFSFileSystem pfs;
041: private POIFSFileSystem pfs2;
042:
043: /**
044: * Set things up, using a PowerPoint document and
045: * a Word Document for our testing
046: */
047: public void setUp() throws Exception {
048: String dirnameHSSF = System.getProperty("HSSF.testdata.path");
049: String filenameHSSF = dirnameHSSF + "/DateFormats.xls";
050: String filenameHSSF2 = dirnameHSSF + "/StringFormulas.xls";
051:
052: FileInputStream fisHSSF = new FileInputStream(filenameHSSF);
053: pfs = new POIFSFileSystem(fisHSSF);
054: doc = new HSSFWorkbook(pfs);
055:
056: FileInputStream fisHSSF2 = new FileInputStream(filenameHSSF2);
057: pfs2 = new POIFSFileSystem(fisHSSF2);
058: doc2 = new HSSFWorkbook(pfs2);
059: }
060:
061: public void testReadProperties() throws Exception {
062: // We should have both sets
063: assertNotNull(doc.getDocumentSummaryInformation());
064: assertNotNull(doc.getSummaryInformation());
065:
066: // Check they are as expected for the test doc
067: assertEquals("Administrator", doc.getSummaryInformation()
068: .getAuthor());
069: assertEquals(0, doc.getDocumentSummaryInformation()
070: .getByteCount());
071: }
072:
073: public void testReadProperties2() throws Exception {
074: // Check again on the word one
075: assertNotNull(doc2.getDocumentSummaryInformation());
076: assertNotNull(doc2.getSummaryInformation());
077:
078: assertEquals("Avik Sengupta", doc2.getSummaryInformation()
079: .getAuthor());
080: assertEquals(null, doc2.getSummaryInformation().getKeywords());
081: assertEquals(0, doc2.getDocumentSummaryInformation()
082: .getByteCount());
083: }
084:
085: public void testWriteProperties() throws Exception {
086: // Just check we can write them back out into a filesystem
087: POIFSFileSystem outFS = new POIFSFileSystem();
088: doc.writeProperties(outFS);
089:
090: // Should now hold them
091: assertNotNull(outFS
092: .createDocumentInputStream("\005SummaryInformation"));
093: assertNotNull(outFS
094: .createDocumentInputStream("\005DocumentSummaryInformation"));
095: }
096:
097: public void testWriteReadProperties() throws Exception {
098: ByteArrayOutputStream baos = new ByteArrayOutputStream();
099:
100: // Write them out
101: POIFSFileSystem outFS = new POIFSFileSystem();
102: doc.writeProperties(outFS);
103: outFS.writeFilesystem(baos);
104:
105: // Create a new version
106: ByteArrayInputStream bais = new ByteArrayInputStream(baos
107: .toByteArray());
108: POIFSFileSystem inFS = new POIFSFileSystem(bais);
109:
110: // Check they're still there
111: doc.filesystem = inFS;
112: doc.readProperties();
113:
114: // Delegate test
115: testReadProperties();
116: }
117: }
|