< prev index next >

test/javax/imageio/plugins/shared/WriteAfterAbort.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2017, 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
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -27,10 +27,11 @@
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.Iterator;
+import java.nio.file.Files;
 
 import javax.imageio.ImageIO;
 import javax.imageio.ImageWriter;
 import javax.imageio.event.IIOWriteProgressListener;
 import javax.imageio.spi.IIORegistry;

@@ -39,23 +40,32 @@
 
 import static java.awt.image.BufferedImage.TYPE_BYTE_BINARY;
 
 /**
  * @test
- * @bug 4952954
+ * @bug 4952954 8183349
  * @summary abortFlag must be cleared for every ImageWriter.write operation
- * @author Sergey Bylokhov
+ * @run     main/manual WriteAfterAbort
  */
 public final class WriteAfterAbort implements IIOWriteProgressListener {
 
     private volatile boolean abortFlag = true;
     private volatile boolean isAbortCalled;
     private volatile boolean isCompleteCalled;
     private volatile boolean isProgressCalled;
     private volatile boolean isStartedCalled;
     private static final int WIDTH = 100;
     private static final int HEIGHT = 100;
+    private static ImageWriter writer;
+    private static FileOutputStream fos;
+    private static File file;
+
+    private void deleteTestFile() throws IOException {
+        writer.dispose();
+        fos.close();
+        Files.delete(file.toPath());
+    }
 
     private void test(final ImageWriter writer) throws IOException {
         // Image initialization
         final BufferedImage imageWrite = new BufferedImage(WIDTH, HEIGHT,
                                                            TYPE_BYTE_BINARY);

@@ -63,29 +73,37 @@
         g.setColor(Color.WHITE);
         g.fillRect(0, 0, WIDTH, HEIGHT);
         g.dispose();
 
         // File initialization
-        final File file = File.createTempFile("temp", ".img");
-        file.deleteOnExit();
-        final FileOutputStream fos = new SkipWriteOnAbortOutputStream(file);
+        String sep = System.getProperty("file.separator");
+        String dir = System.getProperty("test.src", ".");
+        String filePath = dir+sep;
+        File directory = new File(filePath);
+        file = File.createTempFile("temp", ".img", directory);
+        directory.delete();
+        fos = new SkipWriteOnAbortOutputStream(file);
         final ImageOutputStream ios = ImageIO.createImageOutputStream(fos);
         writer.setOutput(ios);
         writer.addIIOWriteProgressListener(this);
 
         // This write will be aborted, and file will not be touched
         writer.write(imageWrite);
         if (!isStartedCalled) {
+            deleteTestFile();
             throw new RuntimeException("Started should be called");
         }
         if (!isProgressCalled) {
+            deleteTestFile();
             throw new RuntimeException("Progress should be called");
         }
         if (!isAbortCalled) {
+            deleteTestFile();
             throw new RuntimeException("Abort should be called");
         }
         if (isCompleteCalled) {
+            deleteTestFile();
             throw new RuntimeException("Complete should not be called");
         }
         // Flush aborted data
         ios.flush();
 

@@ -97,26 +115,30 @@
         isProgressCalled = false;
         isStartedCalled = false;
         writer.write(imageWrite);
 
         if (!isStartedCalled) {
+            deleteTestFile();
             throw new RuntimeException("Started should be called");
         }
         if (!isProgressCalled) {
+            deleteTestFile();
             throw new RuntimeException("Progress should be called");
         }
         if (isAbortCalled) {
+            deleteTestFile();
             throw new RuntimeException("Abort should not be called");
         }
         if (!isCompleteCalled) {
+            deleteTestFile();
             throw new RuntimeException("Complete should be called");
         }
-        writer.dispose();
-        ios.close();
 
+        ios.close();
         // Validates content of the file.
         final BufferedImage imageRead = ImageIO.read(file);
+        deleteTestFile();
         for (int x = 0; x < WIDTH; ++x) {
             for (int y = 0; y < HEIGHT; ++y) {
                 if (imageRead.getRGB(x, y) != imageWrite.getRGB(x, y)) {
                     throw new RuntimeException("Test failed.");
                 }

@@ -131,15 +153,16 @@
 
         // Validates all supported ImageWriters
         int numFailures = 0;
         while (iter.hasNext()) {
             final WriteAfterAbort writeAfterAbort = new WriteAfterAbort();
-            final ImageWriter writer = iter.next().createWriterInstance();
+            writer = iter.next().createWriterInstance();
             System.out.println("ImageWriter = " + writer);
             try {
                 writeAfterAbort.test(writer);
             } catch (Exception e) {
+                writeAfterAbort.deleteTestFile();
                 System.err.println("Test failed for \""
                     + writer.getOriginatingProvider().getFormatNames()[0]
                     + "\" format.");
                 numFailures++;
             }
< prev index next >