KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > property > TestRootProperty


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.property;
20
21 import java.io.*;
22
23 import java.util.*;
24
25 import junit.framework.*;
26
27 import org.apache.poi.poifs.common.POIFSConstants;
28
29 /**
30  * Class to test RootProperty functionality
31  *
32  * @author Marc Johnson
33  */

34
35 public class TestRootProperty
36     extends TestCase
37 {
38     private RootProperty _property;
39     private byte[] _testblock;
40
41     /**
42      * Constructor TestRootProperty
43      *
44      * @param name
45      */

46
47     public TestRootProperty(String JavaDoc name)
48     {
49         super(name);
50     }
51
52     /**
53      * Test constructing RootProperty
54      *
55      * @exception IOException
56      */

57
58     public void testConstructor()
59         throws IOException
60     {
61         createBasicRootProperty();
62         verifyProperty();
63     }
64
65     private void createBasicRootProperty()
66     {
67         _property = new RootProperty();
68         _testblock = new byte[ 128 ];
69         int index = 0;
70
71         for (; index < 0x40; index++)
72         {
73             _testblock[ index ] = ( byte ) 0;
74         }
75         String JavaDoc name = "Root Entry";
76         int limit = Math.min(31, name.length());
77
78         _testblock[ index++ ] = ( byte ) (2 * (limit + 1));
79         _testblock[ index++ ] = ( byte ) 0;
80         _testblock[ index++ ] = ( byte ) 5;
81         _testblock[ index++ ] = ( byte ) 1;
82         for (; index < 0x50; index++)
83         {
84             _testblock[ index ] = ( byte ) 0xff;
85         }
86         for (; index < 0x74; index++)
87         {
88             _testblock[ index ] = ( byte ) 0;
89         }
90         _testblock[ index++ ] = ( byte ) POIFSConstants.END_OF_CHAIN;
91         for (; index < 0x78; index++)
92         {
93             _testblock[ index ] = ( byte ) 0xff;
94         }
95         for (; index < 0x80; index++)
96         {
97             _testblock[ index ] = ( byte ) 0;
98         }
99         byte[] name_bytes = name.getBytes();
100
101         for (index = 0; index < limit; index++)
102         {
103             _testblock[ index * 2 ] = name_bytes[ index ];
104         }
105     }
106
107     private void verifyProperty()
108         throws IOException
109     {
110         ByteArrayOutputStream stream = new ByteArrayOutputStream(512);
111
112         _property.writeData(stream);
113         byte[] output = stream.toByteArray();
114
115         assertEquals(_testblock.length, output.length);
116         for (int j = 0; j < _testblock.length; j++)
117         {
118             assertEquals("mismatch at offset " + j, _testblock[ j ],
119                          output[ j ]);
120         }
121     }
122
123     /**
124      * test setSize
125      */

126
127     public void testSetSize()
128     {
129         for (int j = 0; j < 10; j++)
130         {
131             createBasicRootProperty();
132             _property.setSize(j);
133             assertEquals("trying block count of " + j, j * 64,
134                          _property.getSize());
135         }
136     }
137
138     /**
139      * Test reading constructor
140      *
141      * @exception IOException
142      */

143
144     public void testReadingConstructor()
145         throws IOException
146     {
147         byte[] input =
148         {
149             ( byte ) 0x52, ( byte ) 0x00, ( byte ) 0x6F, ( byte ) 0x00,
150             ( byte ) 0x6F, ( byte ) 0x00, ( byte ) 0x74, ( byte ) 0x00,
151             ( byte ) 0x20, ( byte ) 0x00, ( byte ) 0x45, ( byte ) 0x00,
152             ( byte ) 0x6E, ( byte ) 0x00, ( byte ) 0x74, ( byte ) 0x00,
153             ( byte ) 0x72, ( byte ) 0x00, ( byte ) 0x79, ( byte ) 0x00,
154             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
155             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
156             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
157             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
158             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
159             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
160             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
161             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
162             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
163             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
164             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
165             ( byte ) 0x16, ( byte ) 0x00, ( byte ) 0x05, ( byte ) 0x01,
166             ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF,
167             ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF,
168             ( byte ) 0x02, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
169             ( byte ) 0x20, ( byte ) 0x08, ( byte ) 0x02, ( byte ) 0x00,
170             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
171             ( byte ) 0xC0, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
172             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x46,
173             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
174             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
175             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
176             ( byte ) 0xC0, ( byte ) 0x5C, ( byte ) 0xE8, ( byte ) 0x23,
177             ( byte ) 0x9E, ( byte ) 0x6B, ( byte ) 0xC1, ( byte ) 0x01,
178             ( byte ) 0xFE, ( byte ) 0xFF, ( byte ) 0xFF, ( byte ) 0xFF,
179             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00,
180             ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00
181         };
182
183         verifyReadingProperty(0, input, 0, "Root Entry", "{00020820-0000-0000-C000-000000000046}");
184     }
185
186     private void verifyReadingProperty(int index, byte [] input, int offset,
187                                        String JavaDoc name, String JavaDoc sClsId)
188         throws IOException
189     {
190         RootProperty property = new RootProperty(index, input,
191                                              offset);
192         ByteArrayOutputStream stream = new ByteArrayOutputStream(128);
193         byte[] expected = new byte[ 128 ];
194
195         System.arraycopy(input, offset, expected, 0, 128);
196         property.writeData(stream);
197         byte[] output = stream.toByteArray();
198
199         assertEquals(128, output.length);
200         for (int j = 0; j < 128; j++)
201         {
202             assertEquals("mismatch at offset " + j, expected[ j ],
203                          output[ j ]);
204         }
205         assertEquals(index, property.getIndex());
206         assertEquals(name, property.getName());
207         assertTrue(!property.getChildren().hasNext());
208         assertEquals(property.getStorageClsid().toString(), sClsId);
209     }
210
211     /**
212      * main method to run the unit tests
213      *
214      * @param ignored_args
215      */

216
217     public static void main(String JavaDoc [] ignored_args)
218     {
219         System.out
220             .println("Testing org.apache.poi.poifs.property.RootProperty");
221         junit.textui.TestRunner.run(TestRootProperty.class);
222     }
223 }
224
Popular Tags