]> source.dussan.org Git - poi.git/commitdiff
* Add missing close() on streams in ImageUtils.getImageDimension()
authorDominik Stadler <centic@apache.org>
Wed, 7 Jan 2015 14:36:39 +0000 (14:36 +0000)
committerDominik Stadler <centic@apache.org>
Wed, 7 Jan 2015 14:36:39 +0000 (14:36 +0000)
* Add some unit tests for CellReference

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1650070 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/util/ImageUtils.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
src/testcases/org/apache/poi/ss/util/TestCellReference.java

index b470824519801cac74f13986c6fc1ca1f72d4e71..2e851416ace3510cf1cae20299c61202caa333eb 100644 (file)
@@ -71,25 +71,30 @@ public class ImageUtils {
                 try {\r
                     //read the image using javax.imageio.*\r
                     ImageInputStream iis = ImageIO.createImageInputStream( is );\r
-                    Iterator<ImageReader> i = ImageIO.getImageReaders( iis );\r
-                    ImageReader r = i.next();\r
-                    r.setInput( iis );\r
-                    BufferedImage img = r.read(0);\r
-\r
-                    int[] dpi = getResolution(r);\r
-\r
-                    //if DPI is zero then assume standard 96 DPI\r
-                    //since cannot divide by zero\r
-                    if (dpi[0] == 0) dpi[0] = PIXEL_DPI;\r
-                    if (dpi[1] == 0) dpi[1] = PIXEL_DPI;\r
-\r
-                    size.width = img.getWidth()*PIXEL_DPI/dpi[0];\r
-                    size.height = img.getHeight()*PIXEL_DPI/dpi[1];\r
-\r
-                    r.dispose();\r
-                    iis.close();\r
-\r
-                } catch (IOException e){\r
+                    try {\r
+                        Iterator<ImageReader> i = ImageIO.getImageReaders( iis );\r
+                        ImageReader r = i.next();\r
+                        try {\r
+                            r.setInput( iis );\r
+                            BufferedImage img = r.read(0);\r
+        \r
+                            int[] dpi = getResolution(r);\r
+        \r
+                            //if DPI is zero then assume standard 96 DPI\r
+                            //since cannot divide by zero\r
+                            if (dpi[0] == 0) dpi[0] = PIXEL_DPI;\r
+                            if (dpi[1] == 0) dpi[1] = PIXEL_DPI;\r
+        \r
+                            size.width = img.getWidth()*PIXEL_DPI/dpi[0];\r
+                            size.height = img.getHeight()*PIXEL_DPI/dpi[1];\r
+                        } finally {\r
+                            r.dispose();\r
+                        }\r
+                    } finally {\r
+                        iis.close();\r
+                    }\r
+\r
+                } catch (IOException e) {\r
                     //silently return if ImageIO failed to read the image\r
                     logger.log(POILogger.WARN, e);\r
                 }\r
index 0eb6e4788397195ed462929553444d065cc8e799..48d8bae0bbb6e72c1603d37ce0ef03b27fa74d18 100644 (file)
@@ -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;
index c6ad609c54b37e96ac0a25d9267276f0189529c1..d8bfee77c427a18841394f84cd2951eecb0455d4 100644 (file)
@@ -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));
+       }
 }