Constructors.java revision 3261:a06412e13bf7
1893SN/A/* 213769Salanb * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 3893SN/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4893SN/A * 5893SN/A * This code is free software; you can redistribute it and/or modify it 6893SN/A * under the terms of the GNU General Public License version 2 only, as 72362SN/A * published by the Free Software Foundation. 8893SN/A * 92362SN/A * This code is distributed in the hope that it will be useful, but WITHOUT 10893SN/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11893SN/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12893SN/A * version 2 for more details (a copy is included in the LICENSE file that 13893SN/A * accompanied this code). 14893SN/A * 15893SN/A * You should have received a copy of the GNU General Public License version 16893SN/A * 2 along with this work; if not, write to the Free Software Foundation, 17893SN/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18893SN/A * 19893SN/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20893SN/A * or visit www.oracle.com if you need additional information or have any 212362SN/A * questions. 222362SN/A */ 232362SN/A 24893SN/A/* @test 25893SN/A * @bug 4981811 4984465 5064492 6240171 26893SN/A * @summary Unit test for all constructors introduced by the formatter feature 27893SN/A */ 28893SN/A 29893SN/Aimport java.io.*; 30893SN/Aimport java.util.*; 314216SN/Aimport java.nio.charset.Charset; 32893SN/A 33893SN/Apublic class Constructors { 34893SN/A 35893SN/A private static int fail = 0; 36893SN/A private static int pass = 0; 37893SN/A 381580SN/A private static Throwable first; 39893SN/A 40893SN/A static void pass() { 411040SN/A pass++; 42893SN/A } 43893SN/A 44893SN/A static void fail(String fs) { 45893SN/A String s = "'" + fs + "': exception not thrown"; 46893SN/A if (first == null) 47893SN/A first = new RuntimeException(s); 48893SN/A System.err.println("FAILED: " + s); 49893SN/A fail++; 50893SN/A } 51893SN/A 52893SN/A static void fail(String fs, Throwable ex) { 53893SN/A String s = "'" + fs + "': " + ex.getClass().getName() + " thrown"; 5413344Sredestad if (first == null) 55893SN/A first = ex; 56893SN/A System.err.println("FAILED: " + s); 57893SN/A fail++; 58893SN/A } 59893SN/A 60893SN/A static void locale(Formatter f) { 6113344Sredestad locale(f, Locale.getDefault(Locale.Category.FORMAT)); 62893SN/A } 63893SN/A 64893SN/A static void locale(Formatter f, Locale l) { 65893SN/A try { 667401SN/A if ((l != null && !l.equals(f.locale())) 677401SN/A || (l == null && f.locale() != null)) 68893SN/A throw new RuntimeException(f.locale() + " != " + l); 69893SN/A pass(); 70893SN/A } catch (RuntimeException x) { 71893SN/A fail(x.getMessage()); 72893SN/A } 73893SN/A } 74893SN/A 75893SN/A static void out(Formatter f, Class c) { 7613344Sredestad try { 77893SN/A Appendable a = f.out(); 78893SN/A if (!c.isInstance(a)) 79893SN/A throw new RuntimeException(a.getClass().getName() 80893SN/A + " != " + c.getName()); 81893SN/A pass(); 82893SN/A } catch (RuntimeException x) { 83893SN/A fail(x.getMessage()); 84893SN/A } 85893SN/A } 86893SN/A 87893SN/A public static void main(String [] args) { 88893SN/A 89893SN/A // Formatter() 90893SN/A try { 91893SN/A Formatter f = new Formatter(); 92893SN/A pass(); 93893SN/A out(f, StringBuilder.class); 94893SN/A locale(f); 95893SN/A } catch (Exception x) { 96893SN/A fail("new Formatter()", x); 97893SN/A } 98893SN/A 99893SN/A // Formatter(Appendable a) 100893SN/A try { 101893SN/A Formatter f = new Formatter((Appendable) null); 102893SN/A pass(); 103893SN/A out(f, StringBuilder.class); 104893SN/A locale(f); 10513344Sredestad } catch (Exception x) { 106893SN/A fail("new Formatter((Appendable)null)", x); 10713344Sredestad } 108893SN/A 109893SN/A // Formatter(Locale l) 110893SN/A try { 111893SN/A Formatter f = new Formatter((Locale) null); 112893SN/A pass(); 113893SN/A out(f, StringBuilder.class); 1141580SN/A locale(f, null); 1151580SN/A } catch (Exception x) { 1161580SN/A fail("new Formatter((Locale)null)", x); 1171580SN/A } 1181580SN/A 1191580SN/A // Formatter(Appendable a, Locale l) 1201580SN/A try { 1211580SN/A Formatter f = new Formatter((Appendable) null, (Locale) null); 1221580SN/A pass(); 1231580SN/A out(f, StringBuilder.class); 1241580SN/A locale(f, null); 1251580SN/A } catch (Exception x) { 1261580SN/A fail("new Formatter((Appendable) null, (Locale) null)", x); 1271580SN/A } 1281580SN/A 1291580SN/A // Formatter(String fileName) 1301580SN/A try { 1311580SN/A Formatter f = new Formatter("foo"); 1321580SN/A pass(); 1331580SN/A out(f, BufferedWriter.class); 1341580SN/A locale(f); 1351580SN/A } catch (Exception x) { 1361580SN/A fail("new Formatter(\"foo\")", x); 137893SN/A } 138893SN/A 139893SN/A try { 140893SN/A Formatter f = new Formatter((String)null); 141893SN/A fail("new Formatter((String)null)"); 142893SN/A } catch (NullPointerException x) { 143893SN/A pass(); 144893SN/A } catch (Exception x) { 145893SN/A fail("new Formatter((String)null)", x); 146893SN/A } 147893SN/A 148893SN/A // Formatter(String fileName, String csn) 149893SN/A try { 150893SN/A Formatter f = new Formatter("foo", "UTF-8"); 151893SN/A pass(); 152893SN/A out(f, BufferedWriter.class); 153893SN/A locale(f); 154893SN/A } catch (Exception x) { 155893SN/A fail("new Formatter(\"foo\", \"UTF-8\")", x); 156893SN/A } 157893SN/A 158893SN/A try { 159893SN/A new Formatter("foo", "bar"); 160893SN/A fail("new Formatter(\"foo\", \"bar\")"); 1611040SN/A } catch (UnsupportedEncodingException x) { 162893SN/A pass(); 163893SN/A } catch (Exception x) { 164893SN/A fail("new Formatter(\"foo\", \"bar\")", x); 165893SN/A } 166893SN/A 167893SN/A try { 168893SN/A new Formatter(".", "bar"); 169893SN/A fail("new Formatter(\".\", \"bar\")"); 170893SN/A } catch (FileNotFoundException x) { 171893SN/A pass(); 172893SN/A } catch (Exception x) { 173893SN/A fail("new Formatter(\".\", \"bar\")", x); 174893SN/A } 175893SN/A 1767421SN/A // Formatter(String fileName, String csn, Locale l) 177893SN/A try { 178893SN/A Formatter f = new Formatter("foo", "ISO-8859-1", Locale.GERMANY); 179893SN/A pass(); 180893SN/A out(f, BufferedWriter.class); 181893SN/A locale(f, Locale.GERMANY); 182893SN/A } catch (Exception x) { 183893SN/A fail("new Formatter(\"foo\", \"ISO-8859-1\", Locale.GERMANY)", x); 184893SN/A } 185893SN/A 186893SN/A try { 187893SN/A Formatter f = new Formatter("foo", "ISO-8859-1", null); 188893SN/A pass(); 189893SN/A locale(f, null); 190893SN/A out(f, BufferedWriter.class); 1917401SN/A } catch (Exception x) { 1927401SN/A fail("new Formatter(\"foo\", \"ISO-8859-1\", null)", x); 1937401SN/A } 1947401SN/A 1957401SN/A // Formatter(File) 1967401SN/A try { 1977401SN/A Formatter f = new Formatter(new File("foo")); 1987401SN/A pass(); 199893SN/A locale(f); 200893SN/A out(f, BufferedWriter.class); 201893SN/A } catch (Exception x) { 202893SN/A fail("new Formatter(new File(\"foo\")", x); 203893SN/A } 204893SN/A 205893SN/A try { 206893SN/A Formatter f = new Formatter((File)null); 207893SN/A fail("new Formatter((File)null)"); 208893SN/A } catch (NullPointerException x) { 209893SN/A pass(); 210893SN/A } catch (Exception x) { 211893SN/A fail("new Formatter((File)null)", x); 212893SN/A } 213893SN/A 214893SN/A // Formatter(PrintStream ps) 2157401SN/A try { 2167401SN/A // ambiguity detected at compile-time 2177401SN/A Formatter f = new Formatter(System.out); 2187401SN/A pass(); 2197401SN/A out(f, PrintStream.class); 2207401SN/A locale(f); 221893SN/A } catch (Exception x) { 222893SN/A fail("new Formatter(System.out)", x); 223893SN/A } 224893SN/A 225893SN/A try { 226893SN/A new Formatter((PrintStream) null); 227893SN/A fail("new Formatter((PrintStream) null)"); 228893SN/A } catch (NullPointerException x) { 229893SN/A pass(); 230893SN/A } catch (Exception x) { 23111805Sdarcy fail("new Formatter((PrintStream) null)", x); 2324216SN/A } 2334216SN/A 23413769Salanb try { 23513769Salanb Formatter f = new Formatter(new PrintStream("foo")); 23613769Salanb pass(); 237893SN/A locale(f); 238893SN/A out(f, PrintStream.class); 239893SN/A } catch (FileNotFoundException x) { 240893SN/A fail("new Formatter(new PrintStream(\"foo\")", x); 241893SN/A } catch (Exception x) { 242893SN/A fail("new Formatter(new PrintStream(\"foo\")", x); 243893SN/A } 244893SN/A 245893SN/A try { 246893SN/A Formatter f = new Formatter(new PrintStream("foo"), 247893SN/A Locale.JAPANESE); 248893SN/A pass(); 249893SN/A locale(f, Locale.JAPANESE); 250893SN/A out(f, PrintStream.class); 251893SN/A } catch (FileNotFoundException x) { 252893SN/A fail("new Formatter(new PrintStream(\"foo\")", x); 253893SN/A } catch (Exception x) { 254893SN/A fail("new Formatter(new PrintStream(\"foo\")", x); 255893SN/A } 256893SN/A 2577421SN/A try { 258893SN/A // The cast here is necessary to avoid an ambiguity error 259893SN/A // between Formatter(Appendable a, Locale l) 260893SN/A // and Formatter(OutputStream os, String csn) 261893SN/A Formatter f = new Formatter(new PrintStream("foo"), 262893SN/A (String)null); 263893SN/A 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} 532