You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2019/10/26 05:26:52 UTC

svn commit: r1868982 - in /poi/trunk/src: java/org/apache/poi/ss/usermodel/FractionFormat.java testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java

Author: centic
Date: Sat Oct 26 05:26:52 2019
New Revision: 1868982

URL: http://svn.apache.org/viewvc?rev=1868982&view=rev
Log:
Adjust comments and add slightly more test-coverage

Modified:
    poi/trunk/src/java/org/apache/poi/ss/usermodel/FractionFormat.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java

Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/FractionFormat.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/FractionFormat.java?rev=1868982&r1=1868981&r2=1868982&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/FractionFormat.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/FractionFormat.java Sat Oct 26 05:26:52 2019
@@ -61,13 +61,15 @@ public class FractionFormat extends Form
     private final int maxDenom;
 
     private final String wholePartFormatString;
+
     /**
      * Single parameter ctor
      * @param denomFormatString The format string for the denominator
      */
     public FractionFormat(String wholePartFormatString, String denomFormatString) {
         this.wholePartFormatString = wholePartFormatString;
-        //init exactDenom and maxDenom
+
+        // initialize exactDenom and maxDenom
         Matcher m = DENOM_FORMAT_PATTERN.matcher(denomFormatString);
         int tmpExact = -1;
         int tmpMax = -1;
@@ -81,7 +83,10 @@ public class FractionFormat extends Form
                         tmpExact = -1;
                     }
                 } catch (NumberFormatException e){
-                    //should never happen
+                    // should not happen because the pattern already verifies that this is a number,
+                    // but a number larger than Integer.MAX_VALUE can cause it,
+                    // so throw an exception if we somehow end up here
+                    throw new IllegalStateException(e);
                 }
             } else if (m.group(1) != null) {
                 int len = m.group(1).length();
@@ -132,8 +137,8 @@ public class FractionFormat extends Form
             return sb.toString();
         }
         
-        SimpleFraction fract = null;
-        try{
+        final SimpleFraction fract;
+        try {
             //this should be the case because of the constructor
             if (exactDenom > 0){
                 fract = SimpleFraction.buildFractionExactDenominator(decPart.doubleValue(), exactDenom);

Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java?rev=1868982&r1=1868981&r2=1868982&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java Sat Oct 26 05:26:52 2019
@@ -27,6 +27,7 @@ import java.io.InputStreamReader;
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
 import org.apache.poi.util.LocaleUtil;
+import org.junit.Ignore;
 import org.junit.Test;
 
 /**
@@ -35,15 +36,34 @@ import org.junit.Test;
  */
 public final class TestFractionFormat {
     @Test
-    public void testSingle() throws Exception {
+    public void testSingle() {
         FractionFormat f = new FractionFormat("", "##");
         double val = 321.321;
         String ret = f.format(val);
         assertEquals("26027/81", ret);
     }
-    
+
+    @Test(expected = IllegalStateException.class)
+    public void testInvalid() {
+        FractionFormat f = new FractionFormat("", "9999999999999999999999999999");
+        double val = 321.321;
+        String ret = f.format(val);
+        assertEquals("26027/81", ret);
+    }
+
+    @Ignore("Runs for some longer time")
+    @Test
+    public void microBenchmark() {
+        FractionFormat f = new FractionFormat("", "##");
+        double val = 321.321;
+        for(int i = 0;i < 1000000;i++) {
+            String ret = f.format(val);
+            assertEquals("26027/81", ret);
+        }
+    }
+
     @Test
-    public void testWithBigWholePart() throws Exception {
+    public void testWithBigWholePart() {
         FractionFormat f = new FractionFormat("#", "???/???");
         
         assertEquals("10100136259702", f.format(10100136259702d));



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