KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > storage > TestDocumentBlock


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.poifs.storage;
20
21 import java.io.*;
22
23 import java.util.*;
24
25 import junit.framework.*;
26
27 /**
28  * Class to test DocumentBlock functionality
29  *
30  * @author Marc Johnson
31  */

32
33 public class TestDocumentBlock
34     extends TestCase
35 {
36     static final private byte[] _testdata;
37
38     static
39     {
40         _testdata = new byte[ 2000 ];
41         for (int j = 0; j < _testdata.length; j++)
42         {
43             _testdata[ j ] = ( byte ) j;
44         }
45     }
46     ;
47
48     /**
49      * Constructor TestDocumentBlock
50      *
51      * @param name
52      */

53
54     public TestDocumentBlock(String JavaDoc name)
55     {
56         super(name);
57     }
58
59     /**
60      * Test the writing DocumentBlock constructor.
61      *
62      * @exception IOException
63      */

64
65     public void testConstructor()
66         throws IOException
67     {
68         ByteArrayInputStream input = new ByteArrayInputStream(_testdata);
69         int index = 0;
70         int size = 0;
71
72         while (true)
73         {
74             byte[] data = new byte[ Math.min(_testdata.length - index, 512) ];
75
76             System.arraycopy(_testdata, index, data, 0, data.length);
77             DocumentBlock block = new DocumentBlock(input);
78
79             verifyOutput(block, data);
80             size += block.size();
81             if (block.partiallyRead())
82             {
83                 break;
84             }
85             index += 512;
86         }
87         assertEquals(_testdata.length, size);
88     }
89
90     /**
91      * test static read method
92      *
93      * @exception IOException
94      */

95
96     public void testRead()
97         throws IOException
98     {
99         DocumentBlock[] blocks = new DocumentBlock[ 4 ];
100         ByteArrayInputStream input = new ByteArrayInputStream(_testdata);
101
102         for (int j = 0; j < 4; j++)
103         {
104             blocks[ j ] = new DocumentBlock(input);
105         }
106         for (int j = 1; j <= 2000; j += 17)
107         {
108             byte[] buffer = new byte[ j ];
109             int offset = 0;
110
111             for (int k = 0; k < (2000 / j); k++)
112             {
113                 DocumentBlock.read(blocks, buffer, offset);
114                 for (int n = 0; n < buffer.length; n++)
115                 {
116                     assertEquals("checking byte " + (k * j) + n,
117                                  _testdata[ (k * j) + n ], buffer[ n ]);
118                 }
119                 offset += j;
120             }
121         }
122     }
123
124     /**
125      * Test 'reading' constructor
126      *
127      * @exception IOException
128      */

129
130     public void testReadingConstructor()
131         throws IOException
132     {
133         RawDataBlock input =
134             new RawDataBlock(new ByteArrayInputStream(_testdata));
135
136         verifyOutput(new DocumentBlock(input), input.getData());
137     }
138
139     private void verifyOutput(DocumentBlock block, byte [] input)
140         throws IOException
141     {
142         assertEquals(input.length, block.size());
143         if (input.length < 512)
144         {
145             assertTrue(block.partiallyRead());
146         }
147         else
148         {
149             assertTrue(!block.partiallyRead());
150         }
151         ByteArrayOutputStream output = new ByteArrayOutputStream(512);
152
153         block.writeBlocks(output);
154         byte[] copy = output.toByteArray();
155         int j = 0;
156
157         for (; j < input.length; j++)
158         {
159             assertEquals(input[ j ], copy[ j ]);
160         }
161         for (; j < 512; j++)
162         {
163             assertEquals(( byte ) 0xFF, copy[ j ]);
164         }
165     }
166
167     /**
168      * main method to run the unit tests
169      *
170      * @param ignored_args
171      */

172
173     public static void main(String JavaDoc [] ignored_args)
174     {
175         System.out
176             .println("Testing org.apache.poi.poifs.storage.DocumentBlock");
177         junit.textui.TestRunner.run(TestDocumentBlock.class);
178     }
179 }
180
Popular Tags