< prev index next >

test/java/io/PrintWriter/FailingConstructors.java

Print this page




  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  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.PrintWriter;
  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         /* PrintWriter(File file, String csn) */
  60         try {
  61             new PrintWriter(file, UNSUPPORTED_CHARSET);
  62             fail();
  63         } catch(FileNotFoundException|UnsupportedEncodingException e) {
  64             pass();
  65         }
  66 
  67         check(exists, file);
  68 
  69         try {
  70             new PrintWriter(file, null);









  71             fail();
  72         } catch(FileNotFoundException|NullPointerException e) {
  73             pass();
  74         }
  75 
  76         check(exists, file);
  77 
  78         /* PrintWriter(String fileName, String csn)  */
  79         try {
  80             new PrintWriter(file.getName(), UNSUPPORTED_CHARSET);
  81             fail();
  82         } catch(FileNotFoundException|UnsupportedEncodingException e) {
  83             pass();
  84         }
  85 
  86         check(exists, file);
  87 
  88         try {
  89             new PrintWriter(file.getName(), null);
  90             fail();




  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  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.ByteArrayOutputStream;
  32 import java.io.File;
  33 import java.io.FileInputStream;

  34 import java.io.FileNotFoundException;
  35 import java.io.FileOutputStream;
  36 import java.io.IOException;
  37 import java.io.OutputStream;
  38 import java.io.PrintWriter;
  39 import java.io.UnsupportedEncodingException;
  40 import java.nio.charset.Charset;
  41 
  42 public class FailingConstructors {
  43     static final String fileName = "FailingConstructorsTest";
  44     static final String UNSUPPORTED_CHARSET = "unknownCharset";
  45     static final String FILE_CONTENTS = "This is a small file!";
  46 
  47     private static void realMain(String[] args) throws Throwable {
  48         testOutputStream();
  49 
  50         test(false, new File(fileName));
  51 
  52         /* create the file and write its contents */
  53         File file = File.createTempFile(fileName, null);
  54         file.deleteOnExit();
  55         FileOutputStream fos = new FileOutputStream(file);
  56         fos.write(FILE_CONTENTS.getBytes());
  57         fos.close();
  58 
  59         test(true, file);
  60         file.delete();
  61     }
  62 
  63     private static void testWithAutoFlush() throws Throwable {
  64         ByteArrayOutputStream out = new ByteArrayOutputStream();
  65 
  66         PrintWriter writer = new PrintWriter(out);
  67 
  68         PrintWriter writer_noAutoFlush = writer.withAutoFlush(false);
  69         if (writer == writer_noAutoFlush) {
  70             pass();
  71         } else {
  72             fail();;
  73         }
  74 
  75         PrintWriter writer_withAutoFlush = writer.withAutoFlush(true);
  76         if (writer == writer_withAutoFlush) {
  77             pass();
  78         } else {
  79             fail();;
  80         }
  81 
  82         PrintWriter writer_withAutoFlush2 = writer_withAutoFlush.withAutoFlush(true);
  83         if (writer_withAutoFlush == writer_withAutoFlush2) {
  84             pass();
  85         } else {
  86             fail();;
  87         }
  88     }
  89 
  90     private static void testOutputStream() throws Throwable {
  91         try {
  92             new PrintWriter((OutputStream)null, (Charset)null);
  93             fail();
  94         } catch(NullPointerException e) {
  95             pass();
  96         }
  97     }
  98 
  99     private static void test(boolean exists, File file) throws Throwable {
 100         /* PrintWriter(File file, String csn) */
 101         try {
 102             new PrintWriter(file, UNSUPPORTED_CHARSET);
 103             fail();
 104         } catch(FileNotFoundException|UnsupportedEncodingException e) {
 105             pass();
 106         }
 107 
 108         check(exists, file);
 109 
 110         try {
 111             new PrintWriter(file, (String)null);
 112             fail();
 113         } catch(FileNotFoundException|NullPointerException e) {
 114             pass();
 115         }
 116 
 117         check(exists, file);
 118 
 119         try {
 120             new PrintWriter(file, (Charset) null);
 121             fail();
 122         } catch(FileNotFoundException|NullPointerException e) {
 123             pass();
 124         }
 125 
 126         check(exists, file);
 127 
 128         /* PrintWriter(String fileName, String csn)  */
 129         try {
 130             new PrintWriter(file.getName(), UNSUPPORTED_CHARSET);
 131             fail();
 132         } catch(FileNotFoundException|UnsupportedEncodingException e) {
 133             pass();
 134         }
 135 
 136         check(exists, file);
 137 
 138         try {
 139             new PrintWriter(file.getName(), null);
 140             fail();


< prev index next >