poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cen...@apache.org
Subject svn commit: r1874950 - in /poi/trunk/src: java/org/apache/poi/ss/format/CellNumberFormatter.java testcases/org/apache/poi/hssf/usermodel/TestUnfixedBugs.java testcases/org/apache/poi/ss/format/TestCellFormat.java
Date Sat, 07 Mar 2020 15:33:53 GMT
Author: centic
Date: Sat Mar  7 15:33:53 2020
New Revision: 1874950

URL: http://svn.apache.org/viewvc?rev=1874950&view=rev
Log:
Fix incorrect handling of format which should not produce any digit for zero

Also include the internally computed format-string when the resulting format causes an exception

One other case with question marks is still not handled correctly, though

Modified:
    poi/trunk/src/java/org/apache/poi/ss/format/CellNumberFormatter.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestUnfixedBugs.java
    poi/trunk/src/testcases/org/apache/poi/ss/format/TestCellFormat.java

Modified: poi/trunk/src/java/org/apache/poi/ss/format/CellNumberFormatter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/format/CellNumberFormatter.java?rev=1874950&r1=1874949&r2=1874950&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/format/CellNumberFormatter.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/format/CellNumberFormatter.java Sat Mar  7 15:33:53
2020
@@ -22,6 +22,7 @@ import java.text.FieldPosition;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Formatter;
+import java.util.IllegalFormatException;
 import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;
