test/java/util/Scanner/FailingConstructors.java

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


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




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