test/java/io/PrintStream/FailingConstructors.java

Print this page
rev 3565 : 7022624: use try-with-resources in java.io tests
Reviewed-by: XXX


  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 7000511
  27  * @summary PrintStream, PrintWriter, Formatter, Scanner leave files open when
  28  *          exception thrown
  29  */
  30 
  31 import java.io.File;
  32 import java.io.FileInputStream;
  33 import java.io.FileOutputStream;
  34 import java.io.FileNotFoundException;
  35 import java.io.IOException;
  36 import java.io.PrintStream;
  37 import java.io.UnsupportedEncodingException;


  38 
  39 public class FailingConstructors {
  40     static final String fileName = "FailingConstructorsTest";
  41     static final String UNSUPPORTED_CHARSET = "unknownCharset";
  42     static final String FILE_CONTENTS = "This is a small file!";
  43 
  44     private static void realMain(String[] args) throws Throwable {
  45         test(false, new File(fileName));
  46 
  47         /* create the file and write its contents */
  48         File file = File.createTempFile(fileName, null);
  49         file.deleteOnExit();
  50         FileOutputStream fos = new FileOutputStream(file);
  51         fos.write(FILE_CONTENTS.getBytes());
  52         fos.close();
  53 
  54         test(true, file);
  55         file.delete();
  56     }
  57 
  58     private static void test(boolean exists, File file) throws Throwable {
  59         /* PrintStream(File file, String csn) */
  60         try {
  61             new PrintStream(file, UNSUPPORTED_CHARSET);
  62             fail();
  63         } catch(FileNotFoundException|UnsupportedEncodingException e) {
  64             pass();
  65         }
  66 
  67         check(exists, file);
  68 
  69         try {
  70             new PrintStream(file, null);
  71             fail();
  72         } catch(FileNotFoundException|NullPointerException e) {
  73             pass();
  74         }
  75 




  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 7000511
  27  * @summary PrintStream, PrintWriter, Formatter, Scanner leave files open when
  28  *          exception thrown
  29  */
  30 
  31 import java.io.File;
  32 import java.io.FileInputStream;
  33 import java.io.FileOutputStream;
  34 import java.io.FileNotFoundException;
  35 import java.io.IOException;
  36 import java.io.PrintStream;
  37 import java.io.UnsupportedEncodingException;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 
  41 public class FailingConstructors {
  42     static final String fileName = "FailingConstructorsTest";
  43     static final String UNSUPPORTED_CHARSET = "unknownCharset";
  44     static final String FILE_CONTENTS = "This is a small file!";
  45 
  46     private static void realMain(String[] args) throws Throwable {
  47         test(false, new File(fileName));
  48 
  49         /* create the file and write its contents */
  50         Path path = Files.createTempFile(fileName, null);
  51         try {
  52             Files.write(path, FILE_CONTENTS.getBytes());
  53             test(true, path.toFile());
  54         } finally {
  55             Files.delete(path);
  56         }

  57     }
  58 
  59     private static void test(boolean exists, File file) throws Throwable {
  60         /* PrintStream(File file, String csn) */
  61         try {
  62             new PrintStream(file, UNSUPPORTED_CHARSET);
  63             fail();
  64         } catch(FileNotFoundException|UnsupportedEncodingException e) {
  65             pass();
  66         }
  67 
  68         check(exists, file);
  69 
  70         try {
  71             new PrintStream(file, null);
  72             fail();
  73         } catch(FileNotFoundException|NullPointerException e) {
  74             pass();
  75         }
  76