From 8997d8900d77f2e9ec8aeb26f6bbb7e66539ba83 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Wed, 7 Jan 2015 14:36:39 +0000 Subject: [PATCH] * Add missing close() on streams in ImageUtils.getImageDimension() * Add some unit tests for CellReference git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1650070 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/ss/util/ImageUtils.java | 41 +++++++++++-------- .../poi/xssf/usermodel/TestXSSFBugs.java | 1 - .../apache/poi/ss/util/TestCellReference.java | 30 ++++++++++++++ 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/java/org/apache/poi/ss/util/ImageUtils.java b/src/java/org/apache/poi/ss/util/ImageUtils.java index b47082451..2e851416a 100644 --- a/src/java/org/apache/poi/ss/util/ImageUtils.java +++ b/src/java/org/apache/poi/ss/util/ImageUtils.java @@ -71,25 +71,30 @@ public class ImageUtils { try { //read the image using javax.imageio.* ImageInputStream iis = ImageIO.createImageInputStream( is ); - Iterator i = ImageIO.getImageReaders( iis ); - ImageReader r = i.next(); - r.setInput( iis ); - BufferedImage img = r.read(0); + try { + Iterator i = ImageIO.getImageReaders( iis ); + ImageReader r = i.next(); + try { + r.setInput( iis ); + BufferedImage img = r.read(0); + + int[] dpi = getResolution(r); + + //if DPI is zero then assume standard 96 DPI + //since cannot divide by zero + if (dpi[0] == 0) dpi[0] = PIXEL_DPI; + if (dpi[1] == 0) dpi[1] = PIXEL_DPI; + + size.width = img.getWidth()*PIXEL_DPI/dpi[0]; + size.height = img.getHeight()*PIXEL_DPI/dpi[1]; + } finally { + r.dispose(); + } + } finally { + iis.close(); + } - int[] dpi = getResolution(r); - - //if DPI is zero then assume standard 96 DPI - //since cannot divide by zero - if (dpi[0] == 0) dpi[0] = PIXEL_DPI; - if (dpi[1] == 0) dpi[1] = PIXEL_DPI; - - size.width = img.getWidth()*PIXEL_DPI/dpi[0]; - size.height = img.getHeight()*PIXEL_DPI/dpi[1]; - - r.dispose(); - iis.close(); - - } catch (IOException e){ + } catch (IOException e) { //silently return if ImageIO failed to read the image logger.log(POILogger.WARN, e); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 0eb6e4788..48d8bae0b 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -50,7 +50,6 @@ import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.AreaReference; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; -import org.apache.poi.ss.util.RegionUtil; import org.apache.poi.util.TempFile; import org.apache.poi.xssf.XLSBUnsupportedException; import org.apache.poi.xssf.XSSFITestDataProvider; diff --git a/src/testcases/org/apache/poi/ss/util/TestCellReference.java b/src/testcases/org/apache/poi/ss/util/TestCellReference.java index c6ad609c5..d8bfee77c 100644 --- a/src/testcases/org/apache/poi/ss/util/TestCellReference.java +++ b/src/testcases/org/apache/poi/ss/util/TestCellReference.java @@ -218,4 +218,34 @@ public final class TestCellReference extends TestCase { throw new AssertionFailedError("expected (c='" + colStr + "', r='" + rowStr + "' to be " + (expResult ? "within" : "out of") + " bounds for version " + sv.name()); } + + public void testConvertColStringToIndex() { + assertEquals(0, CellReference.convertColStringToIndex("A")); + assertEquals(1, CellReference.convertColStringToIndex("B")); + assertEquals(14, CellReference.convertColStringToIndex("O")); + assertEquals(701, CellReference.convertColStringToIndex("ZZ")); + assertEquals(18252, CellReference.convertColStringToIndex("ZZA")); + + assertEquals(0, CellReference.convertColStringToIndex("$A")); + assertEquals(1, CellReference.convertColStringToIndex("$B")); + + try { + CellReference.convertColStringToIndex("A$"); + fail("Should throw exception here"); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains("A$")); + } + } + + public void testConvertNumColColString() { + assertEquals("A", CellReference.convertNumToColString(0)); + assertEquals("AV", CellReference.convertNumToColString(47)); + assertEquals("AW", CellReference.convertNumToColString(48)); + assertEquals("BF", CellReference.convertNumToColString(57)); + + assertEquals("", CellReference.convertNumToColString(-1)); + assertEquals("", CellReference.convertNumToColString(Integer.MIN_VALUE)); + assertEquals("", CellReference.convertNumToColString(Integer.MAX_VALUE)); + assertEquals("FXSHRXW", CellReference.convertNumToColString(Integer.MAX_VALUE-1)); + } }