test/java/util/Formatter/FailingConstructors.java

Print this page
rev 3509 : 7021209: convert lang, math, util to use try-with-resources
Reviewed-by: XXX


  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.UnsupportedEncodingException;
  37 import java.util.Formatter;
  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         /* Formatter(File file, String csn) */
  60         try {
  61             new Formatter(file, UNSUPPORTED_CHARSET);
  62             fail();
  63         } catch(FileNotFoundException|UnsupportedEncodingException e) {
  64             pass();
  65         }
  66 
  67         check(exists, file);
  68 
  69         try {
  70             new Formatter(file, null);
  71             fail();
  72         } catch(FileNotFoundException|NullPointerException e) {




  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.UnsupportedEncodingException;
  37 import java.util.Formatter;
  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         try (FileOutputStream fos = new FileOutputStream(file)) {
  51             fos.write(FILE_CONTENTS.getBytes());
  52         }
  53 
  54         test(true, file);
  55         file.delete();
  56     }
  57 
  58     private static void test(boolean exists, File file) throws Throwable {
  59         /* Formatter(File file, String csn) */
  60         try {
  61             new Formatter(file, UNSUPPORTED_CHARSET);
  62             fail();
  63         } catch(FileNotFoundException|UnsupportedEncodingException e) {
  64             pass();
  65         }
  66 
  67         check(exists, file);
  68 
  69         try {
  70             new Formatter(file, null);
  71             fail();
  72         } catch(FileNotFoundException|NullPointerException e) {