changeset 17826:70359afda5d0

8187113: test/javax/imageio/plugins/png/PngCreationTimeTest.java fails Reviewed-by: serb, psadhukhan Contributed-by: prahalad.kumar.narayanan@oracle.com
author pnarayanan
date Sun, 03 Sep 2017 19:31:11 +0530
parents 6581f08c67b6
children f3f6f6410d2d
files test/javax/imageio/plugins/png/PngCreationTimeTest.java test/javax/imageio/plugins/png/duke.png
diffstat 2 files changed, 91 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/test/javax/imageio/plugins/png/PngCreationTimeTest.java	Fri Sep 01 12:32:20 2017 +0530
+++ b/test/javax/imageio/plugins/png/PngCreationTimeTest.java	Sun Sep 03 19:31:11 2017 +0530
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8164971
+ * @bug 8164971 8187113
  * @summary The test decodes a png file and checks if the metadata contains
  *          image creation time. In addition, the test also merges the custom
  *          metadata tree (both standard and native) and succeeds when the
@@ -55,6 +55,7 @@
     private static IIOMetadata pngMetadata = null;
 
     public static void initializeTest() throws IOException {
+        Iterator<ImageReader> iterR = null;
         ImageReader pngImageReader = null;
         BufferedImage decImage = null;
         ImageInputStream imageStream = null;
@@ -62,25 +63,43 @@
         String separator = System.getProperty("file.separator");
         String dirPath = System.getProperty("test.src", ".");
         String filePath = dirPath + separator + fileName;
-        File file = new File(filePath);
+        File file = null;
 
         try {
-            Iterator<ImageReader> iterR = ImageIO.getImageReadersBySuffix("PNG");
+            // Open the required file and check if file exists.
+            file = new File(filePath);
+            if (file != null && !file.exists()) {
+                reportExceptionAndFail("Test Failed. Required image file was"
+                        + " not found.");
+            }
+
+            // Get PNG image reader
+            iterR = ImageIO.getImageReadersBySuffix("PNG");
             if (iterR.hasNext()) {
                 pngImageReader = iterR.next();
                 ImageReadParam param = pngImageReader.getDefaultReadParam();
                 imageStream = ImageIO.createImageInputStream(file);
-
-                /*
-                 * Last argument- false, informs reader not to ignore the
-                 * metadata from the image file
-                 */
-                pngImageReader.setInput(imageStream, false, false);
-                decImage = pngImageReader.read(0, param);
-                pngMetadata = pngImageReader.getImageMetadata(0);
-
-                // Check if the metadata contains creation time
-                testImageMetadata(pngMetadata);
+                if (imageStream != null) {
+                    // Last argument informs reader not to ignore metadata
+                    pngImageReader.setInput(imageStream,
+                                            false,
+                                            false);
+                    decImage = pngImageReader.read(0, param);
+                    pngMetadata = pngImageReader.getImageMetadata(0);
+                    if (pngMetadata != null) {
+                        // Check if the metadata contains creation time
+                        testImageMetadata(pngMetadata);
+                    } else {
+                        reportExceptionAndFail("Test Failed. Reader could not"
+                                + " generate image metadata.");
+                    }
+                } else {
+                    reportExceptionAndFail("Test Failed. Could not initialize"
+                            + " image input stream.");
+                }
+            } else {
+                reportExceptionAndFail("Test Failed. Required image reader"
+                        + " was not found.");
             }
         } finally {
             // Release ther resources
@@ -103,14 +122,16 @@
             Node keyNode = findNode(metadata.getAsTree("javax_imageio_1.0"),
                     "ImageCreationTime");
             if (keyNode == null) {
-                throw new RuntimeException("Test Failed: Reader could not"
-                        + " find creation time in the metadata");
+                reportExceptionAndFail("Test Failed: Could not find image"
+                        + " creation time in the metadata.");
             }
         }
     }
 
     public static void testSaveCreationTime() throws IOException {
         File file = null;
+        Iterator<ImageWriter> iterW = null;
+        Iterator<ImageReader> iterR = null;
         ImageWriter pngImageWriter = null;
         ImageReader pngImageReader = null;
         ImageInputStream inputStream = null;
@@ -127,39 +148,60 @@
             // Create a temporary file for the output png image
             String fileName = "RoundTripTest";
             file = File.createTempFile(fileName, ".png");
+            if (file == null) {
+                reportExceptionAndFail("Test Failed. Could not create a"
+                        + " temporary file for round trip test.");
+            }
 
             // Create a PNG writer and write test image with metadata
-            Iterator<ImageWriter> iterW = ImageIO.getImageWritersBySuffix("PNG");
+            iterW = ImageIO.getImageWritersBySuffix("PNG");
             if (iterW.hasNext()) {
                 pngImageWriter = iterW.next();
                 outputStream = ImageIO.createImageOutputStream(file);
-                pngImageWriter.setOutput(outputStream);
+                if (outputStream != null) {
+                    pngImageWriter.setOutput(outputStream);
 
-                // Get the default metadata & add image creation time to it.
-                IIOMetadata metadata = pngImageWriter.getDefaultImageMetadata(
-                        ImageTypeSpecifier.createFromRenderedImage(buffImage),
-                        null);
-                IIOMetadataNode root = createStandardMetadataNodeTree();
-                metadata.mergeTree("javax_imageio_1.0", root);
+                    // Get the default metadata & add image creation time to it.
+                    ImageTypeSpecifier imgType =
+                            ImageTypeSpecifier.createFromRenderedImage(buffImage);
+                    IIOMetadata metadata =
+                            pngImageWriter.getDefaultImageMetadata(imgType, null);
+                    IIOMetadataNode root = createStandardMetadataNodeTree();
+                    metadata.mergeTree("javax_imageio_1.0", root);
 
-                // Write a png image using buffImage & metadata
-                IIOImage iioImage = new IIOImage(buffImage, null, metadata);
-                pngImageWriter.write(iioImage);
+                    // Write a png image using buffImage & metadata
+                    IIOImage iioImage = new IIOImage(buffImage, null, metadata);
+                    pngImageWriter.write(iioImage);
+                } else {
+                    reportExceptionAndFail("Test Failed. Could not initialize"
+                            + " image output stream for round trip test.");
+                }
+            } else {
+                reportExceptionAndFail("Test Failed. Could not find required"
+                        + " image writer for the round trip test.");
             }
 
             // Create a PNG reader and check if metadata was written
-            Iterator<ImageReader> iterR = ImageIO.getImageReadersBySuffix("PNG");
+            iterR = ImageIO.getImageReadersBySuffix("PNG");
             if (iterR.hasNext()) {
                 pngImageReader = iterR.next();
                 inputStream = ImageIO.createImageInputStream(file);
+                if (inputStream != null) {
+                    // Read the image and get the metadata
+                    pngImageReader.setInput(inputStream, false, false);
+                    pngImageReader.read(0);
+                    IIOMetadata imgMetadata =
+                            pngImageReader.getImageMetadata(0);
 
-                // Read the image and get the metadata
-                pngImageReader.setInput(inputStream, false, false);
-                pngImageReader.read(0);
-                IIOMetadata imgMetadata = pngImageReader.getImageMetadata(0);
-
-                // Test if the metadata contains creation time.
-                testImageMetadata(imgMetadata);
+                    // Test if the metadata contains creation time.
+                    testImageMetadata(imgMetadata);
+                } else {
+                    reportExceptionAndFail("Test Failed. Could not initialize"
+                            + " image input stream for round trip test.");
+                }
+            } else {
+                reportExceptionAndFail("Test Failed. Cound not find the"
+                        + " required image reader.");
             }
         } finally {
             // Release the resources held
@@ -182,6 +224,11 @@
         }
     }
 
