1/*
2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
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 *
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/* @test
25 * @bug 4981811 4984465 5064492 6240171 7000511
26 * @summary Unit test for all constructors introduced by the formatter feature
27 */
28
29import java.io.*;
30import java.util.*;
31import java.nio.charset.Charset;
32
33public 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) {
61        locale(f, Locale.getDefault(Locale.Category.FORMAT));
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()
89        try (Formatter f = new Formatter()) {
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)
98        try (Formatter f = new Formatter((Appendable) null)) {
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)
107        try (Formatter f = new Formatter((Locale) null)) {
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)
116        try (Formatter f = new Formatter((Appendable) null, (Locale) null)) {
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)
125        try (Formatter f = new Formatter("foo")) {
126            pass();
127            out(f, BufferedWriter.class);
128            locale(f);
129        } catch (Exception x) {
130            fail("new Formatter(\"foo\")", x);
131        }
132
133        try {
134            new Formatter((String)null);
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)
143        try (Formatter f = new Formatter("foo", "UTF-8")) {
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\")");
163        } catch (FileNotFoundException|UnsupportedEncodingException x) {
164            pass();
165        } catch (Exception x) {
166            fail("new Formatter(\".\", \"bar\")", x);
167        }
168
169        // Formatter(String fileName, String csn, Locale l)
170        try (Formatter f = new Formatter("foo", "ISO-8859-1", Locale.GERMANY)) {
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
178        try (Formatter f = new Formatter("foo", "ISO-8859-1", null)) {
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)
187        try (Formatter f = new Formatter(new File("foo"))) {
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 {
196            new Formatter((File)null);
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
224        try (Formatter f = new Formatter(new PrintStream("foo"))) {
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
234        try (Formatter f = new Formatter(new PrintStream("foo"),
235                                         Locale.JAPANESE)) {
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
245        try (PrintStream ps = new PrintStream("foo")) {
246            // The cast here is necessary to avoid an ambiguity error
247            // between Formatter(Appendable a, Locale l)
248            // and Formatter(OutputStream os, String csn)
249            new Formatter(ps, (String)null);
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
261        // The cast here is necessary to avoid an ambiguity error
262        // between Formatter(Appendable a, Locale l)
263        // and  Formatter(OutputStream os, String csn)
264        try (Formatter f = new Formatter(new PrintStream("foo"),
265                                         (Locale)null)) {
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
275        try (Formatter f = new Formatter(new PrintStream("foo"),
276                                         Locale.KOREAN)) {
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
286        try (Formatter f = new Formatter(new PrintStream("foo"),
287                                         "UTF-16BE", null)) {
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
299        try (Formatter f = new Formatter(new PrintStream("foo"),
300                                         "UTF-16BE", Locale.ITALIAN)) {
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
347        try (Formatter f = new Formatter((OutputStream) new PrintStream("foo"))) {
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
365        try (PrintStream ps = new PrintStream("foo")) {
366            new Formatter((OutputStream) ps, null);
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
375        try (PrintStream ps = new PrintStream("foo")) {
376            new Formatter(ps, "bar");
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
384        try (Formatter f = new Formatter(new PrintStream("foo"), "UTF-8")) {
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
403        try (PrintStream ps = new PrintStream("foo")) {
404            new Formatter(ps, null, Locale.UK);
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
413        try (PrintStream ps = new PrintStream("foo")) {
414            new Formatter(ps, "bar", Locale.UK);
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
423        try (Formatter f = new Formatter(new PrintStream("foo"), "UTF-8", Locale.UK)) {
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)
433        try (PrintStream ps = new PrintStream("foo")) {
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)
450        try (PrintStream ps = new PrintStream(new File("foo"))) {
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)
467        try (PrintWriter pw = new PrintWriter("foo")) {
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)
484        try (PrintWriter pw = new PrintWriter(new File("foo"))) {
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}
507