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.poifs.property;
019:
020: import java.io.*;
021:
022: import java.util.*;
023:
024: import junit.framework.*;
025:
026: import org.apache.poi.poifs.common.POIFSConstants;
027:
028: /**
029: * Class to test RootProperty functionality
030: *
031: * @author Marc Johnson
032: */
033:
034: public class TestRootProperty extends TestCase {
035: private RootProperty _property;
036: private byte[] _testblock;
037:
038: /**
039: * Constructor TestRootProperty
040: *
041: * @param name
042: */
043:
044: public TestRootProperty(String name) {
045: super (name);
046: }
047:
048: /**
049: * Test constructing RootProperty
050: *
051: * @exception IOException
052: */
053:
054: public void testConstructor() throws IOException {
055: createBasicRootProperty();
056: verifyProperty();
057: }
058:
059: private void createBasicRootProperty() {
060: _property = new RootProperty();
061: _testblock = new byte[128];
062: int index = 0;
063:
064: for (; index < 0x40; index++) {
065: _testblock[index] = (byte) 0;
066: }
067: String name = "Root Entry";
068: int limit = Math.min(31, name.length());
069:
070: _testblock[index++] = (byte) (2 * (limit + 1));
071: _testblock[index++] = (byte) 0;
072: _testblock[index++] = (byte) 5;
073: _testblock[index++] = (byte) 1;
074: for (; index < 0x50; index++) {
075: _testblock[index] = (byte) 0xff;
076: }
077: for (; index < 0x74; index++) {
078: _testblock[index] = (byte) 0;
079: }
080: _testblock[index++] = (byte) POIFSConstants.END_OF_CHAIN;
081: for (; index < 0x78; index++) {
082: _testblock[index] = (byte) 0xff;
083: }
084: for (; index < 0x80; index++) {
085: _testblock[index] = (byte) 0;
086: }
087: byte[] name_bytes = name.getBytes();
088:
089: for (index = 0; index < limit; index++) {
090: _testblock[index * 2] = name_bytes[index];
091: }
092: }
093:
094: private void verifyProperty() throws IOException {
095: ByteArrayOutputStream stream = new ByteArrayOutputStream(512);
096:
097: _property.writeData(stream);
098: byte[] output = stream.toByteArray();
099:
100: assertEquals(_testblock.length, output.length);
101: for (int j = 0; j < _testblock.length; j++) {
102: assertEquals("mismatch at offset " + j, _testblock[j],
103: output[j]);
104: }
105: }
106:
107: /**
108: * test setSize
109: */
110:
111: public void testSetSize() {
112: for (int j = 0; j < 10; j++) {
113: createBasicRootProperty();
114: _property.setSize(j);
115: assertEquals("trying block count of " + j, j * 64,
116: _property.getSize());
117: }
118: }
119:
120: /**
121: * Test reading constructor
122: *
123: * @exception IOException
124: */
125:
126: public void testReadingConstructor() throws IOException {
127: byte[] input = { (byte) 0x52, (byte) 0x00, (byte) 0x6F,
128: (byte) 0x00, (byte) 0x6F, (byte) 0x00, (byte) 0x74,
129: (byte) 0x00, (byte) 0x20, (byte) 0x00, (byte) 0x45,
130: (byte) 0x00, (byte) 0x6E, (byte) 0x00, (byte) 0x74,
131: (byte) 0x00, (byte) 0x72, (byte) 0x00, (byte) 0x79,
132: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
133: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
134: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
135: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
136: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
137: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
138: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
139: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
140: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
141: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
142: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
143: (byte) 0x00, (byte) 0x16, (byte) 0x00, (byte) 0x05,
144: (byte) 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
145: (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
146: (byte) 0xFF, (byte) 0x02, (byte) 0x00, (byte) 0x00,
147: (byte) 0x00, (byte) 0x20, (byte) 0x08, (byte) 0x02,
148: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
149: (byte) 0x00, (byte) 0xC0, (byte) 0x00, (byte) 0x00,
150: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
151: (byte) 0x46, (byte) 0x00, (byte) 0x00, (byte) 0x00,
152: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
153: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
154: (byte) 0x00, (byte) 0xC0, (byte) 0x5C, (byte) 0xE8,
155: (byte) 0x23, (byte) 0x9E, (byte) 0x6B, (byte) 0xC1,
156: (byte) 0x01, (byte) 0xFE, (byte) 0xFF, (byte) 0xFF,
157: (byte) 0xFF, (byte) 0x00, (byte) 0x00, (byte) 0x00,
158: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
159: (byte) 0x00 };
160:
161: verifyReadingProperty(0, input, 0, "Root Entry",
162: "{00020820-0000-0000-C000-000000000046}");
163: }
164:
165: private void verifyReadingProperty(int index, byte[] input,
166: int offset, String name, String sClsId) throws IOException {
167: RootProperty property = new RootProperty(index, input, offset);
168: ByteArrayOutputStream stream = new ByteArrayOutputStream(128);
169: byte[] expected = new byte[128];
170:
171: System.arraycopy(input, offset, expected, 0, 128);
172: property.writeData(stream);
173: byte[] output = stream.toByteArray();
174:
175: assertEquals(128, output.length);
176: for (int j = 0; j < 128; j++) {
177: assertEquals("mismatch at offset " + j, expected[j],
178: output[j]);
179: }
180: assertEquals(index, property.getIndex());
181: assertEquals(name, property.getName());
182: assertTrue(!property.getChildren().hasNext());
183: assertEquals(property.getStorageClsid().toString(), sClsId);
184: }
185:
186: /**
187: * main method to run the unit tests
188: *
189: * @param ignored_args
190: */
191:
192: public static void main(String[] ignored_args) {
193: System.out
194: .println("Testing org.apache.poi.poifs.property.RootProperty");
195: junit.textui.TestRunner.run(TestRootProperty.class);
196: }
197: }
|