+    public static void reportExceptionAndFail(String message) {
+        // A common method to report exception.
+        throw new RuntimeException(message);
+    }
+
     public static void testMergeNativeTree() {
         // Merge a custom native metadata tree and inspect creation time
         if (pngMetadata != null) {
@@ -195,7 +242,6 @@
                 pngMetadata.mergeTree("javax_imageio_png_1.0", root);
                 Node keyNode = findNode(pngMetadata.getAsTree("javax_imageio_1.0"),
                         "ImageCreationTime");
-
                 if (keyNode != null) {
                     // Query the attributes of the node and check for the value
                     NamedNodeMap attrMap = keyNode.getAttributes();
@@ -204,17 +250,17 @@
                     int decYear = Integer.parseInt(attrValue);
                     if (decYear != 2014) {
                         // Throw exception. Incorrect year value observed
-                        throw new RuntimeException("Test Failed: Incorrect"
-                                + " creation time value observed");
+                        reportExceptionAndFail("Test Failed: Incorrect"
+                                + " creation time value observed.");
                     }
                 } else {
                     // Throw exception.
-                    throw new RuntimeException("Test Failed: Image creation"
-                            + "time doesn't exist in metadata");
+                    reportExceptionAndFail("Test Failed: Image creation"
+                            + " time doesn't exist in metadata.");
                 }
             } catch (IOException ex) {
                 // Throw exception.
-                throw new RuntimeException("Test Failed: While executing"
+                reportExceptionAndFail("Test Failed: While executing"
                         + " mergeTree on metadata.");
             }
         }
@@ -246,16 +292,16 @@
                     if (!attrValue.contains("2016")) {
                         // Throw exception. Incorrect year value observed
                         throw new RuntimeException("Test Failed: Incorrect"
-                                + " creation time value observed");
+                                + " creation time value observed.");
                     }
                 } else {
                     // Throw exception.
-                    throw new RuntimeException("Test Failed: Image creation"
-                            + "time doesn't exist in metadata");
+                    reportExceptionAndFail("Test Failed: Image creation"
+                            + " time doesn't exist in metadata.");
                 }
             } catch (IOException ex) {
                 // Throw exception.
-                throw new RuntimeException("Test Failed: While executing"
+                reportExceptionAndFail("Test Failed: While executing"
                         + " mergeTree on metadata.");
             }
         }
Binary file test/javax/imageio/plugins/png/duke.png has changed