]> source.dussan.org Git - poi.git/commitdiff
Unit test for column autosizing of % values
authorNick Burch <nick@apache.org>
Fri, 18 Apr 2014 20:46:57 +0000 (20:46 +0000)
committerNick Burch <nick@apache.org>
Fri, 18 Apr 2014 20:46:57 +0000 (20:46 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1588562 13f79535-47bb-0310-9956-ffa450edef68

src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java

index bc234d2a9c926f3ec55dcc71bc19ee282425752c..a4bd4a5cf3438a650d50e3e31432b84515a72186 100644 (file)
@@ -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);
+    }
 }
index bc489595de3d5b15d3c91287eddd5bcb197e8ec5..9083b7048fc4f60772204d9203cda4c5b7296768 100644 (file)
@@ -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