@@ -232,15 +233,15 @@ public class CellNumberFormatter extends
         integerSpecials.addAll(specials.subList(0, integerEnd()));
 
         if (exponent == null) {
-            StringBuilder fmtBuf = new StringBuilder("%");
-
             int integerPartWidth = calculateIntegerPartWidth();
             int totalWidth = integerPartWidth + fractionPartWidth;
 
-            fmtBuf.append('0').append(totalWidth).append('.').append(precision);
-
-            fmtBuf.append("f");
-            printfFmt = fmtBuf.toString();
+            // need to handle empty width specially as %00.0f fails during formatting
+            if(totalWidth == 0) {
+                printfFmt = "";
+            } else {
+                printfFmt = "%0" + totalWidth + '.' + precision + "f";
+            }
             decimalFmt = null;
         } else {
             StringBuffer fmtBuf = new StringBuffer();
@@ -278,7 +279,7 @@ public class CellNumberFormatter extends
     private DecimalFormatSymbols getDecimalFormatSymbols() {
         return DecimalFormatSymbols.getInstance(locale);
     }
-    
+
     private static void placeZeros(StringBuffer sb, List<Special> specials) {
         for (Special s : specials) {
             if (isDigitFmt(s)) {
@@ -458,6 +459,8 @@ public class CellNumberFormatter extends
             StringBuffer result = new StringBuffer();
             try (Formatter f = new Formatter(result, locale)) {
                 f.format(locale, printfFmt, value);
+            } catch (IllegalFormatException e) {
+                throw new IllegalArgumentException("Format: " + printfFmt, e);
             }
 
             if (numerator == null) {
@@ -695,7 +698,7 @@ public class CellNumberFormatter extends
             LOG.log(POILogger.ERROR, "error while fraction evaluation", ignored);
         }
     }
-    
+
     private String localiseFormat(String format) {
         DecimalFormatSymbols dfs = getDecimalFormatSymbols();
         if(format.contains(",") && dfs.getGroupingSeparator() != ',') {
@@ -711,8 +714,8 @@ public class CellNumberFormatter extends
         }
         return format;
     }
-    
-    
+
+
     private static String replaceLast(String text, String regex, String replacement) {
         return text.replaceFirst("(?s)(.*)" + regex, "$1" + replacement);
     }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestUnfixedBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestUnfixedBugs.java?rev=1874950&r1=1874949&r2=1874950&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestUnfixedBugs.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestUnfixedBugs.java Sat Mar  7
15:33:53 2020
@@ -24,6 +24,7 @@ import java.io.IOException;
 
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.hssf.util.HSSFColor;
+import org.apache.poi.ss.format.CellFormat;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
@@ -32,7 +33,7 @@ import org.junit.Test;
 
 /**
  * @author aviks
- * 
+ *
  * This testcase contains tests for bugs that are yet to be fixed. Therefore,
  * the standard ant test target does not run these tests. Run this testcase with
  * the single-test target. The names of the tests usually correspond to the
@@ -94,7 +95,7 @@ public final class TestUnfixedBugs {
         Sheet sheet = wb.getSheet("Sheet1");
         Row row = sheet.getRow(0);
         Cell cell = row.getCell(0);
-        
+
         HSSFColor bgColor = (HSSFColor) cell.getCellStyle().getFillBackgroundColorColor();
         String bgColorStr = bgColor.getTriplet()[0]+", "+bgColor.getTriplet()[1]+", "+bgColor.getTriplet()[2];
         //System.out.println(bgColorStr);
@@ -106,4 +107,24 @@ public final class TestUnfixedBugs {
         assertEquals("0, 128, 128", fontColorStr);
         wb.close();
     }
+
+    @Test
+    public void testDataFormattingWithQuestionMark() {
+        // The question mark in the format should be replaced by blanks, but
+        // this is currently not handled when producing the Java formatting and
+        // so we end up with a trailing zero here
+        CellFormat cfUK  = CellFormat.getInstance("??");
+        assertEquals("  ", cfUK.apply((double) 0).text);
+    }
+
+    @Test
+    public void testDataFormattingWithQuestionMarkAndPound() {
+        char pound = '\u00A3';
+
+        // The question mark in the format should be replaced by blanks, but
+        // this is currently not handled when producing the Java formatting and
+        // so we end up with a trailing zero here
+        CellFormat cfUK  = CellFormat.getInstance("_-[$£-809]* \"-\"??_-");
+        assertEquals(" "+pound+"   -  ", cfUK.apply((double) 0).text);
+    }
 }

Modified: poi/trunk/src/testcases/org/apache/poi/ss/format/TestCellFormat.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/format/TestCellFormat.java?rev=1874950&r1=1874949&r2=1874950&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/format/TestCellFormat.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/format/TestCellFormat.java Sat Mar  7 15:33:53
2020
@@ -943,34 +943,33 @@ public class TestCellFormat {
 
         // For +ve numbers, should be Space + currency symbol + spaces + whole number with
commas + space
         // (Except French, which is mostly reversed...)
-        assertEquals(" $   12 ", cfDft.apply(Double.valueOf(12.33)).text);
-        assertEquals(" $   12 ",  cfUS.apply(Double.valueOf(12.33)).text);
-        assertEquals(" "+pound+"   12 ", cfUK.apply(Double.valueOf(12.33)).text);
-        assertEquals(" 12   "+euro+" ", cfFR.apply(Double.valueOf(12.33)).text);
-
-        assertEquals(" $   16,789 ", cfDft.apply(Double.valueOf(16789.2)).text);
-        assertEquals(" $   16,789 ",  cfUS.apply(Double.valueOf(16789.2)).text);
-        assertEquals(" "+pound+"   16,789 ", cfUK.apply(Double.valueOf(16789.2)).text);
-        assertEquals(" 16,789   "+euro+" ", cfFR.apply(Double.valueOf(16789.2)).text);
+        assertEquals(" $   12 ", cfDft.apply(12.33).text);
+        assertEquals(" $   12 ",  cfUS.apply(12.33).text);
+        assertEquals(" "+pound+"   12 ", cfUK.apply(12.33).text);
+        assertEquals(" 12   "+euro+" ", cfFR.apply(12.33).text);
+
+        assertEquals(" $   16,789 ", cfDft.apply(16789.2).text);
+        assertEquals(" $   16,789 ",  cfUS.apply(16789.2).text);
+        assertEquals(" "+pound+"   16,789 ", cfUK.apply(16789.2).text);
+        assertEquals(" 16,789   "+euro+" ", cfFR.apply(16789.2).text);
 
         // For -ve numbers, gets a bit more complicated...
-        assertEquals("-$   12 ", cfDft.apply(Double.valueOf(-12.33)).text);
-        assertEquals(" $   -12 ",  cfUS.apply(Double.valueOf(-12.33)).text);
-        assertEquals("-"+pound+"   12 ", cfUK.apply(Double.valueOf(-12.33)).text);
-        assertEquals("-12   "+euro+" ", cfFR.apply(Double.valueOf(-12.33)).text);
-
-        assertEquals("-$   16,789 ", cfDft.apply(Double.valueOf(-16789.2)).text);
-        assertEquals(" $   -16,789 ",  cfUS.apply(Double.valueOf(-16789.2)).text);
-        assertEquals("-"+pound+"   16,789 ", cfUK.apply(Double.valueOf(-16789.2)).text);
-        assertEquals("-16,789   "+euro+" ", cfFR.apply(Double.valueOf(-16789.2)).text);
+        assertEquals("-$   12 ", cfDft.apply(-12.33).text);
+        assertEquals(" $   -12 ",  cfUS.apply(-12.33).text);
+        assertEquals("-"+pound+"   12 ", cfUK.apply(-12.33).text);
+        assertEquals("-12   "+euro+" ", cfFR.apply(-12.33).text);
+
+        assertEquals("-$   16,789 ", cfDft.apply(-16789.2).text);
+        assertEquals(" $   -16,789 ",  cfUS.apply(-16789.2).text);
+        assertEquals("-"+pound+"   16,789 ", cfUK.apply(-16789.2).text);
+        assertEquals("-16,789   "+euro+" ", cfFR.apply(-16789.2).text);
 
         // For zero, should be Space + currency symbol + spaces + Minus + spaces
-        assertEquals(" $   - ", cfDft.apply(Double.valueOf(0)).text);
-        // TODO Fix the exception this incorrectly triggers
-        //assertEquals(" $   - ",  cfUS.apply(Double.valueOf(0)).text);
+        assertEquals(" $   - ", cfDft.apply((double) 0).text);
+        assertEquals(" $   - ", cfUS.apply((double) 0).text);
         // TODO Fix these to not have an incorrect bonus 0 on the end
-        //assertEquals(" "+pound+"   -  ", cfUK.apply(Double.valueOf(0)).text);
-        //assertEquals(" -    "+euro+"  ", cfFR.apply(Double.valueOf(0)).text);
+        //assertEquals(" "+pound+"   -  ", cfUK.apply((double) 0).text);
+        //assertEquals(" -    "+euro+"  ", cfFR.apply((double) 0).text);
     }
 
     @Test



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message