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 DirectoryProperty functionality
030: *
031: * @author Marc Johnson
032: */
033:
034: public class TestDirectoryProperty extends TestCase {
035: private DirectoryProperty _property;
036: private byte[] _testblock;
037:
038: /**
039: * Constructor TestDirectoryProperty
040: *
041: * @param name
042: */
043:
044: public TestDirectoryProperty(String name) {
045: super (name);
046: }
047:
048: /**
049: * Test constructing DirectoryProperty
050: *
051: * @exception IOException
052: */
053:
054: public void testConstructor() throws IOException {
055: createBasicDirectoryProperty();
056: verifyProperty();
057: }
058:
059: /**
060: * Test pre-write functionality
061: *
062: * @exception IOException
063: */
064:
065: public void testPreWrite() throws IOException {
066: createBasicDirectoryProperty();
067: _property.preWrite();
068:
069: // shouldn't change anything at all
070: verifyProperty();
071: verifyChildren(0);
072:
073: // now try adding 1 property
074: createBasicDirectoryProperty();
075: _property.addChild(new LocalProperty(1));
076: _property.preWrite();
077:
078: // update children index
079: _testblock[0x4C] = 1;
080: _testblock[0x4D] = 0;
081: _testblock[0x4E] = 0;
082: _testblock[0x4F] = 0;
083: verifyProperty();
084: verifyChildren(1);
085:
086: // now try adding 2 properties
087: createBasicDirectoryProperty();
088: _property.addChild(new LocalProperty(1));
089: _property.addChild(new LocalProperty(2));
090: _property.preWrite();
091:
092: // update children index
093: _testblock[0x4C] = 2;
094: _testblock[0x4D] = 0;
095: _testblock[0x4E] = 0;
096: _testblock[0x4F] = 0;
097: verifyProperty();
098: verifyChildren(2);
099:
100: // beat on the children allocation code
101: for (int count = 1; count < 100; count++) {
102: createBasicDirectoryProperty();
103: for (int j = 1; j < (count + 1); j++) {
104: _property.addChild(new LocalProperty(j));
105: }
106: _property.preWrite();
107: verifyChildren(count);
108: }
109: }
110:
111: private void verifyChildren(int count) throws IOException {
112: Iterator iter = _property.getChildren();
113: List children = new ArrayList();
114:
115: while (iter.hasNext()) {
116: children.add(iter.next());
117: }
118: assertEquals(count, children.size());
119: if (count != 0) {
120: boolean[] found = new boolean[count];
121:
122: found[_property.getChildIndex() - 1] = true;
123: int total_found = 1;
124:
125: Arrays.fill(found, false);
126: iter = children.iterator();
127: while (iter.hasNext()) {
128: Property child = (Property) iter.next();
129: Child next = child.getNextChild();
130:
131: if (next != null) {
132: int index = ((Property) next).getIndex();
133:
134: if (index != -1) {
135: assertTrue("found index " + index + " twice",
136: !found[index - 1]);
137: found[index - 1] = true;
138: total_found++;
139: }
140: }
141: Child previous = child.getPreviousChild();
142:
143: if (previous != null) {
144: int index = ((Property) previous).getIndex();
145:
146: if (index != -1) {
147: assertTrue("found index " + index + " twice",
148: !found[index - 1]);
149: found[index - 1] = true;
150: total_found++;
151: }
152: }
153: }
154: assertEquals(count, total_found);
155: }
156: }
157:
158: private void createBasicDirectoryProperty() {
159: String name = "MyDirectory";
160:
161: _property = new DirectoryProperty(name);
162: _testblock = new byte[128];
163: int index = 0;
164:
165: for (; index < 0x40; index++) {
166: _testblock[index] = (byte) 0;
167: }
168: int limit = Math.min(31, name.length());
169:
170: _testblock[index++] = (byte) (2 * (limit + 1));
171: _testblock[index++] = (byte) 0;
172: _testblock[index++] = (byte) 1;
173: _testblock[index++] = (byte) 1;
174: for (; index < 0x50; index++) {
175: _testblock[index] = (byte) 0xff;
176: }
177: for (; index < 0x80; index++) {
178: _testblock[index] = (byte) 0;
179: }
180: byte[] name_bytes = name.getBytes();
181:
182: for (index = 0; index < limit; index++) {
183: _testblock[index * 2] = name_bytes[index];
184: }
185: }
186:
187: private void verifyProperty() throws IOException {
188: ByteArrayOutputStream stream = new ByteArrayOutputStream(512);
189:
190: _property.writeData(stream);
191: byte[] output = stream.toByteArray();
192:
193: assertEquals(_testblock.length, output.length);
194: for (int j = 0; j < _testblock.length; j++) {
195: assertEquals("mismatch at offset " + j, _testblock[j],
196: output[j]);
197: }
198: }
199:
200: /**
201: * Test addChild
202: *
203: * @exception IOException
204: */
205:
206: public void testAddChild() throws IOException {
207: createBasicDirectoryProperty();
208: _property.addChild(new LocalProperty(1));
209: _property.addChild(new LocalProperty(2));
210: try {
211: _property.addChild(new LocalProperty(1));
212: fail("should have caught IOException");
213: } catch (IOException ignored) {
214:
215: // as expected
216: }
217: try {
218: _property.addChild(new LocalProperty(2));
219: fail("should have caught IOException");
220: } catch (IOException ignored) {
221:
222: // as expected
223: }
224: _property.addChild(new LocalProperty(3));
225: }
226:
227: /**
228: * Test deleteChild
229: *
230: * @exception IOException
231: */
232:
233: public void testDeleteChild() throws IOException {
234: createBasicDirectoryProperty();
235: Property p1 = new LocalProperty(1);
236:
237: _property.addChild(p1);
238: try {
239: _property.addChild(new LocalProperty(1));
240: fail("should have caught IOException");
241: } catch (IOException ignored) {
242:
243: // as expected
244: }
245: assertTrue(_property.deleteChild(p1));
246: assertTrue(!_property.deleteChild(p1));
247: _property.addChild(new LocalProperty(1));
248: }
249:
250: /**
251: * Test changeName
252: *
253: * @exception IOException
254: */
255:
256: public void testChangeName() throws IOException {
257: createBasicDirectoryProperty();
258: Property p1 = new LocalProperty(1);
259: String originalName = p1.getName();
260:
261: _property.addChild(p1);
262: assertTrue(_property.changeName(p1, "foobar"));
263: assertEquals("foobar", p1.getName());
264: assertTrue(!_property.changeName(p1, "foobar"));
265: assertEquals("foobar", p1.getName());
266: Property p2 = new LocalProperty(1);
267:
268: _property.addChild(p2);
269: assertTrue(!_property.changeName(p1, originalName));
270: assertTrue(_property.changeName(p2, "foo"));
271: assertTrue(_property.changeName(p1, originalName));
272: }
273:
274: /**
275: * Test reading constructor
276: *
277: * @exception IOException
278: */
279:
280: public void testReadingConstructor() throws IOException {
281: byte[] input = { (byte) 0x42, (byte) 0x00, (byte) 0x6F,
282: (byte) 0x00, (byte) 0x6F, (byte) 0x00, (byte) 0x74,
283: (byte) 0x00, (byte) 0x20, (byte) 0x00, (byte) 0x45,
284: (byte) 0x00, (byte) 0x6E, (byte) 0x00, (byte) 0x74,
285: (byte) 0x00, (byte) 0x72, (byte) 0x00, (byte) 0x79,
286: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
287: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
288: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
289: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
290: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
291: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
292: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
293: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
294: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
295: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
296: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
297: (byte) 0x00, (byte) 0x16, (byte) 0x00, (byte) 0x01,
298: (byte) 0x01, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
299: (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
300: (byte) 0xFF, (byte) 0x02, (byte) 0x00, (byte) 0x00,
301: (byte) 0x00, (byte) 0x20, (byte) 0x08, (byte) 0x02,
302: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
303: (byte) 0x00, (byte) 0xC0, (byte) 0x00, (byte) 0x00,
304: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
305: (byte) 0x46, (byte) 0x00, (byte) 0x00, (byte) 0x00,
306: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
307: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
308: (byte) 0x00, (byte) 0xC0, (byte) 0x5C, (byte) 0xE8,
309: (byte) 0x23, (byte) 0x9E, (byte) 0x6B, (byte) 0xC1,
310: (byte) 0x01, (byte) 0xFE, (byte) 0xFF, (byte) 0xFF,
311: (byte) 0xFF, (byte) 0x00, (byte) 0x00, (byte) 0x00,
312: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
313: (byte) 0x00 };
314:
315: verifyReadingProperty(0, input, 0, "Boot Entry");
316: }
317:
318: private void verifyReadingProperty(int index, byte[] input,
319: int offset, String name) throws IOException {
320: DirectoryProperty property = new DirectoryProperty(index,
321: input, offset);
322: ByteArrayOutputStream stream = new ByteArrayOutputStream(128);
323: byte[] expected = new byte[128];
324:
325: System.arraycopy(input, offset, expected, 0, 128);
326: property.writeData(stream);
327: byte[] output = stream.toByteArray();
328:
329: assertEquals(128, output.length);
330: for (int j = 0; j < 128; j++) {
331: assertEquals("mismatch at offset " + j, expected[j],
332: output[j]);
333: }
334: assertEquals(index, property.getIndex());
335: assertEquals(name, property.getName());
336: assertTrue(!property.getChildren().hasNext());
337: }
338:
339: /**
340: * main method to run the unit tests
341: *
342: * @param ignored_args
343: */
344:
345: public static void main(String[] ignored_args) {
346: System.out
347: .println("Testing org.apache.poi.poifs.property.DirectoryProperty");
348: junit.textui.TestRunner.run(TestDirectoryProperty.class);
349: }
350: }
|