From 14812bee45f28ae07d32da4873ee434a7210224e Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sat, 26 Oct 2019 05:26:52 +0000 Subject: [PATCH] Adjust comments and add slightly more test-coverage git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1868982 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/usermodel/FractionFormat.java | 13 +++++++--- .../poi/ss/usermodel/TestFractionFormat.java | 26 ++++++++++++++++--- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/java/org/apache/poi/ss/usermodel/FractionFormat.java b/src/java/org/apache/poi/ss/usermodel/FractionFormat.java index 3d95698134..66f5e7c535 100644 --- a/src/java/org/apache/poi/ss/usermodel/FractionFormat.java +++ b/src/java/org/apache/poi/ss/usermodel/FractionFormat.java @@ -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); diff --git a/src/testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java b/src/testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java index dd08f6d799..95e2947007 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java +++ b/src/testcases/org/apache/poi/ss/usermodel/TestFractionFormat.java @@ -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)); -- 2.39.5