]> source.dussan.org Git - poi.git/commitdiff
Bug 63057: made SXSSFCell.setCellValue(String|RichTextString) exception-safe.
authorVladislav Galas <gallon@apache.org>
Fri, 4 Jan 2019 10:10:30 +0000 (10:10 +0000)
committerVladislav Galas <gallon@apache.org>
Fri, 4 Jan 2019 10:10:30 +0000 (10:10 +0000)
Also removed unused code.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1850342 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java

index ed95d88b2146262311789a5fe5805e55ec634a58..8917e60105342191e3c8812731654ca22d6b7337 100644 (file)
@@ -274,19 +274,17 @@ public class SXSSFCell implements Cell {
     @Override
     public void setCellValue(RichTextString value)
     {
-        XSSFRichTextString xvalue = (XSSFRichTextString)value;
-
-        if (xvalue != null && xvalue.getString() != null) {
-            ensureRichTextStringType();
-
-            if (xvalue.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) {
+        if (value != null && value.getString() != null) {
+            if (value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) {
                 throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
             }
 
+            ensureRichTextStringType();
+
             if(_value instanceof RichTextStringFormulaValue) {
-                ((RichTextStringFormulaValue) _value).setPreEvaluatedValue(xvalue);
+                ((RichTextStringFormulaValue) _value).setPreEvaluatedValue(value);
             } else {
-                ((RichTextValue) _value).setValue(xvalue);
+                ((RichTextValue) _value).setValue(value);
             }
         } else {
             setCellType(CellType.BLANK);
@@ -305,22 +303,17 @@ public class SXSSFCell implements Cell {
     public void setCellValue(String value)
     {
         if (value != null) {
-            ensureTypeOrFormulaType(CellType.STRING);
-            
             if (value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) {
                 throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
             }
-    
-            if(_value.getType()==CellType.FORMULA)
-                if(_value instanceof NumericFormulaValue) {
-                    ((NumericFormulaValue) _value).setPreEvaluatedValue(Double.parseDouble(value));
-                } else if(_value instanceof RichTextStringFormulaValue) {
-                    ((RichTextStringFormulaValue) _value).setPreEvaluatedValue(new XSSFRichTextString(value));
-                } else {
-                    ((StringFormulaValue) _value).setPreEvaluatedValue(value);
-                }
-            else
-                ((PlainStringValue)_value).setValue(value);
+
+            ensureTypeOrFormulaType(CellType.STRING);
+
+            if(_value.getType() == CellType.FORMULA) {
+                ((StringFormulaValue) _value).setPreEvaluatedValue(value);
+            } else {
+                ((PlainStringValue) _value).setValue(value);
+            }
         } else {
             setCellType(CellType.BLANK);
         }
index eab4222ba38840a0e94e5203c223ba7658f09547..a69916141037d9677b0bae619747639a5eb2e02e 100644 (file)
@@ -1156,4 +1156,47 @@ public abstract class BaseTestCell {
         boolean result = cell.getBooleanCellValue();
         assertFalse(result);
     }
+
+    @Test
+    public void setStringCellValue_ifThrows_shallNotChangeCell() {
+        Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
+
+        final double value = 2.78;
+        cell.setCellValue(value);
+        assertEquals(CellType.NUMERIC, cell.getCellType());
+
+        int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1;
+        String badStringValue = new String(new byte[badLength]);
+
+        try {
+            cell.setCellValue(badStringValue);
+        } catch (IllegalArgumentException e) {
+            // no-op, expected to throw but we need to assert something more
+        }
+
+        assertEquals(CellType.NUMERIC, cell.getCellType());
+        assertEquals(value, cell.getNumericCellValue(), 0);
+    }
+
+    @Test
+    public void setStringCellValueWithRichTextString_ifThrows_shallNotChangeCell() {
+        Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0);
+
+        final double value = 2.78;
+        cell.setCellValue(value);
+        assertEquals(CellType.NUMERIC, cell.getCellType());
+
+        int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1;
+        RichTextString badStringValue = cell.getSheet().getWorkbook().getCreationHelper().
+                createRichTextString(new String(new byte[badLength]));
+
+        try {
+            cell.setCellValue(badStringValue);
+        } catch (IllegalArgumentException e) {
+            // no-op, expected to throw but we need to assert something more
+        }
+
+        assertEquals(CellType.NUMERIC, cell.getCellType());
+        assertEquals(value, cell.getNumericCellValue(), 0);
+    }
 }