aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2014-04-18 20:46:57 +0000
committerNick Burch <nick@apache.org>2014-04-18 20:46:57 +0000
commita0c17162601a956d5efb683ca3375ab52b22b55c (patch)
tree50a732654d6e614719d811e02d9ba1cfc37e5883 /src
parentd9bf061db757f85fd50df1c6de22a4a742a12548 (diff)
downloadpoi-a0c17162601a956d5efb683ca3375ab52b22b55c.tar.gz
poi-a0c17162601a956d5efb683ca3375ab52b22b55c.zip
Unit test for column autosizing of % values
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1588562 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
-rw-r--r--src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java65
-rw-r--r--src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java6
2 files changed, 71 insertions, 0 deletions
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
index bc234d2a9c..a4bd4a5cf3 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
@@ -62,6 +62,7 @@ import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.Row;
@@ -2485,4 +2486,68 @@ public final class TestBugs extends BaseTestBugzillaIssues {
openSample("xor-encryption-abc.xls");
}
+ @Test
+ public void stackoverflow23114397() throws Exception {
+ Workbook wb = new HSSFWorkbook();
+ DataFormat format = wb.getCreationHelper().createDataFormat();
+
+ // How close the sizing should be, given that not all
+ // systems will have quite the same fonts on them
+ int fontAccuracy = 25;
+
+ // x%
+ CellStyle iPercent = wb.createCellStyle();
+ iPercent.setDataFormat(format.getFormat("0%"));
+ // x.x%
+ CellStyle d1Percent = wb.createCellStyle();
+ d1Percent.setDataFormat(format.getFormat("0.0%"));
+ // x.xx%
+ CellStyle d2Percent = wb.createCellStyle();
+ d2Percent.setDataFormat(format.getFormat("0.00%"));
+
+ Sheet s = wb.createSheet();
+ Row r1 = s.createRow(0);
+
+ for (int i=0; i<3; i++) {
+ r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(0);
+ }
+ for (int i=3; i<6; i++) {
+ r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(1);
+ }
+ for (int i=6; i<9; i++) {
+ r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(0.12345);
+ }
+ for (int i=9; i<12; i++) {
+ r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(1.2345);
+ }
+ for (int i=0; i<12; i+=3) {
+ r1.getCell(i+0).setCellStyle(iPercent);
+ r1.getCell(i+1).setCellStyle(d1Percent);
+ r1.getCell(i+2).setCellStyle(d2Percent);
+ }
+ for (int i=0; i<12; i++) {
+ s.autoSizeColumn(i);
+ System.out.println(i + " => " + s.getColumnWidth(i));
+ }
+
+ // Check the 0(.00)% ones
+ assertAlmostEquals(980, s.getColumnWidth(0), fontAccuracy);
+ assertAlmostEquals(1400, s.getColumnWidth(1), fontAccuracy);
+ assertAlmostEquals(1700, s.getColumnWidth(2), fontAccuracy);
+
+ // Check the 100(.00)% ones
+ assertAlmostEquals(1500, s.getColumnWidth(3), fontAccuracy);
+ assertAlmostEquals(1950, s.getColumnWidth(4), fontAccuracy);
+ assertAlmostEquals(2225, s.getColumnWidth(5), fontAccuracy);
+
+ // Check the 12(.34)% ones
+ assertAlmostEquals(1225, s.getColumnWidth(6), fontAccuracy);
+ assertAlmostEquals(1650, s.getColumnWidth(7), fontAccuracy);
+ assertAlmostEquals(1950, s.getColumnWidth(8), fontAccuracy);
+
+ // Check the 123(.45)% ones
+ assertAlmostEquals(1500, s.getColumnWidth(9), fontAccuracy);
+ assertAlmostEquals(1950, s.getColumnWidth(10), fontAccuracy);
+ assertAlmostEquals(2225, s.getColumnWidth(11), fontAccuracy);
+ }
}
diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java
index bc489595de..9083b7048f 100644
--- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java
+++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java
@@ -39,6 +39,12 @@ public abstract class BaseTestBugzillaIssues {
protected BaseTestBugzillaIssues(ITestDataProvider testDataProvider) {
_testDataProvider = testDataProvider;
}
+
+ public static void assertAlmostEquals(double expected, double actual, double fuzz) {
+ double diff = Math.abs(expected - actual);
+ if (diff > fuzz)
+ fail(actual + " not within " + fuzz + " of " + expected);
+ }
/**
* Test writing a hyperlink