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.hwpf.model;
019:
020: import java.io.*;
021: import java.util.*;
022: import junit.framework.*;
023:
024: import org.apache.poi.hwpf.*;
025: import org.apache.poi.hwpf.model.*;
026: import org.apache.poi.util.*;
027:
028: /**
029: * Unit test for {@link SavedByTable} and {@link SavedByEntry}.
030: *
031: * @author Daniel Noll
032: */
033: public class TestSavedByTable extends TestCase {
034: /** Data dir */
035: private File testFile = new File(new File(System
036: .getProperty("HWPF.testdata.path")), "saved-by-table.doc");
037:
038: /** The expected entries in the test document. */
039: private List expected = Arrays
040: .asList(new Object[] {
041: new SavedByEntry(
042: "cic22",
043: "C:\\DOCUME~1\\phamill\\LOCALS~1\\Temp\\AutoRecovery save of Iraq - security.asd"),
044: new SavedByEntry(
045: "cic22",
046: "C:\\DOCUME~1\\phamill\\LOCALS~1\\Temp\\AutoRecovery save of Iraq - security.asd"),
047: new SavedByEntry(
048: "cic22",
049: "C:\\DOCUME~1\\phamill\\LOCALS~1\\Temp\\AutoRecovery save of Iraq - security.asd"),
050: new SavedByEntry("JPratt",
051: "C:\\TEMP\\Iraq - security.doc"),
052: new SavedByEntry("JPratt",
053: "A:\\Iraq - security.doc"),
054: new SavedByEntry("ablackshaw",
055: "C:\\ABlackshaw\\Iraq - security.doc"),
056: new SavedByEntry("ablackshaw",
057: "C:\\ABlackshaw\\A;Iraq - security.doc"),
058: new SavedByEntry("ablackshaw",
059: "A:\\Iraq - security.doc"),
060: new SavedByEntry("MKhan",
061: "C:\\TEMP\\Iraq - security.doc"),
062: new SavedByEntry("MKhan",
063: "C:\\WINNT\\Profiles\\mkhan\\Desktop\\Iraq.doc") });
064:
065: /**
066: * Tests reading in the entries, comparing them against the expected entries.
067: * Then tests writing the document out and reading the entries yet again.
068: *
069: * @throws Exception if an unexpected error occurs.
070: */
071: public void testReadWrite() throws Exception {
072: // This document is widely available on the internet as "blair.doc".
073: // I tried stripping the content and saving the document but my version
074: // of Word (from Office XP) strips this table out.
075: InputStream stream = new BufferedInputStream(
076: new FileInputStream(testFile));
077: HWPFDocument doc;
078: try {
079: doc = new HWPFDocument(stream);
080: } finally {
081: stream.close();
082: }
083:
084: // Check what we just read.
085: assertEquals("List of saved-by entries was not as expected",
086: expected, doc.getSavedByTable().getEntries());
087:
088: // Now write the entire document out, and read it back in...
089: ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
090: doc.write(byteStream);
091: InputStream copyStream = new ByteArrayInputStream(byteStream
092: .toByteArray());
093: HWPFDocument copy = new HWPFDocument(copyStream);
094:
095: // And check again.
096: assertEquals(
097: "List of saved-by entries was incorrect after writing",
098: expected, copy.getSavedByTable().getEntries());
099: }
100: }
|