diff options
author | Josh Micich <josh@apache.org> | 2008-11-06 02:38:06 +0000 |
---|---|---|
committer | Josh Micich <josh@apache.org> | 2008-11-06 02:38:06 +0000 |
commit | 2963774c3752202c0686b0223d7dfd8ae1973ca8 (patch) | |
tree | 305535395789ea61f556b412ab5288a7886e06dd /src/testcases/org | |
parent | 75e352f2a073bdda52a2e3c30df63be6c71ff61d (diff) | |
download | poi-2963774c3752202c0686b0223d7dfd8ae1973ca8.tar.gz poi-2963774c3752202c0686b0223d7dfd8ae1973ca8.zip |
Merged revisions 709570,709598,710114,710134,710136,711505,711513-711515,711694,711739,711741,711746,711749 via svnmerge from
https://svn.apache.org/repos/asf/poi/trunk
........
r709570 | josh | 2008-10-31 14:17:08 -0700 (Fri, 31 Oct 2008) | 1 line
made DrawingSelectionRecord into a plain BIFF record (not an escher holder aggregate). Added some interpretation of fields
........
r709598 | josh | 2008-10-31 16:24:41 -0700 (Fri, 31 Oct 2008) | 1 line
Simplified multiple record expansion logic
........
r710114 | yegor | 2008-11-03 09:54:01 -0800 (Mon, 03 Nov 2008) | 1 line
fixed #46122: Picture#getEscherBSERecord threw NullPointerException if EscherContainerRecord.BSTORE_CONTAINER was not found
........
r710134 | yegor | 2008-11-03 11:19:39 -0800 (Mon, 03 Nov 2008) | 1 line
fixed bug #46033: table cells had incorrect text type resulting in corrupted style info
........
r710136 | yegor | 2008-11-03 11:23:52 -0800 (Mon, 03 Nov 2008) | 1 line
updated status of the fixed bug #46033
........
r711505 | josh | 2008-11-04 19:50:31 -0800 (Tue, 04 Nov 2008) | 1 line
Refactored test case
........
r711513 | josh | 2008-11-04 21:45:17 -0800 (Tue, 04 Nov 2008) | 1 line
Converted ConstantValueParser to use plain Strings instead of UnicodeStrings
........
r711514 | josh | 2008-11-04 21:52:35 -0800 (Tue, 04 Nov 2008) | 1 line
Converted SupBookRecord to use plain Strings instead of UnicodeStrings
........
r711515 | josh | 2008-11-04 22:15:59 -0800 (Tue, 04 Nov 2008) | 1 line
Refactored test case
........
r711694 | josh | 2008-11-05 12:46:00 -0800 (Wed, 05 Nov 2008) | 1 line
Fixed bug in conversion to/from text cells
........
r711739 | josh | 2008-11-05 15:28:55 -0800 (Wed, 05 Nov 2008) | 1 line
Refactoring test case
........
r711741 | josh | 2008-11-05 15:35:02 -0800 (Wed, 05 Nov 2008) | 1 line
Refactoring test case
........
r711746 | josh | 2008-11-05 15:45:42 -0800 (Wed, 05 Nov 2008) | 1 line
Fixed mistake in test case. Constant value was 4 bytes too large (should be max *data* size not max *record* size).
........
r711749 | josh | 2008-11-05 17:12:41 -0800 (Wed, 05 Nov 2008) | 1 line
Introduced ContinuableRecord to help fix serialization of StringRecords with large data. Fixed TextObjectRecord to only write 16bit unicode when needed. Simplification in UnicodeString.
........
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@711755 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org')
10 files changed, 370 insertions, 311 deletions
diff --git a/src/testcases/org/apache/poi/hssf/record/TestRecordFactory.java b/src/testcases/org/apache/poi/hssf/record/TestRecordFactory.java index 13cea4189c..b54b4032d6 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestRecordFactory.java +++ b/src/testcases/org/apache/poi/hssf/record/TestRecordFactory.java @@ -48,7 +48,6 @@ public final class TestRecordFactory extends TestCase { byte[] data = { 0, 6, 5, 0, -2, 28, -51, 7, -55, 64, 0, 0, 6, 1, 0, 0 }; - short size = 16; Record[] record = RecordFactory.createRecord(TestcaseRecordInputStream.create(recType, data)); assertEquals(BOFRecord.class.getName(), @@ -64,7 +63,6 @@ public final class TestRecordFactory extends TestCase { assertEquals(5, bofRecord.getType()); assertEquals(1536, bofRecord.getVersion()); recType = MMSRecord.sid; - size = 2; data = new byte[] { 0, 0 @@ -93,7 +91,6 @@ public final class TestRecordFactory extends TestCase { byte[] data = { 0, 0, 0, 0, 21, 0, 0, 0, 0, 0 }; - short size = 10; Record[] record = RecordFactory.createRecord(TestcaseRecordInputStream.create(recType, data)); assertEquals(NumberRecord.class.getName(), @@ -154,34 +151,34 @@ public final class TestRecordFactory extends TestCase { */ public void testMixedContinue() throws Exception { /** - * Taken from a real test sample file 39512.xls. See Bug 39512 for details. + * Adapted from a real test sample file 39512.xls (Offset 0x4854). + * See Bug 39512 for details. */ String dump = //OBJ - "5D, 00, 48, 00, 15, 00, 12, 00, 0C, 00, 3C, 00, 11, 00, A0, 2E, 03, 01, CC, 42, " + - "CF, 00, 00, 00, 00, 00, 0A, 00, 0C, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, " + - "03, 00, 0B, 00, 06, 00, 28, 01, 03, 01, 00, 00, 12, 00, 08, 00, 00, 00, 00, 00, " + - "00, 00, 03, 00, 11, 00, 04, 00, 3D, 00, 00, 00, 00, 00, 00, 00, " + + "5D 00 48 00 15 00 12 00 0C 00 3C 00 11 00 A0 2E 03 01 CC 42 " + + "CF 00 00 00 00 00 0A 00 0C 00 00 00 00 00 00 00 00 00 00 00 " + + "03 00 0B 00 06 00 28 01 03 01 00 00 12 00 08 00 00 00 00 00 " + + "00 00 03 00 11 00 04 00 3D 00 00 00 00 00 00 00 " + //MSODRAWING - "EC, 00, 08, 00, 00, 00, 0D, F0, 00, 00, 00, 00, " + - //TXO - "B6, 01, 12, 00, 22, 02, 00, 00, 00, 00, 00, 00, 00, 00, 10, 00, 10, 00, 00, 00, " + - "00, 00, 3C, 00, 21, 00, 01, 4F, 00, 70, 00, 74, 00, 69, 00, 6F, 00, 6E, 00, 20, " + - "00, 42, 00, 75, 00, 74, 00, 74, 00, 6F, 00, 6E, 00, 20, 00, 33, 00, 39, 00, 3C, " + - "00, 10, 00, 00, 00, 05, 00, 00, 00, 00, 00, 10, 00, 00, 00, 00, 00, 00, 00, " + - //CONTINUE - "3C, 00, 7E, 00, 0F, 00, 04, F0, 7E, 00, 00, 00, 92, 0C, 0A, F0, 08, 00, 00, 00, " + - "3D, 04, 00, 00, 00, 0A, 00, 00, A3, 00, 0B, F0, 3C, 00, 00, 00, 7F, 00, 00, 01, " + - "00, 01, 80, 00, 8C, 01, 03, 01, 85, 00, 01, 00, 00, 00, 8B, 00, 02, 00, 00, 00, " + - "BF, 00, 08, 00, 1A, 00, 7F, 01, 29, 00, 29, 00, 81, 01, 41, 00, 00, 08, BF, 01, " + - "00, 00, 10, 00, C0, 01, 40, 00, 00, 08, FF, 01, 00, 00, 08, 00, 00, 00, 10, F0, " + - "12, 00, 00, 00, 02, 00, 02, 00, A0, 03, 18, 00, B5, 00, 04, 00, 30, 02, 1A, 00, " + - "00, 00, 00, 00, 11, F0, 00, 00, 00, 00, " + + "EC 00 08 00 00 00 0D F0 00 00 00 00 " + + //TXO (and 2 trailing CONTINUE records) + "B6 01 12 00 22 02 00 00 00 00 00 00 00 00 10 00 10 00 00 00 00 00 " + + "3C 00 11 00 00 4F 70 74 69 6F 6E 20 42 75 74 74 6F 6E 20 33 39 " + + "3C 00 10 00 00 00 05 00 00 00 00 00 10 00 00 00 00 00 00 00 " + + // another CONTINUE + "3C 00 7E 00 0F 00 04 F0 7E 00 00 00 92 0C 0A F0 08 00 00 00 " + + "3D 04 00 00 00 0A 00 00 A3 00 0B F0 3C 00 00 00 7F 00 00 01 " + + "00 01 80 00 8C 01 03 01 85 00 01 00 00 00 8B 00 02 00 00 00 " + + "BF 00 08 00 1A 00 7F 01 29 00 29 00 81 01 41 00 00 08 BF 01 " + + "00 00 10 00 C0 01 40 00 00 08 FF 01 00 00 08 00 00 00 10 F0 " + + "12 00 00 00 02 00 02 00 A0 03 18 00 B5 00 04 00 30 02 1A 00 " + + "00 00 00 00 11 F0 00 00 00 00 " + //OBJ - "5D, 00, 48, 00, 15, 00, 12, 00, 0C, 00, 3D, 00, 11, 00, 8C, 01, 03, 01, C8, 59, CF, 00, 00, " + - "00, 00, 00, 0A, 00, 0C, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 03, 00, 0B, 00, 06, 00, " + - "7C, 16, 03, 01, 00, 00, 12, 00, 08, 00, 00, 00, 00, 00, 00, 00, 03, 00, 11, 00, 04, 00, 01, " + - "00, 00, 00, 00, 00, 00, 00"; + "5D 00 48 00 15 00 12 00 0C 00 3D 00 11 00 8C 01 03 01 C8 59 CF 00 00 " + + "00 00 00 0A 00 0C 00 00 00 00 00 00 00 00 00 00 00 03 00 0B 00 06 00 " + + "7C 16 03 01 00 00 12 00 08 00 00 00 00 00 00 00 03 00 11 00 04 00 01 " + + "00 00 00 00 00 00 00"; byte[] data = HexRead.readFromString(dump); List records = RecordFactory.createRecords(new ByteArrayInputStream(data)); diff --git a/src/testcases/org/apache/poi/hssf/record/TestSSTRecordSizeCalculator.java b/src/testcases/org/apache/poi/hssf/record/TestSSTRecordSizeCalculator.java index 83aaf658f0..2a0830ac79 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestSSTRecordSizeCalculator.java +++ b/src/testcases/org/apache/poi/hssf/record/TestSSTRecordSizeCalculator.java @@ -1,4 +1,3 @@ - /* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with @@ -15,133 +14,106 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ - package org.apache.poi.hssf.record; import junit.framework.TestCase; +import org.apache.poi.hssf.record.cont.ContinuableRecordOutput; import org.apache.poi.util.IntMapper; /** * Tests that records size calculates correctly. - * + * * @author Glen Stampoultzis (glens at apache.org) */ -public class TestSSTRecordSizeCalculator - extends TestCase -{ - private static final String SMALL_STRING = "Small string"; - private static final int COMPRESSED_PLAIN_STRING_OVERHEAD = 3; -// private List recordLengths; - private IntMapper strings; - private static final int OPTION_FIELD_SIZE = 1; - - public TestSSTRecordSizeCalculator( String s ) - { - super( s ); - } - - public void testBasic() - throws Exception - { - strings.add(makeUnicodeString(SMALL_STRING)); - SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(strings); - assertEquals(SSTRecord.SST_RECORD_OVERHEAD + COMPRESSED_PLAIN_STRING_OVERHEAD + SMALL_STRING.length(), - calculator.getRecordSize()); - } - - public void testBigStringAcrossUnicode() - throws Exception - { - String bigString = new String(new char[SSTRecord.MAX_DATA_SPACE + 100]); - strings.add(makeUnicodeString(bigString)); - SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(strings); - assertEquals(SSTRecord.SST_RECORD_OVERHEAD - + COMPRESSED_PLAIN_STRING_OVERHEAD - + SSTRecord.MAX_DATA_SPACE - + SSTRecord.STD_RECORD_OVERHEAD - + OPTION_FIELD_SIZE - + 100, - calculator.getRecordSize()); - } - - public void testPerfectFit() - throws Exception - { - String perfectFit = new String(new char[SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD]); - strings.add(makeUnicodeString(perfectFit)); - SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(strings); - assertEquals(SSTRecord.SST_RECORD_OVERHEAD - + COMPRESSED_PLAIN_STRING_OVERHEAD - + perfectFit.length(), - calculator.getRecordSize()); - } - - public void testJustOversized() - throws Exception - { - String tooBig = new String(new char[SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD + 1]); - strings.add(makeUnicodeString(tooBig)); - SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(strings); - assertEquals(SSTRecord.SST_RECORD_OVERHEAD - + COMPRESSED_PLAIN_STRING_OVERHEAD - + tooBig.length() - 1 - // continue record - + SSTRecord.STD_RECORD_OVERHEAD - + OPTION_FIELD_SIZE - + 1, - calculator.getRecordSize()); - - } - - public void testSecondStringStartsOnNewContinuation() - throws Exception - { - String perfectFit = new String(new char[SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD]); - strings.add(makeUnicodeString(perfectFit)); - strings.add(makeUnicodeString(SMALL_STRING)); - SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(strings); - assertEquals(SSTRecord.SST_RECORD_OVERHEAD - + SSTRecord.MAX_DATA_SPACE - // second string - + SSTRecord.STD_RECORD_OVERHEAD - + COMPRESSED_PLAIN_STRING_OVERHEAD - + SMALL_STRING.length(), - calculator.getRecordSize()); - } - - public void testHeaderCrossesNormalContinuePoint() - throws Exception - { - String almostPerfectFit = new String(new char[SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD - 2]); - strings.add(makeUnicodeString(almostPerfectFit)); - String oneCharString = new String(new char[1]); - strings.add(makeUnicodeString(oneCharString)); - SSTRecordSizeCalculator calculator = new SSTRecordSizeCalculator(strings); - assertEquals(SSTRecord.SST_RECORD_OVERHEAD - + COMPRESSED_PLAIN_STRING_OVERHEAD - + almostPerfectFit.length() - // second string - + SSTRecord.STD_RECORD_OVERHEAD - + COMPRESSED_PLAIN_STRING_OVERHEAD - + oneCharString.length(), - calculator.getRecordSize()); - - } - - - public void setUp() - { - strings = new IntMapper(); - } - - - private UnicodeString makeUnicodeString( String s ) - { - UnicodeString st = new UnicodeString(s); - st.setOptionFlags((byte)0); - return st; - } - +public final class TestSSTRecordSizeCalculator extends TestCase { + private static final String SMALL_STRING = "Small string"; + private static final int COMPRESSED_PLAIN_STRING_OVERHEAD = 3; + private static final int OPTION_FIELD_SIZE = 1; + + private final IntMapper strings = new IntMapper(); + + + private void confirmSize(int expectedSize) { + ContinuableRecordOutput cro = ContinuableRecordOutput.createForCountingOnly(); + SSTSerializer ss = new SSTSerializer(strings, 0, 0); + ss.serialize(cro); + assertEquals(expectedSize, cro.getTotalSize()); + } + + public void testBasic() { + strings.add(makeUnicodeString(SMALL_STRING)); + confirmSize(SSTRecord.SST_RECORD_OVERHEAD + + COMPRESSED_PLAIN_STRING_OVERHEAD + + SMALL_STRING.length()); + } + + public void testBigStringAcrossUnicode() { + int bigString = SSTRecord.MAX_DATA_SPACE + 100; + strings.add(makeUnicodeString(bigString)); + confirmSize(SSTRecord.SST_RECORD_OVERHEAD + + COMPRESSED_PLAIN_STRING_OVERHEAD + + SSTRecord.MAX_DATA_SPACE + + SSTRecord.STD_RECORD_OVERHEAD + + OPTION_FIELD_SIZE + + 100); + } + + public void testPerfectFit() { + int perfectFit = SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD; + strings.add(makeUnicodeString(perfectFit)); + confirmSize(SSTRecord.SST_RECORD_OVERHEAD + + COMPRESSED_PLAIN_STRING_OVERHEAD + + perfectFit); + } + + public void testJustOversized() { + int tooBig = SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD + 1; + strings.add(makeUnicodeString(tooBig)); + confirmSize(SSTRecord.SST_RECORD_OVERHEAD + + COMPRESSED_PLAIN_STRING_OVERHEAD + + tooBig - 1 + // continue record + + SSTRecord.STD_RECORD_OVERHEAD + + OPTION_FIELD_SIZE + 1); + + } + + public void testSecondStringStartsOnNewContinuation() { + int perfectFit = SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD; + strings.add(makeUnicodeString(perfectFit)); + strings.add(makeUnicodeString(SMALL_STRING)); + confirmSize(SSTRecord.SST_RECORD_OVERHEAD + + SSTRecord.MAX_DATA_SPACE + // second string + + SSTRecord.STD_RECORD_OVERHEAD + + COMPRESSED_PLAIN_STRING_OVERHEAD + + SMALL_STRING.length()); + } + + public void testHeaderCrossesNormalContinuePoint() { + int almostPerfectFit = SSTRecord.MAX_DATA_SPACE - COMPRESSED_PLAIN_STRING_OVERHEAD - 2; + strings.add(makeUnicodeString(almostPerfectFit)); + String oneCharString = new String(new char[1]); + strings.add(makeUnicodeString(oneCharString)); + confirmSize(SSTRecord.SST_RECORD_OVERHEAD + + COMPRESSED_PLAIN_STRING_OVERHEAD + + almostPerfectFit + // second string + + SSTRecord.STD_RECORD_OVERHEAD + + COMPRESSED_PLAIN_STRING_OVERHEAD + + oneCharString.length()); + + } + private static UnicodeString makeUnicodeString(int size) { + String s = new String(new char[size]); + return makeUnicodeString(s); + } + + private static UnicodeString makeUnicodeString(String s) { + UnicodeString st = new UnicodeString(s); + st.setOptionFlags((byte) 0); + return st; + } } diff --git a/src/testcases/org/apache/poi/hssf/record/TestStringRecord.java b/src/testcases/org/apache/poi/hssf/record/TestStringRecord.java index 14b708cdc8..ec7b84c693 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestStringRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestStringRecord.java @@ -18,6 +18,12 @@ package org.apache.poi.hssf.record; +import org.apache.poi.util.HexRead; +import org.apache.poi.util.LittleEndian; +import org.apache.poi.util.LittleEndianByteArrayInputStream; +import org.apache.poi.util.LittleEndianInput; + +import junit.framework.AssertionFailedError; import junit.framework.TestCase; /** @@ -28,29 +34,66 @@ import junit.framework.TestCase; * @author Glen Stampoultzis (glens at apache.org) */ public final class TestStringRecord extends TestCase { - byte[] data = new byte[] { - (byte)0x0B,(byte)0x00, // length - (byte)0x00, // option - // string - (byte)0x46,(byte)0x61,(byte)0x68,(byte)0x72,(byte)0x7A,(byte)0x65,(byte)0x75,(byte)0x67,(byte)0x74,(byte)0x79,(byte)0x70 - }; - - public void testLoad() { - - StringRecord record = new StringRecord(TestcaseRecordInputStream.create(0x207, data)); - assertEquals( "Fahrzeugtyp", record.getString()); - - assertEquals( 18, record.getRecordSize() ); - } - - public void testStore() - { - StringRecord record = new StringRecord(); - record.setString("Fahrzeugtyp"); - - byte [] recordBytes = record.serialize(); - assertEquals(recordBytes.length - 4, data.length); - for (int i = 0; i < data.length; i++) - assertEquals("At offset " + i, data[i], recordBytes[i+4]); - } + private static final byte[] data = HexRead.readFromString( + "0B 00 " + // length + "00 " + // option + // string + "46 61 68 72 7A 65 75 67 74 79 70" + ); + + public void testLoad() { + + StringRecord record = new StringRecord(TestcaseRecordInputStream.create(0x207, data)); + assertEquals( "Fahrzeugtyp", record.getString()); + + assertEquals( 18, record.getRecordSize() ); + } + + public void testStore() { + StringRecord record = new StringRecord(); + record.setString("Fahrzeugtyp"); + + byte [] recordBytes = record.serialize(); + assertEquals(recordBytes.length - 4, data.length); + for (int i = 0; i < data.length; i++) + assertEquals("At offset " + i, data[i], recordBytes[i+4]); + } + + public void testContinue() { + int MAX_BIFF_DATA = RecordInputStream.MAX_RECORD_DATA_SIZE; + int TEXT_LEN = MAX_BIFF_DATA + 1000; // deliberately over-size + String textChunk = "ABCDEGGHIJKLMNOP"; // 16 chars + StringBuffer sb = new StringBuffer(16384); + while (sb.length() < TEXT_LEN) { + sb.append(textChunk); + } + sb.setLength(TEXT_LEN); + + StringRecord sr = new StringRecord(); + sr.setString(sb.toString()); + byte[] ser = sr.serialize(); + assertEquals(StringRecord.sid, LittleEndian.getUShort(ser, 0)); + if (LittleEndian.getUShort(ser, 2) > MAX_BIFF_DATA) { + throw new AssertionFailedError( + "StringRecord should have been split with a continue record"); + } + // Confirm expected size of first record, and ushort strLen. + assertEquals(MAX_BIFF_DATA, LittleEndian.getUShort(ser, 2)); + assertEquals(TEXT_LEN, LittleEndian.getUShort(ser, 4)); + + // Confirm first few bytes of ContinueRecord + LittleEndianInput crIn = new LittleEndianByteArrayInputStream(ser, (MAX_BIFF_DATA + 4)); + int nCharsInFirstRec = MAX_BIFF_DATA - (2 + 1); // strLen, optionFlags + int nCharsInSecondRec = TEXT_LEN - nCharsInFirstRec; + assertEquals(ContinueRecord.sid, crIn.readUShort()); + assertEquals(1 + nCharsInSecondRec, crIn.readUShort()); + assertEquals(0, crIn.readUByte()); + assertEquals('N', crIn.readUByte()); + assertEquals('O', crIn.readUByte()); + + // re-read and make sure string value is the same + RecordInputStream in = TestcaseRecordInputStream.create(ser); + StringRecord sr2 = new StringRecord(in); + assertEquals(sb.toString(), sr2.getString()); + } } diff --git a/src/testcases/org/apache/poi/hssf/record/TestSupBookRecord.java b/src/testcases/org/apache/poi/hssf/record/TestSupBookRecord.java index d7725b8ee0..82ac62f70c 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestSupBookRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestSupBookRecord.java @@ -69,10 +69,10 @@ public final class TestSupBookRecord extends TestCase { assertEquals( 34, record.getRecordSize() ); //sid+size+data assertEquals("testURL", record.getURL()); - UnicodeString[] sheetNames = record.getSheetNames(); + String[] sheetNames = record.getSheetNames(); assertEquals(2, sheetNames.length); - assertEquals("Sheet1", sheetNames[0].getString()); - assertEquals("Sheet2", sheetNames[1].getString()); + assertEquals("Sheet1", sheetNames[0]); + assertEquals("Sheet2", sheetNames[1]); } /** @@ -97,11 +97,8 @@ public final class TestSupBookRecord extends TestCase { } public void testStoreER() { - UnicodeString url = new UnicodeString("testURL"); - UnicodeString[] sheetNames = { - new UnicodeString("Sheet1"), - new UnicodeString("Sheet2"), - }; + String url = "testURL"; + String[] sheetNames = { "Sheet1", "Sheet2", }; SupBookRecord record = SupBookRecord.createExternalReferences(url, sheetNames); TestcaseRecordInputStream.confirmRecordEncoding(0x01AE, dataER, record.serialize()); diff --git a/src/testcases/org/apache/poi/hssf/record/TestTextObjectBaseRecord.java b/src/testcases/org/apache/poi/hssf/record/TestTextObjectBaseRecord.java index 9b53cdd339..674279388c 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestTextObjectBaseRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestTextObjectBaseRecord.java @@ -44,9 +44,9 @@ public final class TestTextObjectBaseRecord extends TestCase { "00 00" + "00 00 " + "3C 00 " + // ContinueRecord.sid - "05 00 " + // size 5 - "01 " + // unicode uncompressed - "41 00 42 00 " + // 'AB' + "03 00 " + // size 3 + "00 " + // unicode compressed + "41 42 " + // 'AB' "3C 00 " + // ContinueRecord.sid "10 00 " + // size 16 "00 00 18 00 00 00 00 00 " + @@ -63,7 +63,7 @@ public final class TestTextObjectBaseRecord extends TestCase { assertEquals(true, record.isTextLocked()); assertEquals(TextObjectRecord.TEXT_ORIENTATION_ROT_RIGHT, record.getTextOrientation()); - assertEquals(51, record.getRecordSize() ); + assertEquals(49, record.getRecordSize() ); } public void testStore() diff --git a/src/testcases/org/apache/poi/hssf/record/TestTextObjectRecord.java b/src/testcases/org/apache/poi/hssf/record/TestTextObjectRecord.java index 19ec07c810..39ea8ba820 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestTextObjectRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestTextObjectRecord.java @@ -37,16 +37,14 @@ import org.apache.poi.util.LittleEndian; public final class TestTextObjectRecord extends TestCase {
private static final byte[] simpleData = HexRead.readFromString(
- "B6 01 12 00 " +
- "12 02 00 00 00 00 00 00" +
- "00 00 0D 00 08 00 00 00" +
- "00 00 " +
- "3C 00 1B 00 " +
- "01 48 00 65 00 6C 00 6C 00 6F 00 " +
- "2C 00 20 00 57 00 6F 00 72 00 6C " +
- "00 64 00 21 00 " +
- "3C 00 08 " +
- "00 0D 00 00 00 00 00 00 00"
+ "B6 01 12 00 " +
+ "12 02 00 00 00 00 00 00" +
+ "00 00 0D 00 08 00 00 00" +
+ "00 00 " +
+ "3C 00 0E 00 " +
+ "00 48 65 6C 6C 6F 2C 20 57 6F 72 6C 64 21 " +
+ "3C 00 08 " +
+ "00 0D 00 00 00 00 00 00 00"
);
@@ -92,12 +90,12 @@ public final class TestTextObjectRecord extends TestCase { record.setStr(str);
byte [] ser = record.serialize();
-
+
int formatDataLen = LittleEndian.getUShort(ser, 16);
assertEquals("formatDataLength", 0, formatDataLen);
assertEquals(22, ser.length); // just the TXO record
-
+
//read again
RecordInputStream is = TestcaseRecordInputStream.create(ser);
record = new TextObjectRecord(is);
@@ -152,38 +150,38 @@ public final class TestTextObjectRecord extends TestCase { byte[] cln = cloned.serialize();
assertTrue(Arrays.equals(src, cln));
}
-
- /** similar to {@link #simpleData} but with link formula at end of TXO rec*/
+
+ /** similar to {@link #simpleData} but with link formula at end of TXO rec*/
private static final byte[] linkData = HexRead.readFromString(
- "B6 01 " + // TextObjectRecord.sid
- "1E 00 " + // size 18
- "44 02 02 00 00 00 00 00" +
- "00 00 " +
- "02 00 " + // strLen 2
- "10 00 " + // 16 bytes for 2 format runs
- "00 00 00 00 " +
+ "B6 01 " + // TextObjectRecord.sid
+ "1E 00 " + // size 18
+ "44 02 02 00 00 00 00 00" +
+ "00 00 " +
+ "02 00 " + // strLen 2
+ "10 00 " + // 16 bytes for 2 format runs
+ "00 00 00 00 " +
"05 00 " + // formula size
"D4 F0 8A 03 " + // unknownInt
"24 01 00 13 C0 " + //tRef(T2)
"13 " + // ??
- "3C 00 " + // ContinueRecord.sid
- "05 00 " + // size 5
- "01 " + // unicode uncompressed
- "41 00 42 00 " + // 'AB'
- "3C 00 " + // ContinueRecord.sid
- "10 00 " + // size 16
- "00 00 18 00 00 00 00 00 " +
- "02 00 00 00 00 00 00 00 "
+ "3C 00 " + // ContinueRecord.sid
+ "03 00 " + // size 3
+ "00 " + // unicode compressed
+ "41 42 " + // 'AB'
+ "3C 00 " + // ContinueRecord.sid
+ "10 00 " + // size 16
+ "00 00 18 00 00 00 00 00 " +
+ "02 00 00 00 00 00 00 00 "
);
-
-
+
+
public void testLinkFormula() {
RecordInputStream is = new RecordInputStream(new ByteArrayInputStream(linkData));
is.nextRecord();
TextObjectRecord rec = new TextObjectRecord(is);
-
+
Ptg ptg = rec.getLinkRefPtg();
assertNotNull(ptg);
assertEquals(RefPtg.class, ptg.getClass());
@@ -193,6 +191,6 @@ public final class TestTextObjectRecord extends TestCase { byte [] data2 = rec.serialize();
assertEquals(linkData.length, data2.length);
assertTrue(Arrays.equals(linkData, data2));
- }
-
+ }
+
}
diff --git a/src/testcases/org/apache/poi/hssf/record/TestUnicodeString.java b/src/testcases/org/apache/poi/hssf/record/TestUnicodeString.java index 8f3c65be49..1a80f9e921 100755 --- a/src/testcases/org/apache/poi/hssf/record/TestUnicodeString.java +++ b/src/testcases/org/apache/poi/hssf/record/TestUnicodeString.java @@ -15,115 +15,123 @@ limitations under the License. ==================================================================== */ - package org.apache.poi.hssf.record; -import org.apache.poi.util.HexRead; - import junit.framework.TestCase; +import org.apache.poi.hssf.record.cont.ContinuableRecordOutput; + /** - * Tests that records size calculates correctly. + * Tests that {@link UnicodeString} record size calculates correctly. The record size + * is used when serializing {@link SSTRecord}s. * * @author Jason Height (jheight at apache.org) */ public final class TestUnicodeString extends TestCase { + private static final int MAX_DATA_SIZE = RecordInputStream.MAX_RECORD_DATA_SIZE; + /** a 4 character string requiring 16 bit encoding */ + private static final String STR_16_BIT = "A\u591A\u8A00\u8A9E"; + + private static void confirmSize(int expectedSize, UnicodeString s) { + confirmSize(expectedSize, s, 0); + } + /** + * Note - a value of zero for <tt>amountUsedInCurrentRecord</tt> would only ever occur just + * after a {@link ContinueRecord} had been started. In the initial {@link SSTRecord} this + * value starts at 8 (for the first {@link UnicodeString} written). In general, it can be + * any value between 0 and {@link #MAX_DATA_SIZE} + */ + private static void confirmSize(int expectedSize, UnicodeString s, int amountUsedInCurrentRecord) { + ContinuableRecordOutput out = ContinuableRecordOutput.createForCountingOnly(); + out.writeContinue(); + for(int i=amountUsedInCurrentRecord; i>0; i--) { + out.writeByte(0); + } + int size0 = out.getTotalSize(); + s.serialize(out); + int size1 = out.getTotalSize(); + int actualSize = size1-size0; + assertEquals(expectedSize, actualSize); + } public void testSmallStringSize() { //Test a basic string UnicodeString s = makeUnicodeString("Test"); - UnicodeString.UnicodeRecordStats stats = new UnicodeString.UnicodeRecordStats(); - s.getRecordSize(stats); - assertEquals(7, stats.recordSize); + confirmSize(7, s); //Test a small string that is uncompressed + s = makeUnicodeString(STR_16_BIT); s.setOptionFlags((byte)0x01); - stats = new UnicodeString.UnicodeRecordStats(); - s.getRecordSize(stats); - assertEquals(11, stats.recordSize); + confirmSize(11, s); //Test a compressed small string that has rich text formatting + s.setString("Test"); s.setOptionFlags((byte)0x8); UnicodeString.FormatRun r = new UnicodeString.FormatRun((short)0,(short)1); s.addFormatRun(r); UnicodeString.FormatRun r2 = new UnicodeString.FormatRun((short)2,(short)2); s.addFormatRun(r2); - stats = new UnicodeString.UnicodeRecordStats(); - s.getRecordSize(stats); - assertEquals(17, stats.recordSize); + confirmSize(17, s); //Test a uncompressed small string that has rich text formatting + s.setString(STR_16_BIT); s.setOptionFlags((byte)0x9); - stats = new UnicodeString.UnicodeRecordStats(); - s.getRecordSize(stats); - assertEquals(21, stats.recordSize); + confirmSize(21, s); //Test a compressed small string that has rich text and extended text + s.setString("Test"); s.setOptionFlags((byte)0xC); s.setExtendedRst(new byte[]{(byte)0x1,(byte)0x2,(byte)0x3,(byte)0x4,(byte)0x5}); - stats = new UnicodeString.UnicodeRecordStats(); - s.getRecordSize(stats); - assertEquals(26, stats.recordSize); + confirmSize(26, s); //Test a uncompressed small string that has rich text and extended text + s.setString(STR_16_BIT); s.setOptionFlags((byte)0xD); - stats = new UnicodeString.UnicodeRecordStats(); - s.getRecordSize(stats); - assertEquals(30, stats.recordSize); + confirmSize(30, s); } public void testPerfectStringSize() { //Test a basic string - UnicodeString s = makeUnicodeString(SSTRecord.MAX_RECORD_SIZE-2-1); - UnicodeString.UnicodeRecordStats stats = new UnicodeString.UnicodeRecordStats(); - s.getRecordSize(stats); - assertEquals(SSTRecord.MAX_RECORD_SIZE, stats.recordSize); + UnicodeString s = makeUnicodeString(MAX_DATA_SIZE-2-1); + confirmSize(MAX_DATA_SIZE, s); //Test an uncompressed string //Note that we can only ever get to a maximim size of 8227 since an uncompressed //string is writing double bytes. - s = makeUnicodeString((SSTRecord.MAX_RECORD_SIZE-2-1)/2); + s = makeUnicodeString((MAX_DATA_SIZE-2-1)/2, true); s.setOptionFlags((byte)0x1); - stats = new UnicodeString.UnicodeRecordStats(); - s.getRecordSize(stats); - assertEquals(SSTRecord.MAX_RECORD_SIZE-1, stats.recordSize); + confirmSize(MAX_DATA_SIZE-1, s); } public void testPerfectRichStringSize() { //Test a rich text string - UnicodeString s = makeUnicodeString(SSTRecord.MAX_RECORD_SIZE-2-1-8-2); + UnicodeString s = makeUnicodeString(MAX_DATA_SIZE-2-1-8-2); s.addFormatRun(new UnicodeString.FormatRun((short)1,(short)0)); s.addFormatRun(new UnicodeString.FormatRun((short)2,(short)1)); - UnicodeString.UnicodeRecordStats stats = new UnicodeString.UnicodeRecordStats(); s.setOptionFlags((byte)0x8); - s.getRecordSize(stats); - assertEquals(SSTRecord.MAX_RECORD_SIZE, stats.recordSize); + confirmSize(MAX_DATA_SIZE, s); //Test an uncompressed rich text string - //Note that we can only ever get to a maximim size of 8227 since an uncompressed + //Note that we can only ever get to a maximum size of 8227 since an uncompressed //string is writing double bytes. - s = makeUnicodeString((SSTRecord.MAX_RECORD_SIZE-2-1-8-2)/2); + s = makeUnicodeString((MAX_DATA_SIZE-2-1-8-2)/2, true); s.addFormatRun(new UnicodeString.FormatRun((short)1,(short)0)); s.addFormatRun(new UnicodeString.FormatRun((short)2,(short)1)); s.setOptionFlags((byte)0x9); - stats = new UnicodeString.UnicodeRecordStats(); - s.getRecordSize(stats); - assertEquals(SSTRecord.MAX_RECORD_SIZE-1, stats.recordSize); + confirmSize(MAX_DATA_SIZE-1, s); } public void testContinuedStringSize() { //Test a basic string - UnicodeString s = makeUnicodeString(SSTRecord.MAX_RECORD_SIZE-2-1+20); - UnicodeString.UnicodeRecordStats stats = new UnicodeString.UnicodeRecordStats(); - s.getRecordSize(stats); - assertEquals(SSTRecord.MAX_RECORD_SIZE+4+1+20, stats.recordSize); + UnicodeString s = makeUnicodeString(MAX_DATA_SIZE-2-1+20); + confirmSize(MAX_DATA_SIZE+4+1+20, s); } /** Tests that a string size calculation that fits neatly in two records, the second being a continue*/ public void testPerfectContinuedStringSize() { //Test a basic string - int strSize = SSTRecord.MAX_RECORD_SIZE*2; + int strSize = MAX_DATA_SIZE*2; //String overhead strSize -= 3; //Continue Record overhead @@ -131,25 +139,29 @@ public final class TestUnicodeString extends TestCase { //Continue Record additional byte overhead strSize -= 1; UnicodeString s = makeUnicodeString(strSize); - UnicodeString.UnicodeRecordStats stats = new UnicodeString.UnicodeRecordStats(); - s.getRecordSize(stats); - assertEquals(SSTRecord.MAX_RECORD_SIZE*2, stats.recordSize); + confirmSize(MAX_DATA_SIZE*2, s); } - - - private static UnicodeString makeUnicodeString( String s ) - { + private static UnicodeString makeUnicodeString(String s) { UnicodeString st = new UnicodeString(s); st.setOptionFlags((byte)0); return st; } - private static UnicodeString makeUnicodeString( int numChars) { + private static UnicodeString makeUnicodeString(int numChars) { + return makeUnicodeString(numChars, false); + } + /** + * @param is16Bit if <code>true</code> the created string will have characters > 0x00FF + * @return a string of the specified number of characters + */ + private static UnicodeString makeUnicodeString(int numChars, boolean is16Bit) { StringBuffer b = new StringBuffer(numChars); + int charBase = is16Bit ? 0x8A00 : 'A'; for (int i=0;i<numChars;i++) { - b.append(i%10); + char ch = (char) ((i%16)+charBase); + b.append(ch); } return makeUnicodeString(b.toString()); } diff --git a/src/testcases/org/apache/poi/hssf/record/constant/TestConstantValueParser.java b/src/testcases/org/apache/poi/hssf/record/constant/TestConstantValueParser.java index a72d0e1aed..6ed2c6245a 100644 --- a/src/testcases/org/apache/poi/hssf/record/constant/TestConstantValueParser.java +++ b/src/testcases/org/apache/poi/hssf/record/constant/TestConstantValueParser.java @@ -22,7 +22,6 @@ import java.util.Arrays; import junit.framework.TestCase; import org.apache.poi.hssf.record.TestcaseRecordInputStream; -import org.apache.poi.hssf.record.UnicodeString; import org.apache.poi.hssf.usermodel.HSSFErrorConstants; import org.apache.poi.util.HexRead; import org.apache.poi.util.LittleEndianByteArrayOutputStream; @@ -36,7 +35,7 @@ public final class TestConstantValueParser extends TestCase { Boolean.TRUE, null, new Double(1.1), - new UnicodeString("Sample text"), + "Sample text", ErrorConstant.valueOf(HSSFErrorConstants.ERROR_DIV_0), }; private static final byte[] SAMPLE_ENCODING = HexRead.readFromString( diff --git a/src/testcases/org/apache/poi/hssf/record/formula/TestArrayPtg.java b/src/testcases/org/apache/poi/hssf/record/formula/TestArrayPtg.java index 94a16b3829..5dab4cca14 100644 --- a/src/testcases/org/apache/poi/hssf/record/formula/TestArrayPtg.java +++ b/src/testcases/org/apache/poi/hssf/record/formula/TestArrayPtg.java @@ -66,10 +66,10 @@ public final class TestArrayPtg extends TestCase { assertEquals(Boolean.TRUE, values[0][0]); - assertEquals(new UnicodeString("ABCD"), values[0][1]); + assertEquals("ABCD", values[0][1]); assertEquals(new Double(0), values[1][0]); assertEquals(Boolean.FALSE, values[1][1]); - assertEquals(new UnicodeString("FG"), values[1][2]); + assertEquals("FG", values[1][2]); byte[] outBuf = new byte[ENCODED_CONSTANT_DATA.length]; ptg.writeTokenValueBytes(new LittleEndianByteArrayOutputStream(outBuf, 0)); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java index 30f63ab4a2..abb9f1b410 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java @@ -28,8 +28,8 @@ import org.apache.poi.hssf.model.Sheet; import org.apache.poi.hssf.util.HSSFColor; /** - * Tests various functionity having to do with HSSFCell. For instance support for - * paticular datatypes, etc. + * Tests various functionality having to do with {@link HSSFCell}. For instance support for + * particular datatypes, etc. * @author Andrew C. Oliver (andy at superlinksoftware dot com) * @author Dan Sherman (dsherman at isisph.com) * @author Alex Jacoby (ajacoby at gmail.com) @@ -345,41 +345,82 @@ public final class TestHSSFCell extends TestCase { } } - /** - * Test to ensure we can only assign cell styles that belong - * to our workbook, and not those from other workbooks. - */ - public void testCellStyleWorkbookMatch() throws Exception { - HSSFWorkbook wbA = new HSSFWorkbook(); - HSSFWorkbook wbB = new HSSFWorkbook(); - - HSSFCellStyle styA = wbA.createCellStyle(); - HSSFCellStyle styB = wbB.createCellStyle(); - - styA.verifyBelongsToWorkbook(wbA); - styB.verifyBelongsToWorkbook(wbB); - try { - styA.verifyBelongsToWorkbook(wbB); - fail(); - } catch(IllegalArgumentException e) {} - try { - styB.verifyBelongsToWorkbook(wbA); - fail(); - } catch(IllegalArgumentException e) {} - - HSSFCell cellA = wbA.createSheet().createRow(0).createCell(0); - HSSFCell cellB = wbB.createSheet().createRow(0).createCell(0); - - cellA.setCellStyle(styA); - cellB.setCellStyle(styB); - try { - cellA.setCellStyle(styB); - fail(); - } catch(IllegalArgumentException e) {} - try { - cellB.setCellStyle(styA); - fail(); - } catch(IllegalArgumentException e) {} - } + /** + * Test to ensure we can only assign cell styles that belong + * to our workbook, and not those from other workbooks. + */ + public void testCellStyleWorkbookMatch() { + HSSFWorkbook wbA = new HSSFWorkbook(); + HSSFWorkbook wbB = new HSSFWorkbook(); + + HSSFCellStyle styA = wbA.createCellStyle(); + HSSFCellStyle styB = wbB.createCellStyle(); + + styA.verifyBelongsToWorkbook(wbA); + styB.verifyBelongsToWorkbook(wbB); + try { + styA.verifyBelongsToWorkbook(wbB); + fail(); + } catch (IllegalArgumentException e) {} + try { + styB.verifyBelongsToWorkbook(wbA); + fail(); + } catch (IllegalArgumentException e) {} + + HSSFCell cellA = wbA.createSheet().createRow(0).createCell(0); + HSSFCell cellB = wbB.createSheet().createRow(0).createCell(0); + + cellA.setCellStyle(styA); + cellB.setCellStyle(styB); + try { + cellA.setCellStyle(styB); + fail(); + } catch (IllegalArgumentException e) {} + try { + cellB.setCellStyle(styA); + fail(); + } catch (IllegalArgumentException e) {} + } + + public void testChangeTypeStringToBool() { + HSSFCell cell = new HSSFWorkbook().createSheet("Sheet1").createRow(0).createCell(0); + + cell.setCellValue(new HSSFRichTextString("TRUE")); + assertEquals(HSSFCell.CELL_TYPE_STRING, cell.getCellType()); + try { + cell.setCellType(HSSFCell.CELL_TYPE_BOOLEAN); + } catch (ClassCastException e) { + throw new AssertionFailedError( + "Identified bug in conversion of cell from text to boolean"); + } + + assertEquals(HSSFCell.CELL_TYPE_BOOLEAN, cell.getCellType()); + assertEquals(true, cell.getBooleanCellValue()); + cell.setCellType(HSSFCell.CELL_TYPE_STRING); + assertEquals("TRUE", cell.getRichStringCellValue().getString()); + + // 'false' text to bool and back + cell.setCellValue(new HSSFRichTextString("FALSE")); + cell.setCellType(HSSFCell.CELL_TYPE_BOOLEAN); + assertEquals(HSSFCell.CELL_TYPE_BOOLEAN, cell.getCellType()); + assertEquals(false, cell.getBooleanCellValue()); + cell.setCellType(HSSFCell.CELL_TYPE_STRING); + assertEquals("FALSE", cell.getRichStringCellValue().getString()); + } + + public void testChangeTypeBoolToString() { + HSSFCell cell = new HSSFWorkbook().createSheet("Sheet1").createRow(0).createCell(0); + cell.setCellValue(true); + try { + cell.setCellType(HSSFCell.CELL_TYPE_STRING); + } catch (IllegalStateException e) { + if (e.getMessage().equals("Cannot get a text value from a boolean cell")) { + throw new AssertionFailedError( + "Identified bug in conversion of cell from boolean to text"); + } + throw e; + } + assertEquals("TRUE", cell.getRichStringCellValue().getString()); + } } |