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.storage;
019:
020: import java.io.*;
021:
022: import java.util.*;
023:
024: import junit.framework.*;
025:
026: import org.apache.poi.poifs.property.Property;
027:
028: /**
029: * Class to test PropertyBlock functionality
030: *
031: * @author Marc Johnson
032: */
033:
034: public class TestPropertyBlock extends TestCase {
035:
036: /**
037: * Constructor TestPropertyBlock
038: *
039: * @param name
040: */
041:
042: public TestPropertyBlock(String name) {
043: super (name);
044: }
045:
046: /**
047: * Test constructing PropertyBlocks
048: *
049: * @exception IOException
050: */
051:
052: public void testCreatePropertyBlocks() throws IOException {
053:
054: // test with 0 properties
055: List properties = new ArrayList();
056: BlockWritable[] blocks = PropertyBlock
057: .createPropertyBlockArray(properties);
058:
059: assertEquals(0, blocks.length);
060:
061: // test with 1 property
062: properties.add(new LocalProperty("Root Entry"));
063: blocks = PropertyBlock.createPropertyBlockArray(properties);
064: assertEquals(1, blocks.length);
065: byte[] testblock = new byte[512];
066:
067: for (int j = 0; j < 4; j++) {
068: setDefaultBlock(testblock, j);
069: }
070: testblock[0x0000] = (byte) 'R';
071: testblock[0x0002] = (byte) 'o';
072: testblock[0x0004] = (byte) 'o';
073: testblock[0x0006] = (byte) 't';
074: testblock[0x0008] = (byte) ' ';
075: testblock[0x000A] = (byte) 'E';
076: testblock[0x000C] = (byte) 'n';
077: testblock[0x000E] = (byte) 't';
078: testblock[0x0010] = (byte) 'r';
079: testblock[0x0012] = (byte) 'y';
080: testblock[0x0040] = (byte) 22;
081: verifyCorrect(blocks, testblock);
082:
083: // test with 3 properties
084: properties.add(new LocalProperty("workbook"));
085: properties.add(new LocalProperty("summary"));
086: blocks = PropertyBlock.createPropertyBlockArray(properties);
087: assertEquals(1, blocks.length);
088: testblock[0x0080] = (byte) 'w';
089: testblock[0x0082] = (byte) 'o';
090: testblock[0x0084] = (byte) 'r';
091: testblock[0x0086] = (byte) 'k';
092: testblock[0x0088] = (byte) 'b';
093: testblock[0x008A] = (byte) 'o';
094: testblock[0x008C] = (byte) 'o';
095: testblock[0x008E] = (byte) 'k';
096: testblock[0x00C0] = (byte) 18;
097: testblock[0x0100] = (byte) 's';
098: testblock[0x0102] = (byte) 'u';
099: testblock[0x0104] = (byte) 'm';
100: testblock[0x0106] = (byte) 'm';
101: testblock[0x0108] = (byte) 'a';
102: testblock[0x010A] = (byte) 'r';
103: testblock[0x010C] = (byte) 'y';
104: testblock[0x0140] = (byte) 16;
105: verifyCorrect(blocks, testblock);
106:
107: // test with 4 properties
108: properties.add(new LocalProperty("wintery"));
109: blocks = PropertyBlock.createPropertyBlockArray(properties);
110: assertEquals(1, blocks.length);
111: testblock[0x0180] = (byte) 'w';
112: testblock[0x0182] = (byte) 'i';
113: testblock[0x0184] = (byte) 'n';
114: testblock[0x0186] = (byte) 't';
115: testblock[0x0188] = (byte) 'e';
116: testblock[0x018A] = (byte) 'r';
117: testblock[0x018C] = (byte) 'y';
118: testblock[0x01C0] = (byte) 16;
119: verifyCorrect(blocks, testblock);
120:
121: // test with 5 properties
122: properties.add(new LocalProperty("foo"));
123: blocks = PropertyBlock.createPropertyBlockArray(properties);
124: assertEquals(2, blocks.length);
125: testblock = new byte[1024];
126: for (int j = 0; j < 8; j++) {
127: setDefaultBlock(testblock, j);
128: }
129: testblock[0x0000] = (byte) 'R';
130: testblock[0x0002] = (byte) 'o';
131: testblock[0x0004] = (byte) 'o';
132: testblock[0x0006] = (byte) 't';
133: testblock[0x0008] = (byte) ' ';
134: testblock[0x000A] = (byte) 'E';
135: testblock[0x000C] = (byte) 'n';
136: testblock[0x000E] = (byte) 't';
137: testblock[0x0010] = (byte) 'r';
138: testblock[0x0012] = (byte) 'y';
139: testblock[0x0040] = (byte) 22;
140: testblock[0x0080] = (byte) 'w';
141: testblock[0x0082] = (byte) 'o';
142: testblock[0x0084] = (byte) 'r';
143: testblock[0x0086] = (byte) 'k';
144: testblock[0x0088] = (byte) 'b';
145: testblock[0x008A] = (byte) 'o';
146: testblock[0x008C] = (byte) 'o';
147: testblock[0x008E] = (byte) 'k';
148: testblock[0x00C0] = (byte) 18;
149: testblock[0x0100] = (byte) 's';
150: testblock[0x0102] = (byte) 'u';
151: testblock[0x0104] = (byte) 'm';
152: testblock[0x0106] = (byte) 'm';
153: testblock[0x0108] = (byte) 'a';
154: testblock[0x010A] = (byte) 'r';
155: testblock[0x010C] = (byte) 'y';
156: testblock[0x0140] = (byte) 16;
157: testblock[0x0180] = (byte) 'w';
158: testblock[0x0182] = (byte) 'i';
159: testblock[0x0184] = (byte) 'n';
160: testblock[0x0186] = (byte) 't';
161: testblock[0x0188] = (byte) 'e';
162: testblock[0x018A] = (byte) 'r';
163: testblock[0x018C] = (byte) 'y';
164: testblock[0x01C0] = (byte) 16;
165: testblock[0x0200] = (byte) 'f';
166: testblock[0x0202] = (byte) 'o';
167: testblock[0x0204] = (byte) 'o';
168: testblock[0x0240] = (byte) 8;
169: verifyCorrect(blocks, testblock);
170: }
171:
172: private void setDefaultBlock(byte[] testblock, int j) {
173: int base = j * 128;
174: int index = 0;
175:
176: for (; index < 0x40; index++) {
177: testblock[base++] = (byte) 0;
178: }
179: testblock[base++] = (byte) 2;
180: testblock[base++] = (byte) 0;
181: index += 2;
182: for (; index < 0x44; index++) {
183: testblock[base++] = (byte) 0;
184: }
185: for (; index < 0x50; index++) {
186: testblock[base++] = (byte) 0xff;
187: }
188: for (; index < 0x80; index++) {
189: testblock[base++] = (byte) 0;
190: }
191: }
192:
193: private void verifyCorrect(BlockWritable[] blocks, byte[] testblock)
194: throws IOException {
195: ByteArrayOutputStream stream = new ByteArrayOutputStream(
196: 512 * blocks.length);
197:
198: for (int j = 0; j < blocks.length; j++) {
199: blocks[j].writeBlocks(stream);
200: }
201: byte[] output = stream.toByteArray();
202:
203: assertEquals(testblock.length, output.length);
204: for (int j = 0; j < testblock.length; j++) {
205: assertEquals("mismatch at offset " + j, testblock[j],
206: output[j]);
207: }
208: }
209:
210: /**
211: * main method to run the unit tests
212: *
213: * @param ignored_args
214: */
215:
216: public static void main(String[] ignored_args) {
217: System.out
218: .println("Testing org.apache.poi.poifs.storage.PropertyBlock");
219: junit.textui.TestRunner.run(TestPropertyBlock.class);
220: }
221: }
|