author | mfang |
Wed, 17 Aug 2011 14:18:26 -0700 | |
changeset 10294 | 8fcdae2a7ec7 |
parent 7966 | a23e3f47c5a8 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
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 | 26 |
* @summary Unit test for all constructors introduced by the formatter feature |
27 |
*/ |
|
28 |
||
29 |
import java.io.*; |
|
30 |
import java.util.*; |
|
31 |
import java.nio.charset.Charset; |
|
32 |
||
33 |
public class Constructors { |
|
34 |
||
35 |
private static int fail = 0; |
|
36 |
private static int pass = 0; |
|
37 |
||
38 |
private static Throwable first; |
|
39 |
||
40 |
static void pass() { |
|
41 |
pass++; |
|
42 |
} |
|
43 |
||
44 |
static void fail(String fs) { |
|
45 |
String s = "'" + fs + "': exception not thrown"; |
|
46 |
if (first == null) |
|
47 |
first = new RuntimeException(s); |
|
48 |
System.err.println("FAILED: " + s); |
|
49 |
fail++; |
|
50 |
} |
|
51 |
||
52 |
static void fail(String fs, Throwable ex) { |
|
53 |
String s = "'" + fs + "': " + ex.getClass().getName() + " thrown"; |
|
54 |
if (first == null) |
|
55 |
first = ex; |
|
56 |
System.err.println("FAILED: " + s); |
|
57 |
fail++; |
|
58 |
} |
|
59 |
||
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 | 62 |
} |
63 |
||
64 |
static void locale(Formatter f, Locale l) { |
|
65 |
try { |
|
66 |
if ((l != null && !l.equals(f.locale())) |
|
67 |
|| (l == null && f.locale() != null)) |
|
68 |
throw new RuntimeException(f.locale() + " != " + l); |
|
69 |
pass(); |
|
70 |
} catch (RuntimeException x) { |
|
71 |
fail(x.getMessage()); |
|
72 |
} |
|
73 |
} |
|
74 |
||
75 |
static void out(Formatter f, Class c) { |
|
76 |
try { |
|
77 |
Appendable a = f.out(); |
|
78 |
if (!c.isInstance(a)) |
|
79 |
throw new RuntimeException(a.getClass().getName() |
|
80 |
+ " != " + c.getName()); |
|
81 |
pass(); |
|
82 |
} catch (RuntimeException x) { |
|
83 |
fail(x.getMessage()); |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
public static void main(String [] args) { |
|
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 | 90 |
pass(); |
91 |
out(f, StringBuilder.class); |
|
92 |
locale(f); |
|
93 |
} catch (Exception x) { |
|
94 |
fail("new Formatter()", x); |
|
95 |
} |
|
96 |
||
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 | 99 |
pass(); |
100 |
out(f, StringBuilder.class); |
|
101 |
locale(f); |
|
102 |
} catch (Exception x) { |
|
103 |
fail("new Formatter((Appendable)null)", x); |
|
104 |
} |
|
105 |
||
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 | 108 |
pass(); |
109 |
out(f, StringBuilder.class); |
|
110 |
locale(f, null); |
|
111 |
} catch (Exception x) { |
|
112 |
fail("new Formatter((Locale)null)", x); |
|
113 |
} |
|
114 |
||
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 | 117 |
pass(); |
118 |
out(f, StringBuilder.class); |
|
119 |
locale(f, null); |
|
120 |
} catch (Exception x) { |
|
121 |
fail("new Formatter((Appendable) null, (Locale) null)", x); |
|
122 |
} |
|
123 |
||
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 | 126 |
pass(); |
127 |
out(f, BufferedWriter.class); |
|
128 |
locale(f); |
|
129 |
} catch (Exception x) { |
|
130 |
fail("new Formatter(\"foo\")", x); |
|
131 |
} |
|
132 |
||
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 | 135 |
fail("new Formatter((String)null)"); |
136 |
} catch (NullPointerException x) { |
|
137 |
pass(); |
|
138 |
} catch (Exception x) { |
|
139 |
fail("new Formatter((String)null)", x); |
|
140 |
} |
|
141 |
||
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 | 144 |
pass(); |
145 |
out(f, BufferedWriter.class); |
|
146 |
locale(f); |
|
147 |
} catch (Exception x) { |
|
148 |
fail("new Formatter(\"foo\", \"UTF-8\")", x); |
|
149 |
} |
|
150 |
||
151 |
try { |
|
152 |
new Formatter("foo", "bar"); |
|
153 |
fail("new Formatter(\"foo\", \"bar\")"); |
|
154 |
} catch (UnsupportedEncodingException x) { |
|
155 |
pass(); |
|
156 |
} catch (Exception x) { |
|
157 |
fail("new Formatter(\"foo\", \"bar\")", x); |
|
158 |
} |
|
159 |
||
160 |
try { |
|
161 |
new Formatter(".", "bar"); |
|
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 | 164 |
pass(); |
165 |
} catch (Exception x) { |
|
166 |
fail("new Formatter(\".\", \"bar\")", x); |
|
167 |
} |
|
168 |
||
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 | 171 |
pass(); |
172 |
out(f, BufferedWriter.class); |
|
173 |
locale(f, Locale.GERMANY); |
|
174 |
} catch (Exception x) { |
|
175 |
fail("new Formatter(\"foo\", \"ISO-8859-1\", Locale.GERMANY)", x); |
|
176 |
} |
|
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 | 179 |
pass(); |
180 |
locale(f, null); |
|
181 |
out(f, BufferedWriter.class); |
|
182 |
} catch (Exception x) { |
|
183 |
fail("new Formatter(\"foo\", \"ISO-8859-1\", null)", x); |
|
184 |
} |
|
185 |
||
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 | 188 |
pass(); |
189 |
locale(f); |
|
190 |
out(f, BufferedWriter.class); |
|
191 |
} catch (Exception x) { |
|
192 |
fail("new Formatter(new File(\"foo\")", x); |
|
193 |
} |
|
194 |
||
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 | 197 |
fail("new Formatter((File)null)"); |
198 |
} catch (NullPointerException x) { |
|
199 |
pass(); |
|
200 |
} catch (Exception x) { |
|
201 |
fail("new Formatter((File)null)", x); |
|
202 |
} |
|
203 |
||
204 |
// Formatter(PrintStream ps) |
|
205 |
try { |
|
206 |
// ambiguity detected at compile-time |
|
207 |
Formatter f = new Formatter(System.out); |
|
208 |
pass(); |
|
209 |
out(f, PrintStream.class); |
|
210 |
locale(f); |
|
211 |
} catch (Exception x) { |
|
212 |
fail("new Formatter(System.out)", x); |
|
213 |
} |
|
214 |
||
215 |
try { |
|
216 |
new Formatter((PrintStream) null); |
|
217 |
fail("new Formatter((PrintStream) null)"); |
|
218 |
} catch (NullPointerException x) { |
|
219 |
pass(); |
|
220 |
} catch (Exception x) { |
|
221 |
fail("new Formatter((PrintStream) null)", x); |
|
222 |
} |
|
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 | 225 |
pass(); |
226 |
locale(f); |
|
227 |
out(f, PrintStream.class); |
|
228 |
} catch (FileNotFoundException x) { |
|
229 |
fail("new Formatter(new PrintStream(\"foo\")", x); |
|
230 |
} catch (Exception x) { |
|
231 |
fail("new Formatter(new PrintStream(\"foo\")", x); |
|
232 |
} |
|
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 | 236 |
pass(); |
237 |
locale(f, Locale.JAPANESE); |
|
238 |
out(f, PrintStream.class); |
|
239 |
} catch (FileNotFoundException x) { |
|
240 |
fail("new Formatter(new PrintStream(\"foo\")", x); |
|
241 |
} catch (Exception x) { |
|
242 |
fail("new Formatter(new PrintStream(\"foo\")", x); |
|
243 |
} |
|
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 | 246 |
// The cast here is necessary to avoid an ambiguity error |
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 | 250 |
fail("new Formatter(new PrintStream(\"foo\"), (String)null)"); |
251 |
} catch (FileNotFoundException x) { |
|
252 |
fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x); |
|
253 |
} catch (NullPointerException x) { |
|
254 |
pass(); |
|
255 |
} catch (UnsupportedEncodingException x) { |
|
256 |
fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x); |
|
257 |
} catch (Exception x) { |
|
258 |
fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x); |
|
259 |
} |
|
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 | 266 |
pass(); |
267 |
locale(f, null); |
|
268 |
out(f, PrintStream.class); |
|
269 |
} catch (FileNotFoundException x) { |
|
270 |
fail("new Formatter(new PrintStream(\"foo\"), (Locale)null)", x); |
|
271 |
} catch (Exception x) { |
|
272 |
fail("new Formatter(new PrintStream(\"foo\"), (Locale)null)", x); |
|
273 |
} |
|
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 | 277 |
pass(); |
278 |
locale(f, Locale.KOREAN); |
|
279 |
out(f, PrintStream.class); |
|
280 |
} catch (FileNotFoundException x) { |
|
281 |
fail("new Formatter(new PrintStream(\"foo\"), Locale.KOREAN)", x); |
|
282 |
} catch (Exception x) { |
|
283 |
fail("new Formatter(new PrintStream(\"foo\"), Locale.KOREAN)", x); |
|
284 |
} |
|
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 | 288 |
pass(); |
289 |
locale(f, null); |
|
290 |
out(f, BufferedWriter.class); |
|
291 |
} catch (FileNotFoundException x) { |
|
292 |
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null"); |
|
293 |
} catch (UnsupportedEncodingException x) { |
|
294 |
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null"); |
|
295 |
} catch (Exception x) { |
|
296 |
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null"); |
|
297 |
} |
|
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 | 301 |
pass(); |
302 |
locale(f, Locale.ITALIAN); |
|
303 |
out(f, BufferedWriter.class); |
|
304 |
} catch (FileNotFoundException x) { |
|
305 |
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN"); |
|
306 |
} catch (UnsupportedEncodingException x) { |
|
307 |
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN"); |
|
308 |
} catch (Exception x) { |
|
309 |
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN"); |
|
310 |
} |
|
311 |
||
312 |
String csn = Charset.defaultCharset().newEncoder().canEncode('\u00a3') ? |
|
313 |
"ASCII" : "ISO-8859-1"; |
|
314 |
try { |
|
315 |
ByteArrayOutputStream bs[] = { new ByteArrayOutputStream(), |
|
316 |
new ByteArrayOutputStream(), |
|
317 |
new ByteArrayOutputStream() |
|
318 |
}; |
|
319 |
new Formatter((Appendable) new PrintStream(bs[0], true, csn)).format("\u00a3"); |
|
320 |
new Formatter((OutputStream)new PrintStream(bs[1], true, csn)).format("\u00a3"); |
|
321 |
new Formatter( new PrintStream(bs[2], true, csn)).format("\u00a3"); |
|
322 |
if (Arrays.equals(bs[0].toByteArray(), bs[1].toByteArray())) { |
|
323 |
fail("arrays shouldn't match: " + bs[0].toByteArray()); |
|
324 |
} else { |
|
325 |
pass(); |
|
326 |
} |
|
327 |
if (! Arrays.equals(bs[0].toByteArray(), bs[2].toByteArray())) { |
|
328 |
fail("arrays should match: " + bs[0].toByteArray() + " != " |
|
329 |
+ bs[2].toByteArray()); |
|
330 |
} else { |
|
331 |
pass(); |
|
332 |
} |
|
333 |
} catch (UnsupportedEncodingException x) { |
|
334 |
fail("new PrintStream(newByteArrayOutputStream, true, " + csn + ")", x); |
|
335 |
} |
|
336 |
||
337 |
// Formatter(OutputStream os) |
|
338 |
try { |
|
339 |
new Formatter((OutputStream) null); |
|
340 |
fail("new Formatter((OutputStream) null)"); |
|
341 |
} catch (NullPointerException x) { |
|
342 |
pass(); |
|
343 |
} catch (Exception x) { |
|
344 |
fail("new Formatter((OutputStream) null)", x); |
|
345 |
} |
|
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 | 348 |
pass(); |
349 |
locale(f); |
|
350 |
out(f, BufferedWriter.class); |
|
351 |
} catch (Exception x) { |
|
352 |
fail("new Formatter((OutputStream) new PrintStream(\"foo\")", x); |
|
353 |
} |
|
354 |
||
355 |
// Formatter(OutputStream os, String csn) |
|
356 |
try { |
|
357 |
new Formatter((OutputStream) null, "ISO-8859-1"); |
|
358 |
fail("new Formatter((OutputStream) null, \"ISO-8859-1\")"); |
|
359 |
} catch (NullPointerException x) { |
|
360 |
pass(); |
|
361 |
} catch (Exception x) { |
|
362 |
fail("new Formatter((OutputStream) null, \"ISO-8859-1\")", x); |
|
363 |
} |
|
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 | 367 |
fail("new Formatter((OutputStream) new PrintStream(\"foo\"), null"); |
368 |
} catch (NullPointerException x) { |
|
369 |
pass(); |
|
370 |
} catch (Exception x) { |
|
371 |
fail("new Formatter((OutputStream) new PrintStream(\"foo\"), null", |
|
372 |
x); |
|
373 |
} |
|
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 | 377 |
fail("new Formatter(new PrintStream(\"foo\"), \"bar\")"); |
378 |
} catch (UnsupportedEncodingException x) { |
|
379 |
pass(); |
|
380 |
} catch (Exception x) { |
|
381 |
fail("new Formatter(new PrintStream(\"foo\"), \"bar\")", x); |
|
382 |
} |
|
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 | 385 |
pass(); |
386 |
locale(f); |
|
387 |
out(f, BufferedWriter.class); |
|
388 |
} catch (Exception x) { |
|
389 |
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\")", x); |
|
390 |
} |
|
391 |
||
392 |
// Formatter(OutputStream os, String csn, Locale l) |
|
393 |
try { |
|
394 |
new Formatter((OutputStream) null, "ISO-8859-1", Locale.UK); |
|
395 |
fail("new Formatter((OutputStream) null, \"ISO-8859-1\", Locale.UK)"); |
|
396 |
} catch (NullPointerException x) { |
|
397 |
pass(); |
|
398 |
} catch (Exception x) { |
|
399 |
fail("new Formatter((OutputStream) null, \"ISO-8859-1\", Locale.UK)", |
|
400 |
x); |
|
401 |
} |
|
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 | 405 |
fail("new Formatter(new PrintStream(\"foo\"), null, Locale.UK)"); |
406 |
} catch (NullPointerException x) { |
|
407 |
pass(); |
|
408 |
} catch (Exception x) { |
|
409 |
fail("new Formatter(new PrintStream(\"foo\"), null, Locale.UK)", |
|
410 |
x); |
|
411 |
} |
|
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 | 415 |
fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)"); |
416 |
} catch (UnsupportedEncodingException x) { |
|
417 |
pass(); |
|
418 |
} catch (Exception x) { |
|
419 |
fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)", |
|
420 |
x); |
|
421 |
} |
|
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 | 424 |
pass(); |
425 |
out(f, BufferedWriter.class); |
|
426 |
locale(f, Locale.UK); |
|
427 |
} catch (Exception x) { |
|
428 |
fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\"), Locale.UK", |
|
429 |
x); |
|
430 |
} |
|
431 |
||
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 | 434 |
pass(); |
435 |
} catch (Exception x) { |
|
436 |
fail("new PrintStream(\"foo\")", x); |
|
437 |
} |
|
438 |
||
439 |
// PrintStream(String fileName, String csn) |
|
440 |
try { |
|
441 |
new PrintStream("foo", null); |
|
442 |
fail("new PrintStream(\"foo\", null)"); |
|
443 |
} catch (NullPointerException x) { |
|
444 |
pass(); |
|
445 |
} catch (Exception x) { |
|
446 |
fail("new PrintStream(\"foo\", null)", x); |
|
447 |
} |
|
448 |
||
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 | 451 |
pass(); |
452 |
} catch (Exception x) { |
|
453 |
fail("new PrintStream(new File(\"foo\"))", x); |
|
454 |
} |
|
455 |
||
456 |
// PrintStream(File file, String csn) |
|
457 |
try { |
|
458 |
new PrintStream(new File("foo"), null); |
|
459 |
fail("new PrintStream(new File(\"foo\"), null)"); |
|
460 |
} catch (NullPointerException x) { |
|
461 |
pass(); |
|
462 |
} catch (Exception x) { |
|
463 |
fail("new PrintStream(new File(\"foo\"), null)", x); |
|
464 |
} |
|
465 |
||
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 | 468 |
pass(); |
469 |
} catch (Exception x) { |
|
470 |
fail("new PrintWriter(\"foo\")", x); |
|
471 |
} |
|
472 |
||
473 |
// PrintWriter(String fileName, String csn) |
|
474 |
try { |
|
475 |
new PrintWriter("foo", null); |
|
476 |
fail("new PrintWriter(\"foo\"), null"); |
|
477 |
} catch (NullPointerException x) { |
|
478 |
pass(); |
|
479 |
} catch (Exception x) { |
|
480 |
fail("new PrintWriter(\"foo\"), null", x); |
|
481 |
} |
|
482 |
||
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 | 485 |
pass(); |
486 |
} catch (Exception x) { |
|
487 |
fail("new PrintWriter(new File(\"foo\"))", x); |
|
488 |
} |
|
489 |
||
490 |
// PrintWriter(File file, String csn) |
|
491 |
try { |
|
492 |
new PrintWriter(new File("foo"), null); |
|
493 |
fail("new PrintWriter(new File(\"foo\")), null"); |
|
494 |
} catch (NullPointerException x) { |
|
495 |
pass(); |
|
496 |
} catch (Exception x) { |
|
497 |
fail("new PrintWriter(new File(\"foo\")), null", x); |
|
498 |
} |
|
499 |
||
500 |
if (fail != 0) |
|
501 |
throw new RuntimeException((fail + pass) + " tests: " |
|
502 |
+ fail + " failure(s), first", first); |
|
503 |
else |
|
504 |
System.out.println("all " + (fail + pass) + " tests passed"); |
|
505 |
} |
|
506 |
} |