KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hwpf > model > TestSectionTable


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hwpf.model;
20
21 import junit.framework.*;
22
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.util.ArrayList JavaDoc;
25
26 import org.apache.poi.hwpf.*;
27 import org.apache.poi.hwpf.model.io.*;
28
29 public class TestSectionTable
30   extends TestCase
31 {
32   private HWPFDocFixture _hWPFDocFixture;
33
34   public TestSectionTable(String JavaDoc name)
35   {
36     super(name);
37   }
38
39   public void testReadWrite()
40     throws Exception JavaDoc
41   {
42     FileInformationBlock fib = _hWPFDocFixture._fib;
43     byte[] mainStream = _hWPFDocFixture._mainStream;
44     byte[] tableStream = _hWPFDocFixture._tableStream;
45     int fcMin = fib.getFcMin();
46
47     ComplexFileTable cft = new ComplexFileTable(mainStream, tableStream, fib.getFcClx(), fcMin);
48     TextPieceTable tpt = cft.getTextPieceTable();
49
50     SectionTable sectionTable = new SectionTable(mainStream, tableStream,
51                                                  fib.getFcPlcfsed(),
52                                                  fib.getLcbPlcfsed(),
53                                                  fcMin, tpt.getTextPieces());
54     HWPFFileSystem fileSys = new HWPFFileSystem();
55
56     sectionTable.writeTo(fileSys, 0);
57     ByteArrayOutputStream JavaDoc tableOut = fileSys.getStream("1Table");
58     ByteArrayOutputStream JavaDoc mainOut = fileSys.getStream("WordDocument");
59
60     byte[] newTableStream = tableOut.toByteArray();
61     byte[] newMainStream = mainOut.toByteArray();
62
63     SectionTable newSectionTable = new SectionTable(newMainStream, newTableStream, 0, newTableStream.length, 0, tpt.getTextPieces());
64
65     ArrayList JavaDoc oldSections = sectionTable.getSections();
66     ArrayList JavaDoc newSections = newSectionTable.getSections();
67
68     assertEquals(oldSections.size(), newSections.size());
69
70     //test for proper char offset conversions
71
PlexOfCps oldSedPlex = new PlexOfCps(tableStream, fib.getFcPlcfsed(),
72                                                       fib.getLcbPlcfsed(), 12);
73     PlexOfCps newSedPlex = new PlexOfCps(newTableStream, 0,
74                                          newTableStream.length, 12);
75     assertEquals(oldSedPlex.length(), newSedPlex.length());
76
77     for (int x = 0; x < oldSedPlex.length(); x++)
78     {
79       assertEquals(oldSedPlex.getProperty(x).getStart(), newSedPlex.getProperty(x).getStart());
80       assertEquals(oldSedPlex.getProperty(x).getEnd(), newSedPlex.getProperty(x).getEnd());
81     }
82
83     int size = oldSections.size();
84     for (int x = 0; x < size; x++)
85     {
86       PropertyNode oldNode = (PropertyNode)oldSections.get(x);
87       PropertyNode newNode = (PropertyNode)newSections.get(x);
88       assertEquals(oldNode, newNode);
89     }
90   }
91
92   protected void setUp()
93     throws Exception JavaDoc
94   {
95     super.setUp();
96     /**@todo verify the constructors*/
97     _hWPFDocFixture = new HWPFDocFixture(this);
98
99     _hWPFDocFixture.setUp();
100   }
101
102   protected void tearDown()
103     throws Exception JavaDoc
104   {
105     _hWPFDocFixture.tearDown();
106
107     _hWPFDocFixture = null;
108     super.tearDown();
109   }
110
111 }
112
Popular Tags