]> source.dussan.org Git - poi.git/commitdiff
Adjust comments and add slightly more test-coverage
authorDominik Stadler <centic@apache.org>
Sat, 26 Oct 2019 05:26:52 +0000 (05:26 +0000)
committerDominik Stadler <centic@apache.org>
Sat, 26 Oct 2019 05:26:52 +0000 (05:26 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1868982 13f79535-47bb-0310-9956-ffa450edef68

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

index 3d95698134f922e45ae1ddb1be4f31572ae4accb..66f5e7c535d3d4a33c424ce7d9683ec43b68524f 100644 (file)
@@ -61,13 +61,15 @@ public class FractionFormat extends Format {
     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 Format {
                         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 Format {
             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);
index dd08f6d7992fbbbf9a2f4f91484c73671fb9e817..95e2947007615e7c1d0e01f4e403601f6ac939d8 100644 (file)
@@ -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));