diff options
author | Nick Burch <nick@apache.org> | 2010-01-19 12:04:14 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2010-01-19 12:04:14 +0000 |
commit | 054f1bc28974979d2de2a6c57c321e042614aea1 (patch) | |
tree | f52c0b100088b97f5f2ad8173bc01eef4f5fcb8c /src/testcases | |
parent | d7470746e1e7a666490b6c9de117c76c78d648d2 (diff) | |
download | poi-054f1bc28974979d2de2a6c57c321e042614aea1.tar.gz poi-054f1bc28974979d2de2a6c57c321e042614aea1.zip |
Implement the ExtRst part of a UnicodeString
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@900746 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases')
3 files changed, 192 insertions, 10 deletions
diff --git a/src/testcases/org/apache/poi/hssf/record/TestSSTRecordSizeCalculator.java b/src/testcases/org/apache/poi/hssf/record/TestSSTRecordSizeCalculator.java index b171a77a13..80380ac5e2 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestSSTRecordSizeCalculator.java +++ b/src/testcases/org/apache/poi/hssf/record/TestSSTRecordSizeCalculator.java @@ -33,9 +33,8 @@ public final class TestSSTRecordSizeCalculator extends TestCase { private static final int COMPRESSED_PLAIN_STRING_OVERHEAD = 3; private static final int OPTION_FIELD_SIZE = 1; - private final IntMapper strings = new IntMapper(); + private final IntMapper<UnicodeString> strings = new IntMapper<UnicodeString>(); - private void confirmSize(int expectedSize) { ContinuableRecordOutput cro = ContinuableRecordOutput.createForCountingOnly(); SSTSerializer ss = new SSTSerializer(strings, 0, 0); diff --git a/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java b/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java index 6ecab71a59..591042d7eb 100644 --- a/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java +++ b/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java @@ -17,12 +17,19 @@ package org.apache.poi.hssf.record.common; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + import junit.framework.TestCase; import org.apache.poi.hssf.record.ContinueRecord; import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.hssf.record.SSTRecord; +import org.apache.poi.hssf.record.common.UnicodeString.ExtRst; +import org.apache.poi.hssf.record.common.UnicodeString.FormatRun; import org.apache.poi.hssf.record.cont.ContinuableRecordOutput; +import org.apache.poi.util.LittleEndianInputStream; +import org.apache.poi.util.LittleEndianOutputStream; /** * Tests that {@link UnicodeString} record size calculates correctly. The record size @@ -85,13 +92,23 @@ public final class TestUnicodeString extends TestCase { //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}); - confirmSize(26, s); + confirmSize(17, s); + + // Extended phonetics data + // Minimum size is 14 + // Also adds 4 bytes to hold the length + s.setExtendedRst( + new ExtRst() + ); + confirmSize(35, s); //Test a uncompressed small string that has rich text and extended text s.setString(STR_16_BIT); s.setOptionFlags((byte)0xD); - confirmSize(30, s); + confirmSize(39, s); + + s.setExtendedRst(null); + confirmSize(21, s); } public void testPerfectStringSize() { @@ -144,6 +161,146 @@ public final class TestUnicodeString extends TestCase { UnicodeString s = makeUnicodeString(strSize); confirmSize(MAX_DATA_SIZE*2, s); } + + public void testFormatRun() throws Exception { + FormatRun fr = new FormatRun((short)4, (short)0x15c); + assertEquals(4, fr.getCharacterPos()); + assertEquals(0x15c, fr.getFontIndex()); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + LittleEndianOutputStream out = new LittleEndianOutputStream(baos); + + fr.serialize(out); + + byte[] b = baos.toByteArray(); + assertEquals(4, b.length); + assertEquals(4, b[0]); + assertEquals(0, b[1]); + assertEquals(0x5c, b[2]); + assertEquals(0x01, b[3]); + + LittleEndianInputStream inp = new LittleEndianInputStream( + new ByteArrayInputStream(b) + ); + fr = new FormatRun(inp); + assertEquals(4, fr.getCharacterPos()); + assertEquals(0x15c, fr.getFontIndex()); + } + + public void testExtRstFromEmpty() throws Exception { + ExtRst ext = new ExtRst(); + + assertEquals(0, ext.getNumberOfRuns()); + assertEquals(0, ext.getFormattingFontIndex()); + assertEquals(0, ext.getFormattingOptions()); + assertEquals("", ext.getPhoneticText()); + assertEquals(0, ext.getPhRuns().length); + assertEquals(10, ext.getDataSize()); // Excludes 4 byte header + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + LittleEndianOutputStream out = new LittleEndianOutputStream(baos); + ContinuableRecordOutput cout = new ContinuableRecordOutput(out, 0xffff); + + ext.serialize(cout); + cout.writeContinue(); + + byte[] b = baos.toByteArray(); + assertEquals(20, b.length); + + // First 4 bytes from the outputstream + assertEquals(-1, b[0]); + assertEquals(-1, b[1]); + assertEquals(14, b[2]); + assertEquals(00, b[3]); + + // Reserved + assertEquals(1, b[4]); + assertEquals(0, b[5]); + // Data size + assertEquals(10, b[6]); + assertEquals(00, b[7]); + // Font*2 + assertEquals(0, b[8]); + assertEquals(0, b[9]); + assertEquals(0, b[10]); + assertEquals(0, b[11]); + // 0 Runs + assertEquals(0, b[12]); + assertEquals(0, b[13]); + // Size=0, *2 + assertEquals(0, b[14]); + assertEquals(0, b[15]); + assertEquals(0, b[16]); + assertEquals(0, b[17]); + + // Last 2 bytes from the outputstream + assertEquals(ContinueRecord.sid, b[18]); + assertEquals(0, b[19]); + + + // Load in again and re-test + byte[] data = new byte[14]; + System.arraycopy(b, 4, data, 0, data.length); + LittleEndianInputStream inp = new LittleEndianInputStream( + new ByteArrayInputStream(data) + ); + ext = new ExtRst(inp, data.length); + + assertEquals(0, ext.getNumberOfRuns()); + assertEquals(0, ext.getFormattingFontIndex()); + assertEquals(0, ext.getFormattingOptions()); + assertEquals("", ext.getPhoneticText()); + assertEquals(0, ext.getPhRuns().length); + } + + public void testExtRstFromData() throws Exception { + byte[] data = new byte[] { + 01, 00, 0x0C, 00, + 00, 00, 0x37, 00, + 00, 00, + 00, 00, 00, 00, + 00, 00 // Cruft at the end, as found from real files + }; + assertEquals(16, data.length); + + LittleEndianInputStream inp = new LittleEndianInputStream( + new ByteArrayInputStream(data) + ); + ExtRst ext = new ExtRst(inp, data.length); + assertEquals(0x0c, ext.getDataSize()); // Excludes 4 byte header + + assertEquals(0, ext.getNumberOfRuns()); + assertEquals(0x37, ext.getFormattingOptions()); + assertEquals(0, ext.getFormattingFontIndex()); + assertEquals("", ext.getPhoneticText()); + assertEquals(0, ext.getPhRuns().length); + } + + public void testCorruptExtRstDetection() throws Exception { + byte[] data = new byte[] { + 0x79, 0x79, 0x11, 0x11, + 0x22, 0x22, 0x33, 0x33, + }; + assertEquals(8, data.length); + + LittleEndianInputStream inp = new LittleEndianInputStream( + new ByteArrayInputStream(data) + ); + ExtRst ext = new ExtRst(inp, data.length); + + // Will be empty + assertEquals(ext, new ExtRst()); + + // If written, will be the usual size + assertEquals(10, ext.getDataSize()); // Excludes 4 byte header + + // Is empty + assertEquals(0, ext.getNumberOfRuns()); + assertEquals(0, ext.getFormattingOptions()); + assertEquals(0, ext.getFormattingFontIndex()); + assertEquals("", ext.getPhoneticText()); + assertEquals(0, ext.getPhRuns().length); + } private static UnicodeString makeUnicodeString(String s) { diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index d96ad74662..337499415b 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -36,6 +36,7 @@ import org.apache.poi.hssf.record.CellValueRecordInterface; import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord; import org.apache.poi.hssf.record.NameRecord; import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate; +import org.apache.poi.hssf.record.common.UnicodeString; import org.apache.poi.hssf.record.formula.DeletedArea3DPtg; import org.apache.poi.hssf.record.formula.Ptg; import org.apache.poi.ss.usermodel.*; @@ -1538,12 +1539,37 @@ public final class TestBugs extends BaseTestBugzillaIssues { } /** - * Round trip a file with an unusual ExtRst record + * Round trip a file with an unusual UnicodeString/ExtRst record parts */ - public void test47847() { - HSSFWorkbook wb = openSample("47251.xls"); - assertEquals(1, wb.getNumberOfSheets()); + public void test47847() throws Exception { + HSSFWorkbook wb = openSample("47847.xls"); + assertEquals(3, wb.getNumberOfSheets()); + + // Find the SST record + UnicodeString withExt = wb.getWorkbook().getSSTString(0); + UnicodeString withoutExt = wb.getWorkbook().getSSTString(31); + + assertEquals("O:Alloc:Qty", withExt.getString()); + assertTrue((withExt.getOptionFlags() & 0x0004) == 0x0004); + + assertEquals("RT", withoutExt.getString()); + assertTrue((withoutExt.getOptionFlags() & 0x0004) == 0x0000); + + // Something about continues... + + + // Write out and re-read wb = writeOutAndReadBack(wb); - assertEquals(1, wb.getNumberOfSheets()); + assertEquals(3, wb.getNumberOfSheets()); + + // Check it's the same now + withExt = wb.getWorkbook().getSSTString(0); + withoutExt = wb.getWorkbook().getSSTString(31); + + assertEquals("O:Alloc:Qty", withExt.getString()); + assertTrue((withExt.getOptionFlags() & 0x0004) == 0x0004); + + assertEquals("RT", withoutExt.getString()); + assertTrue((withoutExt.getOptionFlags() & 0x0004) == 0x0000); } } |