diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java index c58795075..13a1de914 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java @@ -135,9 +135,10 @@ public class XSSFRichTextString implements RichTextString { */ public void applyFont(int startIndex, int endIndex, Font font) { if (startIndex > endIndex) - throw new IllegalArgumentException("Start index must be less than end index."); + throw new IllegalArgumentException("Start index must be less than end index, but had " + startIndex + " and " + endIndex); if (startIndex < 0 || endIndex > length()) - throw new IllegalArgumentException("Start and end index not in range."); + throw new IllegalArgumentException("Start and end index not in range, but had " + startIndex + " and " + endIndex); + if (startIndex == endIndex) return; diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java index 3daa570b9..8edf1930e 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java @@ -25,6 +25,7 @@ import junit.framework.TestCase; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.XSSFTestDataSamples; +import org.apache.poi.xssf.model.StylesTable; import org.junit.Test; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; @@ -38,7 +39,6 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring; public final class TestXSSFRichTextString extends TestCase { public void testCreate() { - XSSFRichTextString rt = new XSSFRichTextString("Apache POI"); assertEquals("Apache POI", rt.getString()); @@ -53,9 +53,14 @@ public final class TestXSSFRichTextString extends TestCase { assertEquals("Apache POI is cool stuff", rt.getString()); } + public void testEmpty() { + XSSFRichTextString rt = new XSSFRichTextString(); + assertEquals(0, rt.getIndexOfFormattingRun(9999)); + assertEquals(-1, rt.getLengthOfFormattingRun(9999)); + assertNull(rt.getFontAtIndex(9999)); + } public void testApplyFont() { - XSSFRichTextString rt = new XSSFRichTextString(); rt.append("123"); rt.append("4567"); @@ -82,6 +87,65 @@ public final class TestXSSFRichTextString extends TestCase { assertEquals(7, rt.getIndexOfFormattingRun(3)); assertEquals(2, rt.getLengthOfFormattingRun(3)); assertEquals("89", rt.getCTRst().getRArray(3).getT()); + + + assertEquals(-1, rt.getIndexOfFormattingRun(9999)); + assertEquals(-1, rt.getLengthOfFormattingRun(9999)); + assertNull(rt.getFontAtIndex(9999)); + } + + public void testApplyFontIndex() { + XSSFRichTextString rt = new XSSFRichTextString("Apache POI"); + rt.applyFont(0, 10, (short)1); + + rt.applyFont((short)1); + + assertNotNull(rt.getFontAtIndex(0)); + } + + public void testApplyFontWithStyles() { + XSSFRichTextString rt = new XSSFRichTextString("Apache POI"); + + StylesTable tbl = new StylesTable(); + rt.setStylesTableReference(tbl); + + try { + rt.applyFont(0, 10, (short)1); + fail("Fails without styles in the table"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + tbl.putFont(new XSSFFont()); + rt.applyFont(0, 10, (short)1); + rt.applyFont((short)1); + } + + public void testApplyFontException() { + XSSFRichTextString rt = new XSSFRichTextString("Apache POI"); + + rt.applyFont(0, 0, (short)1); + + try { + rt.applyFont(11, 10, (short)1); + fail("Should catch Exception here"); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("11")); + } + + try { + rt.applyFont(-1, 10, (short)1); + fail("Should catch Exception here"); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("-1")); + } + + try { + rt.applyFont(0, 555, (short)1); + fail("Should catch Exception here"); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("555")); + } } public void testClearFormatting() { @@ -431,4 +495,13 @@ public final class TestXSSFRichTextString extends TestCase { assertNotNull(rt.getFontOfFormattingRun(2)); assertEquals(9, rt.getLengthOfFormattingRun(2)); } + + public void testToString() { + XSSFRichTextString rt = new XSSFRichTextString("Apache POI"); + assertNotNull(rt.toString()); + + // TODO: normally toString() should never return null, should we adjust this? + rt = new XSSFRichTextString(); + assertNull(rt.toString()); + } }