jdk/test/java/util/Formatter/Constructors.java
author chegar
Fri, 07 Jan 2011 13:08:18 +0000
changeset 7966 a23e3f47c5a8
parent 7668 d4a77089c587
permissions -rw-r--r--
7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown Reviewed-by: alanb, mduigou
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
     2
 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
    25
 * @bug 4981811 4984465 5064492 6240171 7000511
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Unit test for all constructors introduced by the formatter feature
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class Constructors {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private static int fail = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private static int pass = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static Throwable first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static void pass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        pass++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static void fail(String fs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        String s = "'" + fs + "': exception not thrown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        if (first == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            first = new RuntimeException(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        System.err.println("FAILED: " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        fail++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static void fail(String fs, Throwable ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        String s = "'" + fs + "': " + ex.getClass().getName() + " thrown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (first == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            first = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        System.err.println("FAILED: " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        fail++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static void locale(Formatter f) {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
    61
        locale(f, Locale.getDefault(Locale.Category.FORMAT));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static void locale(Formatter f, Locale l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            if ((l != null && !l.equals(f.locale()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                || (l == null && f.locale() != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    throw new RuntimeException(f.locale() + " != " + l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } catch (RuntimeException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            fail(x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static void out(Formatter f, Class c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            Appendable a = f.out();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (!c.isInstance(a))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                throw new RuntimeException(a.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                           + " != " + c.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } catch (RuntimeException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            fail(x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static void main(String [] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        // Formatter()
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
    89
        try (Formatter f = new Formatter()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            out(f, StringBuilder.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            locale(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            fail("new Formatter()", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // Formatter(Appendable a)
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
    98
        try (Formatter f = new Formatter((Appendable) null)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            out(f, StringBuilder.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            locale(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            fail("new Formatter((Appendable)null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // Formatter(Locale l)
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   107
        try (Formatter f = new Formatter((Locale) null)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            out(f, StringBuilder.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            locale(f, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            fail("new Formatter((Locale)null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // Formatter(Appendable a, Locale l)
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   116
        try (Formatter f = new Formatter((Appendable) null, (Locale) null)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            out(f, StringBuilder.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            locale(f, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            fail("new Formatter((Appendable) null, (Locale) null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // Formatter(String fileName)
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   125
        try (Formatter f = new Formatter("foo")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            out(f, BufferedWriter.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            locale(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            fail("new Formatter(\"foo\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        try {
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   134
            new Formatter((String)null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            fail("new Formatter((String)null)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } catch (NullPointerException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            fail("new Formatter((String)null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // Formatter(String fileName, String csn)
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   143
        try (Formatter f = new Formatter("foo", "UTF-8")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            out(f, BufferedWriter.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            locale(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            fail("new Formatter(\"foo\", \"UTF-8\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            new Formatter("foo", "bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            fail("new Formatter(\"foo\", \"bar\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            fail("new Formatter(\"foo\", \"bar\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            new Formatter(".", "bar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            fail("new Formatter(\".\", \"bar\")");
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   163
        } catch (FileNotFoundException|UnsupportedEncodingException x) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            fail("new Formatter(\".\", \"bar\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // Formatter(String fileName, String csn, Locale l)
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   170
        try (Formatter f = new Formatter("foo", "ISO-8859-1", Locale.GERMANY)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            out(f, BufferedWriter.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            locale(f, Locale.GERMANY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            fail("new Formatter(\"foo\", \"ISO-8859-1\", Locale.GERMANY)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   178
        try (Formatter f = new Formatter("foo", "ISO-8859-1", null)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            locale(f, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            out(f, BufferedWriter.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            fail("new Formatter(\"foo\", \"ISO-8859-1\", null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // Formatter(File)
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   187
        try (Formatter f = new Formatter(new File("foo"))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            locale(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            out(f, BufferedWriter.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            fail("new Formatter(new File(\"foo\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        try {
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   196
            new Formatter((File)null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            fail("new Formatter((File)null)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } catch (NullPointerException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            fail("new Formatter((File)null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        // Formatter(PrintStream ps)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // ambiguity detected at compile-time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            Formatter f = new Formatter(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            out(f, PrintStream.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            locale(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            fail("new Formatter(System.out)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            new Formatter((PrintStream) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            fail("new Formatter((PrintStream) null)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } catch (NullPointerException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            fail("new Formatter((PrintStream) null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   224
        try (Formatter f = new Formatter(new PrintStream("foo"))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            locale(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            out(f, PrintStream.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } catch (FileNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            fail("new Formatter(new PrintStream(\"foo\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            fail("new Formatter(new PrintStream(\"foo\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   234
        try (Formatter f = new Formatter(new PrintStream("foo"),
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   235
                                         Locale.JAPANESE)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            locale(f, Locale.JAPANESE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            out(f, PrintStream.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        } catch (FileNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            fail("new Formatter(new PrintStream(\"foo\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            fail("new Formatter(new PrintStream(\"foo\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   245
        try (PrintStream ps = new PrintStream("foo")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // The cast here is necessary to avoid an ambiguity error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            // between Formatter(Appendable a, Locale l)
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   248
            // and Formatter(OutputStream os, String csn)
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   249
            new Formatter(ps, (String)null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            fail("new Formatter(new PrintStream(\"foo\"), (String)null)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } catch (FileNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } catch (NullPointerException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   261
        // The cast here is necessary to avoid an ambiguity error
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   262
        // between Formatter(Appendable a, Locale l)
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   263
        // and  Formatter(OutputStream os, String csn)
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   264
        try (Formatter f = new Formatter(new PrintStream("foo"),
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   265
                                         (Locale)null)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            locale(f, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            out(f, PrintStream.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } catch (FileNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            fail("new Formatter(new PrintStream(\"foo\"), (Locale)null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            fail("new Formatter(new PrintStream(\"foo\"), (Locale)null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   275
        try (Formatter f = new Formatter(new PrintStream("foo"),
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   276
                                         Locale.KOREAN)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            locale(f, Locale.KOREAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            out(f, PrintStream.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } catch (FileNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            fail("new Formatter(new PrintStream(\"foo\"), Locale.KOREAN)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            fail("new Formatter(new PrintStream(\"foo\"), Locale.KOREAN)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   286
        try (Formatter f = new Formatter(new PrintStream("foo"),
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   287
                                         "UTF-16BE", null)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            locale(f, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            out(f, BufferedWriter.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } catch (FileNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   299
        try (Formatter f = new Formatter(new PrintStream("foo"),
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   300
                                         "UTF-16BE", Locale.ITALIAN)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            locale(f, Locale.ITALIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            out(f, BufferedWriter.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        } catch (FileNotFoundException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        String csn = Charset.defaultCharset().newEncoder().canEncode('\u00a3') ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            "ASCII" : "ISO-8859-1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            ByteArrayOutputStream bs[] = { new ByteArrayOutputStream(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                           new ByteArrayOutputStream(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                           new ByteArrayOutputStream()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            new Formatter((Appendable)  new PrintStream(bs[0], true, csn)).format("\u00a3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            new Formatter((OutputStream)new PrintStream(bs[1], true, csn)).format("\u00a3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            new Formatter(              new PrintStream(bs[2], true, csn)).format("\u00a3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (Arrays.equals(bs[0].toByteArray(), bs[1].toByteArray())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                fail("arrays shouldn't match: " + bs[0].toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            if (! Arrays.equals(bs[0].toByteArray(), bs[2].toByteArray())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                fail("arrays should match: " + bs[0].toByteArray() + " != "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                     + bs[2].toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            fail("new PrintStream(newByteArrayOutputStream, true, " + csn + ")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        // Formatter(OutputStream os)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            new Formatter((OutputStream) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            fail("new Formatter((OutputStream) null)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } catch (NullPointerException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            fail("new Formatter((OutputStream) null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   347
        try (Formatter f = new Formatter((OutputStream) new PrintStream("foo"))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            locale(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            out(f, BufferedWriter.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            fail("new Formatter((OutputStream) new PrintStream(\"foo\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // Formatter(OutputStream os, String csn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            new Formatter((OutputStream) null, "ISO-8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            fail("new Formatter((OutputStream) null, \"ISO-8859-1\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        } catch (NullPointerException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            fail("new Formatter((OutputStream) null, \"ISO-8859-1\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   365
        try (PrintStream ps = new PrintStream("foo")) {
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   366
            new Formatter((OutputStream) ps, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            fail("new Formatter((OutputStream) new PrintStream(\"foo\"), null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        } catch (NullPointerException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            fail("new Formatter((OutputStream) new PrintStream(\"foo\"), null",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                 x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   375
        try (PrintStream ps = new PrintStream("foo")) {
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   376
            new Formatter(ps, "bar");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            fail("new Formatter(new PrintStream(\"foo\"), \"bar\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            fail("new Formatter(new PrintStream(\"foo\"), \"bar\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   384
        try (Formatter f = new Formatter(new PrintStream("foo"), "UTF-8")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            locale(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            out(f, BufferedWriter.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // Formatter(OutputStream os, String csn, Locale l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            new Formatter((OutputStream) null, "ISO-8859-1", Locale.UK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            fail("new Formatter((OutputStream) null, \"ISO-8859-1\", Locale.UK)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        } catch (NullPointerException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            fail("new Formatter((OutputStream) null, \"ISO-8859-1\", Locale.UK)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                 x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   403
        try (PrintStream ps = new PrintStream("foo")) {
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   404
            new Formatter(ps, null, Locale.UK);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            fail("new Formatter(new PrintStream(\"foo\"), null, Locale.UK)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        } catch (NullPointerException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            fail("new Formatter(new PrintStream(\"foo\"), null, Locale.UK)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                 x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   413
        try (PrintStream ps = new PrintStream("foo")) {
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   414
            new Formatter(ps, "bar", Locale.UK);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        } catch (UnsupportedEncodingException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                 x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   423
        try (Formatter f = new Formatter(new PrintStream("foo"), "UTF-8", Locale.UK)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            out(f, BufferedWriter.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            locale(f, Locale.UK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\"), Locale.UK",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                 x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        // PrintStream(String fileName)
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   433
        try (PrintStream ps = new PrintStream("foo")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            fail("new PrintStream(\"foo\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        // PrintStream(String fileName, String csn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            new PrintStream("foo", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            fail("new PrintStream(\"foo\", null)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        } catch (NullPointerException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            fail("new PrintStream(\"foo\", null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // PrintStream(File file)
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   450
        try (PrintStream ps = new PrintStream(new File("foo"))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            fail("new PrintStream(new File(\"foo\"))", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        // PrintStream(File file, String csn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            new PrintStream(new File("foo"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            fail("new PrintStream(new File(\"foo\"), null)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        } catch (NullPointerException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            fail("new PrintStream(new File(\"foo\"), null)", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        // PrintWriter(String fileName)
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   467
        try (PrintWriter pw = new PrintWriter("foo")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            fail("new PrintWriter(\"foo\")", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        // PrintWriter(String fileName, String csn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            new PrintWriter("foo", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            fail("new PrintWriter(\"foo\"), null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        } catch (NullPointerException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            fail("new PrintWriter(\"foo\"), null", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        // PrintWriter(File file)
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   484
        try (PrintWriter pw = new PrintWriter(new File("foo"))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            fail("new PrintWriter(new File(\"foo\"))", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        // PrintWriter(File file, String csn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            new PrintWriter(new File("foo"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            fail("new PrintWriter(new File(\"foo\")), null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        } catch (NullPointerException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            fail("new PrintWriter(new File(\"foo\")), null", x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (fail != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            throw new RuntimeException((fail + pass) + " tests: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                                       + fail + " failure(s), first", first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            System.out.println("all " + (fail + pass) + " tests passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
}