7059970: Test case: javax/imageio/plugins/png/ITXtTest.java is not closing a file
authorjdv
Wed, 13 Jul 2016 17:34:26 +0530
changeset 39859 c073d27c43e9
parent 39858 882e63569349
child 39860 963bd9428552
7059970: Test case: javax/imageio/plugins/png/ITXtTest.java is not closing a file Reviewed-by: prr, bpb, psadhukhan Contributed-by: jayathirth.d.v@oracle.com
jdk/test/javax/imageio/plugins/png/ITXtTest.java
--- a/jdk/test/javax/imageio/plugins/png/ITXtTest.java	Wed Jul 13 16:05:36 2016 +0530
+++ b/jdk/test/javax/imageio/plugins/png/ITXtTest.java	Wed Jul 13 17:34:26 2016 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -21,16 +21,15 @@
  * questions.
  */
 
-/**
+/*
  * @test
- * @bug     6541476
- * @summary Test verifies that ImageIO PNG plugin correcly handles the
+ * @bug     6541476 7059970
+ * @summary Test verifies that ImageIO PNG plug-in correctly handles the
  *          iTxt chunk (International textual data).
  *
  * @run     main ITXtTest
  */
 
-
 import java.awt.Color;
 import java.awt.Graphics2D;
 import java.awt.image.BufferedImage;
@@ -157,21 +156,22 @@
 
         File file = new File("test.png");
 
-        writeTo(file, src);
-        ITXtTest dst = readFrom(file);
-
-        if (dst == null || !dst.equals(src)) {
-            throw new RuntimeException("Test failed.");
+        try {
+            writeTo(file, src);
+            ITXtTest dst = readFrom(file);
+            if (dst == null || !dst.equals(src)) {
+                throw new RuntimeException("Test failed.");
+            }
+        } finally {
+            file.delete();
         }
-
         System.out.println("Test passed.");
     }
 
     private static void writeTo(File f, ITXtTest t) {
         BufferedImage src = createBufferedImage();
-        try {
-            ImageOutputStream imageOutputStream =
-                ImageIO.createImageOutputStream(f);
+        try (ImageOutputStream imageOutputStream =
+                ImageIO.createImageOutputStream(f)) {
 
             ImageTypeSpecifier imageTypeSpecifier =
                 new ImageTypeSpecifier(src);
@@ -191,7 +191,6 @@
             m.setFromTree(format, root);
 
             imageWriter.write(new IIOImage(src, null, m));
-            imageOutputStream.close();
             System.out.println("Writing done.");
         } catch (Throwable e) {
             throw new RuntimeException("Writing test failed.", e);
@@ -199,10 +198,11 @@
     }
 
     private static ITXtTest readFrom(File f) {
-        try {
-            ImageInputStream iis = ImageIO.createImageInputStream(f);
-            ImageReader r = ImageIO.getImageReaders(iis).next();
-            r.setInput(iis);
+        try (ImageInputStream imageInputStream =
+                ImageIO.createImageInputStream(f)) {
+
+            ImageReader r = ImageIO.getImageReaders(imageInputStream).next();
+            r.setInput(imageInputStream);
 
             IIOImage dst = r.readAll(0, null);
 
@@ -234,3 +234,4 @@
         return image;
     }
 }
+