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.ddf;
019:
020: import junit.framework.TestCase;
021: import org.apache.poi.util.HexRead;
022: import org.apache.poi.util.HexDump;
023:
024: public class TestEscherContainerRecord extends TestCase {
025: public void testFillFields() throws Exception {
026: EscherRecordFactory f = new DefaultEscherRecordFactory();
027: byte[] data = HexRead.readFromString("0F 02 11 F1 00 00 00 00");
028: EscherRecord r = f.createRecord(data, 0);
029: r.fillFields(data, 0, f);
030: assertTrue(r instanceof EscherContainerRecord);
031: assertEquals((short) 0x020F, r.getOptions());
032: assertEquals((short) 0xF111, r.getRecordId());
033:
034: data = HexRead.readFromString("0F 02 11 F1 08 00 00 00"
035: + " 02 00 22 F2 00 00 00 00");
036: r = f.createRecord(data, 0);
037: r.fillFields(data, 0, f);
038: EscherRecord c = r.getChild(0);
039: assertFalse(c instanceof EscherContainerRecord);
040: assertEquals((short) 0x0002, c.getOptions());
041: assertEquals((short) 0xF222, c.getRecordId());
042: }
043:
044: public void testSerialize() throws Exception {
045: UnknownEscherRecord r = new UnknownEscherRecord();
046: r.setOptions((short) 0x123F);
047: r.setRecordId((short) 0xF112);
048: byte[] data = new byte[8];
049: r.serialize(0, data, new NullEscherSerializationListener());
050:
051: assertEquals("[3F, 12, 12, F1, 00, 00, 00, 00, ]", HexDump
052: .toHex(data));
053:
054: EscherRecord childRecord = new UnknownEscherRecord();
055: childRecord.setOptions((short) 0x9999);
056: childRecord.setRecordId((short) 0xFF01);
057: r.addChildRecord(childRecord);
058: data = new byte[16];
059: r.serialize(0, data, new NullEscherSerializationListener());
060:
061: assertEquals(
062: "[3F, 12, 12, F1, 08, 00, 00, 00, 99, 99, 01, FF, 00, 00, 00, 00, ]",
063: HexDump.toHex(data));
064:
065: }
066:
067: public void testToString() throws Exception {
068: EscherContainerRecord r = new EscherContainerRecord();
069: r.setRecordId(EscherContainerRecord.SP_CONTAINER);
070: r.setOptions((short) 0x000F);
071: String nl = System.getProperty("line.separator");
072: assertEquals(
073: "org.apache.poi.ddf.EscherContainerRecord (SpContainer):"
074: + nl + " isContainer: true" + nl
075: + " options: 0x000F" + nl
076: + " recordId: 0xF004" + nl
077: + " numchildren: 0" + nl, r.toString());
078:
079: EscherOptRecord r2 = new EscherOptRecord();
080: r2.setOptions((short) 0x9876);
081: r2.setRecordId(EscherOptRecord.RECORD_ID);
082:
083: r.addChildRecord(r2);
084: String expected = "org.apache.poi.ddf.EscherContainerRecord (SpContainer):"
085: + nl
086: + " isContainer: true"
087: + nl
088: + " options: 0x000F"
089: + nl
090: + " recordId: 0xF004"
091: + nl
092: + " numchildren: 1"
093: + nl
094: + " children: "
095: + nl
096: + "org.apache.poi.ddf.EscherOptRecord:"
097: + nl
098: + " isContainer: false"
099: + nl
100: + " options: 0x0003"
101: + nl
102: + " recordId: 0xF00B"
103: + nl
104: + " numchildren: 0"
105: + nl + " properties:" + nl;
106: assertEquals(expected, r.toString());
107:
108: }
109:
110: public void testGetRecordSize() throws Exception {
111: EscherContainerRecord r = new EscherContainerRecord();
112: r.addChildRecord(new EscherRecord() {
113: public int fillFields(byte[] data, int offset,
114: EscherRecordFactory recordFactory) {
115: return 0;
116: }
117:
118: public int serialize(int offset, byte[] data,
119: EscherSerializationListener listener) {
120: return 0;
121: }
122:
123: public int getRecordSize() {
124: return 10;
125: }
126:
127: public String getRecordName() {
128: return "";
129: }
130: });
131:
132: assertEquals(18, r.getRecordSize());
133: }
134:
135: }
|