1 /*
   2  * Copyright (c) 2004, 2006, 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
  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) {
  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 
  89         // Formatter()
  90         try {
  91             Formatter f = new Formatter();
  92             pass();
  93             out(f, StringBuilder.class);
  94             locale(f);
  95         } catch (Exception x) {
  96             fail("new Formatter()", x);
  97         }
  98 
  99         // Formatter(Appendable a)
 100         try {
 101             Formatter f = new Formatter((Appendable) null);
 102             pass();
 103             out(f, StringBuilder.class);
 104             locale(f);
 105         } catch (Exception x) {
 106             fail("new Formatter((Appendable)null)", x);
 107         }
 108 
 109         // Formatter(Locale l)
 110         try {
 111             Formatter f = new Formatter((Locale) null);
 112             pass();
 113             out(f, StringBuilder.class);
 114             locale(f, null);
 115         } catch (Exception x) {
 116             fail("new Formatter((Locale)null)", x);
 117         }
 118 
 119         // Formatter(Appendable a, Locale l)
 120         try {
 121             Formatter f = new Formatter((Appendable) null, (Locale) null);
 122             pass();
 123             out(f, StringBuilder.class);
 124             locale(f, null);
 125         } catch (Exception x) {
 126             fail("new Formatter((Appendable) null, (Locale) null)", x);
 127         }
 128 
 129         // Formatter(String fileName)
 130         try {
 131             Formatter f = new Formatter("foo");
 132             pass();
 133             out(f, BufferedWriter.class);
 134             locale(f);
 135         } catch (Exception x) {
 136             fail("new Formatter(\"foo\")", x);
 137         }
 138 
 139         try {
 140             Formatter f = new Formatter((String)null);
 141             fail("new Formatter((String)null)");
 142         } catch (NullPointerException x) {
 143             pass();
 144         } catch (Exception x) {
 145             fail("new Formatter((String)null)", x);
 146         }
 147 
 148         // Formatter(String fileName, String csn)
 149         try {
 150             Formatter f = new Formatter("foo", "UTF-8");
 151             pass();
 152             out(f, BufferedWriter.class);
 153             locale(f);
 154         } catch (Exception x) {
 155             fail("new Formatter(\"foo\", \"UTF-8\")", x);
 156         }
 157 
 158         try {
 159             new Formatter("foo", "bar");
 160             fail("new Formatter(\"foo\", \"bar\")");
 161         } catch (UnsupportedEncodingException x) {
 162             pass();
 163         } catch (Exception x) {
 164             fail("new Formatter(\"foo\", \"bar\")", x);
 165         }
 166 
 167         try {
 168             new Formatter(".", "bar");
 169             fail("new Formatter(\".\", \"bar\")");
 170         } catch (FileNotFoundException x) {
 171             pass();
 172         } catch (Exception x) {
 173             fail("new Formatter(\".\", \"bar\")", x);
 174         }
 175 
 176         // Formatter(String fileName, String csn, Locale l)
 177         try {
 178             Formatter f = new Formatter("foo", "ISO-8859-1", Locale.GERMANY);
 179             pass();
 180             out(f, BufferedWriter.class);
 181             locale(f, Locale.GERMANY);
 182         } catch (Exception x) {
 183             fail("new Formatter(\"foo\", \"ISO-8859-1\", Locale.GERMANY)", x);
 184         }
 185 
 186         try {
 187             Formatter f = new Formatter("foo", "ISO-8859-1", null);
 188             pass();
 189             locale(f, null);
 190             out(f, BufferedWriter.class);
 191         } catch (Exception x) {
 192             fail("new Formatter(\"foo\", \"ISO-8859-1\", null)", x);
 193         }
 194 
 195         // Formatter(File)
 196         try {
 197             Formatter f = new Formatter(new File("foo"));
 198             pass();
 199             locale(f);
 200             out(f, BufferedWriter.class);
 201         } catch (Exception x) {
 202             fail("new Formatter(new File(\"foo\")", x);
 203         }
 204 
 205         try {
 206             Formatter f = new Formatter((File)null);
 207             fail("new Formatter((File)null)");
 208         } catch (NullPointerException x) {
 209             pass();
 210         } catch (Exception x) {
 211             fail("new Formatter((File)null)", x);
 212         }
 213 
 214         // Formatter(PrintStream ps)
 215         try {
 216             // ambiguity detected at compile-time
 217             Formatter f = new Formatter(System.out);
 218             pass();
 219             out(f, PrintStream.class);
 220             locale(f);
 221         } catch (Exception x) {
 222             fail("new Formatter(System.out)", x);
 223         }
 224 
 225         try {
 226             new Formatter((PrintStream) null);
 227             fail("new Formatter((PrintStream) null)");
 228         } catch (NullPointerException x) {
 229             pass();
 230         } catch (Exception x) {
 231             fail("new Formatter((PrintStream) null)", x);
 232         }
 233 
 234         try {
 235             Formatter f = new Formatter(new PrintStream("foo"));
 236             pass();
 237             locale(f);
 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 {
 246             Formatter f = new Formatter(new PrintStream("foo"),
 247                                         Locale.JAPANESE);
 248             pass();
 249             locale(f, Locale.JAPANESE);
 250             out(f, PrintStream.class);
 251         } catch (FileNotFoundException x) {
 252             fail("new Formatter(new PrintStream(\"foo\")", x);
 253         } catch (Exception x) {
 254             fail("new Formatter(new PrintStream(\"foo\")", x);
 255         }
 256 
 257         try {
 258             // The cast here is necessary to avoid an ambiguity error
 259             // between Formatter(Appendable a, Locale l)
 260             // and     Formatter(OutputStream os, String csn)
 261             Formatter f = new Formatter(new PrintStream("foo"),
 262                                         (String)null);
 263             fail("new Formatter(new PrintStream(\"foo\"), (String)null)");
 264         } catch (FileNotFoundException x) {
 265             fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
 266         } catch (NullPointerException x) {
 267             pass();
 268         } catch (UnsupportedEncodingException x) {
 269             fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
 270         } catch (Exception x) {
 271             fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);
 272         }
 273 
 274         try {
 275             // The cast here is necessary to avoid an ambiguity error
 276             // between Formatter(Appendable a, Locale l)
 277             // and     Formatter(OutputStream os, String csn)
 278             Formatter f = new Formatter(new PrintStream("foo"),
 279                                         (Locale)null);
 280             pass();
 281             locale(f, null);
 282             out(f, PrintStream.class);
 283         } catch (FileNotFoundException x) {
 284             fail("new Formatter(new PrintStream(\"foo\"), (Locale)null)", x);
 285         } catch (Exception x) {
 286             fail("new Formatter(new PrintStream(\"foo\"), (Locale)null)", x);
 287         }
 288 
 289         try {
 290             Formatter f = new Formatter(new PrintStream("foo"),
 291                                         Locale.KOREAN);
 292             pass();
 293             locale(f, Locale.KOREAN);
 294             out(f, PrintStream.class);
 295         } catch (FileNotFoundException x) {
 296             fail("new Formatter(new PrintStream(\"foo\"), Locale.KOREAN)", x);
 297         } catch (Exception x) {
 298             fail("new Formatter(new PrintStream(\"foo\"), Locale.KOREAN)", x);
 299         }
 300 
 301         try {
 302             Formatter f = new Formatter(new PrintStream("foo"),
 303                                         "UTF-16BE", null);
 304             pass();
 305             locale(f, null);
 306             out(f, BufferedWriter.class);
 307         } catch (FileNotFoundException x) {
 308             fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");
 309         } catch (UnsupportedEncodingException x) {
 310             fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");
 311         } catch (Exception x) {
 312             fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");
 313         }
 314 
 315         try {
 316             Formatter f = new Formatter(new PrintStream("foo"),
 317                                         "UTF-16BE", Locale.ITALIAN);
 318             pass();
 319             locale(f, Locale.ITALIAN);
 320             out(f, BufferedWriter.class);
 321         } catch (FileNotFoundException x) {
 322             fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");
 323         } catch (UnsupportedEncodingException x) {
 324             fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");
 325         } catch (Exception x) {
 326             fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");
 327         }
 328 
 329         String csn = Charset.defaultCharset().newEncoder().canEncode('\u00a3') ?
 330             "ASCII" : "ISO-8859-1";
 331         try {
 332             ByteArrayOutputStream bs[] = { new ByteArrayOutputStream(),
 333                                            new ByteArrayOutputStream(),
 334                                            new ByteArrayOutputStream()
 335             };
 336             new Formatter((Appendable)  new PrintStream(bs[0], true, csn)).format("\u00a3");
 337             new Formatter((OutputStream)new PrintStream(bs[1], true, csn)).format("\u00a3");
 338             new Formatter(              new PrintStream(bs[2], true, csn)).format("\u00a3");
 339             if (Arrays.equals(bs[0].toByteArray(), bs[1].toByteArray())) {
 340                 fail("arrays shouldn't match: " + bs[0].toByteArray());
 341             } else {
 342                 pass();
 343             }
 344             if (! Arrays.equals(bs[0].toByteArray(), bs[2].toByteArray())) {
 345                 fail("arrays should match: " + bs[0].toByteArray() + " != "
 346                      + bs[2].toByteArray());
 347             } else {
 348                 pass();
 349             }
 350         } catch (UnsupportedEncodingException x) {
 351             fail("new PrintStream(newByteArrayOutputStream, true, " + csn + ")", x);
 352         }
 353 
 354         // Formatter(OutputStream os)
 355         try {
 356             new Formatter((OutputStream) null);
 357             fail("new Formatter((OutputStream) null)");
 358         } catch (NullPointerException x) {
 359             pass();
 360         } catch (Exception x) {
 361             fail("new Formatter((OutputStream) null)", x);
 362         }
 363 
 364         try {
 365             Formatter f = new Formatter((OutputStream) new PrintStream("foo"));
 366             pass();
 367             locale(f);
 368             out(f, BufferedWriter.class);
 369         } catch (Exception x) {
 370             fail("new Formatter((OutputStream) new PrintStream(\"foo\")", x);
 371         }
 372 
 373         // Formatter(OutputStream os, String csn)
 374         try {
 375             new Formatter((OutputStream) null, "ISO-8859-1");
 376             fail("new Formatter((OutputStream) null, \"ISO-8859-1\")");
 377         } catch (NullPointerException x) {
 378             pass();
 379         } catch (Exception x) {
 380             fail("new Formatter((OutputStream) null, \"ISO-8859-1\")", x);
 381         }
 382 
 383         try {
 384             new Formatter((OutputStream) new PrintStream("foo"), null);
 385             fail("new Formatter((OutputStream) new PrintStream(\"foo\"), null");
 386         } catch (NullPointerException x) {
 387             pass();
 388         } catch (Exception x) {
 389             fail("new Formatter((OutputStream) new PrintStream(\"foo\"), null",
 390                  x);
 391         }
 392 
 393         try {
 394             new Formatter(new PrintStream("foo"), "bar");
 395             fail("new Formatter(new PrintStream(\"foo\"), \"bar\")");
 396         } catch (UnsupportedEncodingException x) {
 397             pass();
 398         } catch (Exception x) {
 399             fail("new Formatter(new PrintStream(\"foo\"), \"bar\")", x);
 400         }
 401 
 402         try {
 403             Formatter f = new Formatter(new PrintStream("foo"), "UTF-8");
 404             pass();
 405             locale(f);
 406             out(f, BufferedWriter.class);
 407         } catch (Exception x) {
 408             fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\")", x);
 409         }
 410 
 411         // Formatter(OutputStream os, String csn, Locale l)
 412         try {
 413             new Formatter((OutputStream) null, "ISO-8859-1", Locale.UK);
 414             fail("new Formatter((OutputStream) null, \"ISO-8859-1\", Locale.UK)");
 415         } catch (NullPointerException x) {
 416             pass();
 417         } catch (Exception x) {
 418             fail("new Formatter((OutputStream) null, \"ISO-8859-1\", Locale.UK)",
 419                  x);
 420         }
 421 
 422         try {
 423             new Formatter(new PrintStream("foo"), null, Locale.UK);
 424             fail("new Formatter(new PrintStream(\"foo\"), null, Locale.UK)");
 425         } catch (NullPointerException x) {
 426             pass();
 427         } catch (Exception x) {
 428             fail("new Formatter(new PrintStream(\"foo\"), null, Locale.UK)",
 429                  x);
 430         }
 431 
 432         try {
 433             new Formatter(new PrintStream("foo"), "bar", Locale.UK);
 434             fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)");
 435         } catch (UnsupportedEncodingException x) {
 436             pass();
 437         } catch (Exception x) {
 438             fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)",
 439                  x);
 440         }
 441 
 442         try {
 443             Formatter f
 444                 = new Formatter(new PrintStream("foo"), "UTF-8", Locale.UK);
 445             pass();
 446             out(f, BufferedWriter.class);
 447             locale(f, Locale.UK);
 448         } catch (Exception x) {
 449             fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\"), Locale.UK",
 450                  x);
 451         }
 452 
 453         // PrintStream(String fileName)
 454         try {
 455             new PrintStream("foo");
 456             pass();
 457         } catch (Exception x) {
 458             fail("new PrintStream(\"foo\")", x);
 459         }
 460 
 461         // PrintStream(String fileName, String csn)
 462         try {
 463             new PrintStream("foo", null);
 464             fail("new PrintStream(\"foo\", null)");
 465         } catch (NullPointerException x) {
 466             pass();
 467         } catch (Exception x) {
 468             fail("new PrintStream(\"foo\", null)", x);
 469         }
 470 
 471         // PrintStream(File file)
 472         try {
 473             new PrintStream(new File("foo"));
 474             pass();
 475         } catch (Exception x) {
 476             fail("new PrintStream(new File(\"foo\"))", x);
 477         }
 478 
 479         // PrintStream(File file, String csn)
 480         try {
 481             new PrintStream(new File("foo"), null);
 482             fail("new PrintStream(new File(\"foo\"), null)");
 483         } catch (NullPointerException x) {
 484             pass();
 485         } catch (Exception x) {
 486             fail("new PrintStream(new File(\"foo\"), null)", x);
 487         }
 488 
 489         // PrintWriter(String fileName)
 490         try {
 491             new PrintWriter("foo");
 492             pass();
 493         } catch (Exception x) {
 494             fail("new PrintWriter(\"foo\")", x);
 495         }
 496 
 497         // PrintWriter(String fileName, String csn)
 498         try {
 499             new PrintWriter("foo", null);
 500             fail("new PrintWriter(\"foo\"), null");
 501         } catch (NullPointerException x) {
 502             pass();
 503         } catch (Exception x) {
 504             fail("new PrintWriter(\"foo\"), null", x);
 505         }
 506 
 507         // PrintWriter(File file)
 508         try {
 509             new PrintWriter(new File("foo"));
 510             pass();
 511         } catch (Exception x) {
 512             fail("new PrintWriter(new File(\"foo\"))", x);
 513         }
 514 
 515         // PrintWriter(File file, String csn)
 516         try {
 517             new PrintWriter(new File("foo"), null);
 518             fail("new PrintWriter(new File(\"foo\")), null");
 519         } catch (NullPointerException x) {
 520             pass();
 521         } catch (Exception x) {
 522             fail("new PrintWriter(new File(\"foo\")), null", x);
 523         }
 524 
 525         if (fail != 0)
 526             throw new RuntimeException((fail + pass) + " tests: "
 527                                        + fail + " failure(s), first", first);
 528         else
 529             System.out.println("all " + (fail + pass) + " tests passed");
 530     }
 531 }