KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.poi.poifs.property.Property;
28
29 /**
30  * Class to test PropertyBlock functionality
31  *
32  * @author Marc Johnson
33  */

34
35 public class TestPropertyBlock
36     extends TestCase
37 {
38
39     /**
40      * Constructor TestPropertyBlock
41      *
42      * @param name
43      */

44
45     public TestPropertyBlock(String JavaDoc name)
46     {
47         super(name);
48     }
49
50     /**
51      * Test constructing PropertyBlocks
52      *
53      * @exception IOException
54      */

55
56     public void testCreatePropertyBlocks()
57         throws IOException
58     {
59
60         // test with 0 properties
61
List properties = new ArrayList();
62         BlockWritable[] blocks =
63             PropertyBlock.createPropertyBlockArray(properties);
64
65         assertEquals(0, blocks.length);
66
67         // test with 1 property
68
properties.add(new LocalProperty("Root Entry"));
69         blocks = PropertyBlock.createPropertyBlockArray(properties);
70         assertEquals(1, blocks.length);
71         byte[] testblock = new byte[ 512 ];
72
73         for (int j = 0; j < 4; j++)
74         {
75             setDefaultBlock(testblock, j);
76         }
77         testblock[ 0x0000 ] = ( byte ) 'R';
78         testblock[ 0x0002 ] = ( byte ) 'o';
79         testblock[ 0x0004 ] = ( byte ) 'o';
80         testblock[ 0x0006 ] = ( byte ) 't';
81         testblock[ 0x0008 ] = ( byte ) ' ';
82         testblock[ 0x000A ] = ( byte ) 'E';
83         testblock[ 0x000C ] = ( byte ) 'n';
84         testblock[ 0x000E ] = ( byte ) 't';
85         testblock[ 0x0010 ] = ( byte ) 'r';
86         testblock[ 0x0012 ] = ( byte ) 'y';
87         testblock[ 0x0040 ] = ( byte ) 22;
88         verifyCorrect(blocks, testblock);
89
90         // test with 3 properties
91
properties.add(new LocalProperty("workbook"));
92         properties.add(new LocalProperty("summary"));
93         blocks = PropertyBlock.createPropertyBlockArray(properties);
94         assertEquals(1, blocks.length);
95         testblock[ 0x0080 ] = ( byte ) 'w';
96         testblock[ 0x0082 ] = ( byte ) 'o';
97         testblock[ 0x0084 ] = ( byte ) 'r';
98         testblock[ 0x0086 ] = ( byte ) 'k';
99         testblock[ 0x0088 ] = ( byte ) 'b';
100         testblock[ 0x008A ] = ( byte ) 'o';
101         testblock[ 0x008C ] = ( byte ) 'o';
102         testblock[ 0x008E ] = ( byte ) 'k';
103         testblock[ 0x00C0 ] = ( byte ) 18;
104         testblock[ 0x0100 ] = ( byte ) 's';
105         testblock[ 0x0102 ] = ( byte ) 'u';
106         testblock[ 0x0104 ] = ( byte ) 'm';
107         testblock[ 0x0106 ] = ( byte ) 'm';
108         testblock[ 0x0108 ] = ( byte ) 'a';
109         testblock[ 0x010A ] = ( byte ) 'r';
110         testblock[ 0x010C ] = ( byte ) 'y';
111         testblock[ 0x0140 ] = ( byte ) 16;
112         verifyCorrect(blocks, testblock);
113
114         // test with 4 properties
115
properties.add(new LocalProperty("wintery"));
116         blocks = PropertyBlock.createPropertyBlockArray(properties);
117         assertEquals(1, blocks.length);
118         testblock[ 0x0180 ] = ( byte ) 'w';
119         testblock[ 0x0182 ] = ( byte ) 'i';
120         testblock[ 0x0184 ] = ( byte ) 'n';
121         testblock[ 0x0186 ] = ( byte ) 't';
122         testblock[ 0x0188 ] = ( byte ) 'e';
123         testblock[ 0x018A ] = ( byte ) 'r';
124         testblock[ 0x018C ] = ( byte ) 'y';
125         testblock[ 0x01C0 ] = ( byte ) 16;
126         verifyCorrect(blocks, testblock);
127
128         // test with 5 properties
129
properties.add(new LocalProperty("foo"));
130         blocks = PropertyBlock.createPropertyBlockArray(properties);
131         assertEquals(2, blocks.length);
132         testblock = new byte[ 1024 ];
133         for (int j = 0; j < 8; j++)
134         {
135             setDefaultBlock(testblock, j);
136         }
137         testblock[ 0x0000 ] = ( byte ) 'R';
138         testblock[ 0x0002 ] = ( byte ) 'o';
139         testblock[ 0x0004 ] = ( byte ) 'o';
140         testblock[ 0x0006 ] = ( byte ) 't';
141         testblock[ 0x0008 ] = ( byte ) ' ';
142         testblock[ 0x000A ] = ( byte ) 'E';
143         testblock[ 0x000C ] = ( byte ) 'n';
144         testblock[ 0x000E ] = ( byte ) 't';
145         testblock[ 0x0010 ] = ( byte ) 'r';
146         testblock[ 0x0012 ] = ( byte ) 'y';
147         testblock[ 0x0040 ] = ( byte ) 22;
148         testblock[ 0x0080 ] = ( byte ) 'w';
149         testblock[ 0x0082 ] = ( byte ) 'o';
150         testblock[ 0x0084 ] = ( byte ) 'r';
151         testblock[ 0x0086 ] = ( byte ) 'k';
152         testblock[ 0x0088 ] = ( byte ) 'b';
153         testblock[ 0x008A ] = ( byte ) 'o';
154         testblock[ 0x008C ] = ( byte ) 'o';
155         testblock[ 0x008E ] = ( byte ) 'k';
156         testblock[ 0x00C0 ] = ( byte ) 18;
157         testblock[ 0x0100 ] = ( byte ) 's';
158         testblock[ 0x0102 ] = ( byte ) 'u';
159         testblock[ 0x0104 ] = ( byte ) 'm';
160         testblock[ 0x0106 ] = ( byte ) 'm';
161         testblock[ 0x0108 ] = ( byte ) 'a';
162         testblock[ 0x010A ] = ( byte ) 'r';
163         testblock[ 0x010C ] = ( byte ) 'y';
164         testblock[ 0x0140 ] = ( byte ) 16;
165         testblock[ 0x0180 ] = ( byte ) 'w';
166         testblock[ 0x0182 ] = ( byte ) 'i';
167         testblock[ 0x0184 ] = ( byte ) 'n';
168         testblock[ 0x0186 ] = ( byte ) 't';
169         testblock[ 0x0188 ] = ( byte ) 'e';
170         testblock[ 0x018A ] = ( byte ) 'r';
171         testblock[ 0x018C ] = ( byte ) 'y';
172         testblock[ 0x01C0 ] = ( byte ) 16;
173         testblock[ 0x0200 ] = ( byte ) 'f';
174         testblock[ 0x0202 ] = ( byte ) 'o';
175         testblock[ 0x0204 ] = ( byte ) 'o';
176         testblock[ 0x0240 ] = ( byte ) 8;
177         verifyCorrect(blocks, testblock);
178     }
179
180     private void setDefaultBlock(byte [] testblock, int j)
181     {
182         int base = j * 128;
183         int index = 0;
184
185         for (; index < 0x40; index++)
186         {
187             testblock[ base++ ] = ( byte ) 0;
188         }
189         testblock[ base++ ] = ( byte ) 2;
190         testblock[ base++ ] = ( byte ) 0;
191         index += 2;
192         for (; index < 0x44; index++)
193         {
194             testblock[ base++ ] = ( byte ) 0;
195         }
196         for (; index < 0x50; index++)
197         {
198             testblock[ base++ ] = ( byte ) 0xff;
199         }
200         for (; index < 0x80; index++)
201         {
202             testblock[ base++ ] = ( byte ) 0;
203         }
204     }
205
206     private void verifyCorrect(BlockWritable [] blocks, byte [] testblock)
207         throws IOException
208     {
209         ByteArrayOutputStream stream = new ByteArrayOutputStream(512
210                                            * blocks.length);
211
212         for (int j = 0; j < blocks.length; j++)
213         {
214             blocks[ j ].writeBlocks(stream);
215         }
216         byte[] output = stream.toByteArray();
217
218         assertEquals(testblock.length, output.length);
219         for (int j = 0; j < testblock.length; j++)
220         {
221             assertEquals("mismatch at offset " + j, testblock[ j ],
222                          output[ j ]);
223         }
224     }
225
226     /**
227      * main method to run the unit tests
228      *
229      * @param ignored_args
230      */

231
232     public static void main(String JavaDoc [] ignored_args)
233     {
234         System.out
235             .println("Testing org.apache.poi.poifs.storage.PropertyBlock");
236         junit.textui.TestRunner.run(TestPropertyBlock.class);
237     }
238 }
239
Popular Tags