From: Dominik Stadler Date: Sat, 12 Mar 2016 16:56:26 +0000 (+0000) Subject: Fix bug 56549: correctly calculate char index ranges for HWPF in the TextPieceTable X-Git-Tag: REL_3_15_BETA2~456 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2b23bf233e3b0f908f1b52f8c4ddd0ac34abc8af;p=poi.git Fix bug 56549: correctly calculate char index ranges for HWPF in the TextPieceTable git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734718 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java b/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java index 11a8d604d9..baeee0c061 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/TextPieceTable.java @@ -259,6 +259,7 @@ public class TextPieceTable implements CharIndexTranslator return charCount; } + @Override public int[][] getCharIndexRanges( int startBytePosInclusive, int endBytePosExclusive ) { @@ -271,7 +272,7 @@ public class TextPieceTable implements CharIndexTranslator + textPiece.bytesLength(); if ( startBytePosInclusive > tpEnd ) continue; - if ( endBytePosExclusive < tpStart ) + if ( endBytePosExclusive <= tpStart ) break; final int rangeStartBytes = Math.max( tpStart, diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestTextPieceTable.java b/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestTextPieceTable.java index ceecc0f0c5..5f57d1f6d0 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestTextPieceTable.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/model/TestTextPieceTable.java @@ -26,13 +26,38 @@ import org.apache.poi.hwpf.HWPFDocFixture; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.HWPFTestDataSamples; import org.apache.poi.hwpf.model.io.HWPFFileSystem; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; @SuppressWarnings("deprecation") -public final class TestTextPieceTable extends TestCase { +public final class TestTextPieceTable { private HWPFDocFixture _hWPFDocFixture; - // private String dirname; + @Before + public void setUp() throws Exception { + System.setProperty("org.apache.poi.hwpf.preserveTextTable", + Boolean.TRUE.toString()); + + _hWPFDocFixture = new HWPFDocFixture(this, + HWPFDocFixture.DEFAULT_TEST_FILE); + _hWPFDocFixture.setUp(); + } + + @After + public void tearDown() throws Exception { + _hWPFDocFixture.tearDown(); + _hWPFDocFixture = null; + + System.setProperty("org.apache.poi.hwpf.preserveTextTable", + Boolean.FALSE.toString()); + } + @Test public void testReadWrite() throws Exception { FileInformationBlock fib = _hWPFDocFixture._fib; byte[] mainStream = _hWPFDocFixture._mainStream; @@ -63,6 +88,7 @@ public final class TestTextPieceTable extends TestCase { /** * Check that we do the positions correctly when working with pure-ascii */ + @Test public void testAsciiParts() throws Exception { HWPFDocument doc = HWPFTestDataSamples .openSampleFile("ThreeColHeadFoot.doc"); @@ -98,6 +124,7 @@ public final class TestTextPieceTable extends TestCase { * Check that we do the positions correctly when working with a mix ascii, * unicode file */ + @Test public void testUnicodeParts() throws Exception { HWPFDocument doc = HWPFTestDataSamples .openSampleFile("HeaderFooterUnicode.doc"); @@ -164,25 +191,61 @@ public final class TestTextPieceTable extends TestCase { return new HWPFDocument(new ByteArrayInputStream(baos.toByteArray())); } - @Override - protected void setUp() throws Exception { - super.setUp(); - System.setProperty("org.apache.poi.hwpf.preserveTextTable", - Boolean.TRUE.toString()); + @Test + public void test56549_CharIndexRange() { + HWPFDocument doc = HWPFTestDataSamples.openSampleFile("ThreeColHeadFoot.doc"); - _hWPFDocFixture = new HWPFDocFixture(this, - HWPFDocFixture.DEFAULT_TEST_FILE); - _hWPFDocFixture.setUp(); - } + // there is one range from 2048 - 2387 - @Override - protected void tearDown() throws Exception { - _hWPFDocFixture.tearDown(); - _hWPFDocFixture = null; + TextPieceTable tbl = doc.getTextTable(); + int[][] range = tbl.getCharIndexRanges(0, 0); + assertEquals(0, range.length); - System.setProperty("org.apache.poi.hwpf.preserveTextTable", - Boolean.FALSE.toString()); - super.tearDown(); - } + range = tbl.getCharIndexRanges(0, 1); + assertEquals(0, range.length); + + range = tbl.getCharIndexRanges(0, 338); + assertEquals(0, range.length); + + range = tbl.getCharIndexRanges(0, 339); + assertEquals(0, range.length); + + range = tbl.getCharIndexRanges(0, 340); + assertEquals(0, range.length); + range = tbl.getCharIndexRanges(2030, 2048); + assertEquals(0, range.length); + + range = tbl.getCharIndexRanges(2030, 2049); + assertEquals(1, range.length); + assertArrayEquals(new int[] {0,1}, range[0]); + + range = tbl.getCharIndexRanges(2048, 2049); + assertEquals(1, range.length); + assertArrayEquals(new int[] {0,1}, range[0]); + + range = tbl.getCharIndexRanges(2048, 2300); + assertEquals(1, range.length); + assertArrayEquals(new int[] {0,252}, range[0]); + + range = tbl.getCharIndexRanges(2049, 2300); + assertEquals(1, range.length); + assertArrayEquals(new int[] {1,252}, range[0]); + + range = tbl.getCharIndexRanges(2049, 2300); + assertEquals(1, range.length); + assertArrayEquals(new int[] {1,252}, range[0]); + + range = tbl.getCharIndexRanges(2049, 2387); + assertEquals(1, range.length); + assertArrayEquals(new int[] {1,339}, range[0]); + + range = tbl.getCharIndexRanges(2049, 2388); + assertEquals(1, range.length); + assertArrayEquals(new int[] {1,339}, range[0]); + + range = tbl.getCharIndexRanges(2387, 2388); + assertEquals(1, range.length); + assertArrayEquals(new int[] {339,339}, range[0]); + } }