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 junit.framework.*;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.util.ArrayList;
024:
025: import org.apache.poi.hwpf.*;
026: import org.apache.poi.hwpf.model.io.*;
027:
028: public class TestSectionTable extends TestCase {
029: private HWPFDocFixture _hWPFDocFixture;
030:
031: public TestSectionTable(String name) {
032: super (name);
033: }
034:
035: public void testReadWrite() throws Exception {
036: FileInformationBlock fib = _hWPFDocFixture._fib;
037: byte[] mainStream = _hWPFDocFixture._mainStream;
038: byte[] tableStream = _hWPFDocFixture._tableStream;
039: int fcMin = fib.getFcMin();
040:
041: ComplexFileTable cft = new ComplexFileTable(mainStream,
042: tableStream, fib.getFcClx(), fcMin);
043: TextPieceTable tpt = cft.getTextPieceTable();
044:
045: SectionTable sectionTable = new SectionTable(mainStream,
046: tableStream, fib.getFcPlcfsed(), fib.getLcbPlcfsed(),
047: fcMin, tpt.getTextPieces());
048: HWPFFileSystem fileSys = new HWPFFileSystem();
049:
050: sectionTable.writeTo(fileSys, 0);
051: ByteArrayOutputStream tableOut = fileSys.getStream("1Table");
052: ByteArrayOutputStream mainOut = fileSys
053: .getStream("WordDocument");
054:
055: byte[] newTableStream = tableOut.toByteArray();
056: byte[] newMainStream = mainOut.toByteArray();
057:
058: SectionTable newSectionTable = new SectionTable(newMainStream,
059: newTableStream, 0, newTableStream.length, 0, tpt
060: .getTextPieces());
061:
062: ArrayList oldSections = sectionTable.getSections();
063: ArrayList newSections = newSectionTable.getSections();
064:
065: assertEquals(oldSections.size(), newSections.size());
066:
067: //test for proper char offset conversions
068: PlexOfCps oldSedPlex = new PlexOfCps(tableStream, fib
069: .getFcPlcfsed(), fib.getLcbPlcfsed(), 12);
070: PlexOfCps newSedPlex = new PlexOfCps(newTableStream, 0,
071: newTableStream.length, 12);
072: assertEquals(oldSedPlex.length(), newSedPlex.length());
073:
074: for (int x = 0; x < oldSedPlex.length(); x++) {
075: assertEquals(oldSedPlex.getProperty(x).getStart(),
076: newSedPlex.getProperty(x).getStart());
077: assertEquals(oldSedPlex.getProperty(x).getEnd(), newSedPlex
078: .getProperty(x).getEnd());
079: }
080:
081: int size = oldSections.size();
082: for (int x = 0; x < size; x++) {
083: PropertyNode oldNode = (PropertyNode) oldSections.get(x);
084: PropertyNode newNode = (PropertyNode) newSections.get(x);
085: assertEquals(oldNode, newNode);
086: }
087: }
088:
089: protected void setUp() throws Exception {
090: super .setUp();
091: /**@todo verify the constructors*/
092: _hWPFDocFixture = new HWPFDocFixture(this );
093:
094: _hWPFDocFixture.setUp();
095: }
096:
097: protected void tearDown() throws Exception {
098: _hWPFDocFixture.tearDown();
099:
100: _hWPFDocFixture = null;
101: super.tearDown();
102: }
103:
104: }
|