Path: blob/master/test/jdk/java/util/Formatter/Constructors.java
41149 views
/*1* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 4981811 4984465 5064492 6240171 700051125* @summary Unit test for all constructors introduced by the formatter feature26*/2728import java.io.*;29import java.util.*;30import java.nio.charset.Charset;3132public class Constructors {3334private static int fail = 0;35private static int pass = 0;3637private static Throwable first;3839static void pass() {40pass++;41}4243static void fail(String fs) {44String s = "'" + fs + "': exception not thrown";45if (first == null)46first = new RuntimeException(s);47System.err.println("FAILED: " + s);48fail++;49}5051static void fail(String fs, Throwable ex) {52String s = "'" + fs + "': " + ex.getClass().getName() + " thrown";53if (first == null)54first = ex;55System.err.println("FAILED: " + s);56fail++;57}5859static void locale(Formatter f) {60locale(f, Locale.getDefault(Locale.Category.FORMAT));61}6263static void locale(Formatter f, Locale l) {64try {65if ((l != null && !l.equals(f.locale()))66|| (l == null && f.locale() != null))67throw new RuntimeException(f.locale() + " != " + l);68pass();69} catch (RuntimeException x) {70fail(x.getMessage());71}72}7374static void out(Formatter f, Class c) {75try {76Appendable a = f.out();77if (!c.isInstance(a))78throw new RuntimeException(a.getClass().getName()79+ " != " + c.getName());80pass();81} catch (RuntimeException x) {82fail(x.getMessage());83}84}8586public static void main(String [] args) {87// Formatter()88try (Formatter f = new Formatter()) {89pass();90out(f, StringBuilder.class);91locale(f);92} catch (Exception x) {93fail("new Formatter()", x);94}9596// Formatter(Appendable a)97try (Formatter f = new Formatter((Appendable) null)) {98pass();99out(f, StringBuilder.class);100locale(f);101} catch (Exception x) {102fail("new Formatter((Appendable)null)", x);103}104105// Formatter(Locale l)106try (Formatter f = new Formatter((Locale) null)) {107pass();108out(f, StringBuilder.class);109locale(f, null);110} catch (Exception x) {111fail("new Formatter((Locale)null)", x);112}113114// Formatter(Appendable a, Locale l)115try (Formatter f = new Formatter((Appendable) null, (Locale) null)) {116pass();117out(f, StringBuilder.class);118locale(f, null);119} catch (Exception x) {120fail("new Formatter((Appendable) null, (Locale) null)", x);121}122123// Formatter(String fileName)124try (Formatter f = new Formatter("foo")) {125pass();126out(f, BufferedWriter.class);127locale(f);128} catch (Exception x) {129fail("new Formatter(\"foo\")", x);130}131132try {133new Formatter((String)null);134fail("new Formatter((String)null)");135} catch (NullPointerException x) {136pass();137} catch (Exception x) {138fail("new Formatter((String)null)", x);139}140141// Formatter(String fileName, String csn)142try (Formatter f = new Formatter("foo", "UTF-8")) {143pass();144out(f, BufferedWriter.class);145locale(f);146} catch (Exception x) {147fail("new Formatter(\"foo\", \"UTF-8\")", x);148}149150try {151new Formatter("foo", "bar");152fail("new Formatter(\"foo\", \"bar\")");153} catch (UnsupportedEncodingException x) {154pass();155} catch (Exception x) {156fail("new Formatter(\"foo\", \"bar\")", x);157}158159try {160new Formatter(".", "bar");161fail("new Formatter(\".\", \"bar\")");162} catch (FileNotFoundException|UnsupportedEncodingException x) {163pass();164} catch (Exception x) {165fail("new Formatter(\".\", \"bar\")", x);166}167168// Formatter(String fileName, String csn, Locale l)169try (Formatter f = new Formatter("foo", "ISO-8859-1", Locale.GERMANY)) {170pass();171out(f, BufferedWriter.class);172locale(f, Locale.GERMANY);173} catch (Exception x) {174fail("new Formatter(\"foo\", \"ISO-8859-1\", Locale.GERMANY)", x);175}176177try (Formatter f = new Formatter("foo", "ISO-8859-1", null)) {178pass();179locale(f, null);180out(f, BufferedWriter.class);181} catch (Exception x) {182fail("new Formatter(\"foo\", \"ISO-8859-1\", null)", x);183}184185// Formatter(File)186try (Formatter f = new Formatter(new File("foo"))) {187pass();188locale(f);189out(f, BufferedWriter.class);190} catch (Exception x) {191fail("new Formatter(new File(\"foo\")", x);192}193194try {195new Formatter((File)null);196fail("new Formatter((File)null)");197} catch (NullPointerException x) {198pass();199} catch (Exception x) {200fail("new Formatter((File)null)", x);201}202203// Formatter(PrintStream ps)204try {205// ambiguity detected at compile-time206Formatter f = new Formatter(System.out);207pass();208out(f, PrintStream.class);209locale(f);210} catch (Exception x) {211fail("new Formatter(System.out)", x);212}213214try {215new Formatter((PrintStream) null);216fail("new Formatter((PrintStream) null)");217} catch (NullPointerException x) {218pass();219} catch (Exception x) {220fail("new Formatter((PrintStream) null)", x);221}222223try (Formatter f = new Formatter(new PrintStream("foo"))) {224pass();225locale(f);226out(f, PrintStream.class);227} catch (FileNotFoundException x) {228fail("new Formatter(new PrintStream(\"foo\")", x);229} catch (Exception x) {230fail("new Formatter(new PrintStream(\"foo\")", x);231}232233try (Formatter f = new Formatter(new PrintStream("foo"),234Locale.JAPANESE)) {235pass();236locale(f, Locale.JAPANESE);237out(f, PrintStream.class);238} catch (FileNotFoundException x) {239fail("new Formatter(new PrintStream(\"foo\")", x);240} catch (Exception x) {241fail("new Formatter(new PrintStream(\"foo\")", x);242}243244try (PrintStream ps = new PrintStream("foo")) {245// The cast here is necessary to avoid an ambiguity error246// between Formatter(Appendable a, Locale l)247// and Formatter(OutputStream os, String csn)248new Formatter(ps, (String)null);249fail("new Formatter(new PrintStream(\"foo\"), (String)null)");250} catch (FileNotFoundException x) {251fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);252} catch (NullPointerException x) {253pass();254} catch (UnsupportedEncodingException x) {255fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);256} catch (Exception x) {257fail("new Formatter(new PrintStream(\"foo\"), (String)null)", x);258}259260// The cast here is necessary to avoid an ambiguity error261// between Formatter(Appendable a, Locale l)262// and Formatter(OutputStream os, String csn)263try (Formatter f = new Formatter(new PrintStream("foo"),264(Locale)null)) {265pass();266locale(f, null);267out(f, PrintStream.class);268} catch (FileNotFoundException x) {269fail("new Formatter(new PrintStream(\"foo\"), (Locale)null)", x);270} catch (Exception x) {271fail("new Formatter(new PrintStream(\"foo\"), (Locale)null)", x);272}273274try (Formatter f = new Formatter(new PrintStream("foo"),275Locale.KOREAN)) {276pass();277locale(f, Locale.KOREAN);278out(f, PrintStream.class);279} catch (FileNotFoundException x) {280fail("new Formatter(new PrintStream(\"foo\"), Locale.KOREAN)", x);281} catch (Exception x) {282fail("new Formatter(new PrintStream(\"foo\"), Locale.KOREAN)", x);283}284285try (Formatter f = new Formatter(new PrintStream("foo"),286"UTF-16BE", null)) {287pass();288locale(f, null);289out(f, BufferedWriter.class);290} catch (FileNotFoundException x) {291fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");292} catch (UnsupportedEncodingException x) {293fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");294} catch (Exception x) {295fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", null");296}297298try (Formatter f = new Formatter(new PrintStream("foo"),299"UTF-16BE", Locale.ITALIAN)) {300pass();301locale(f, Locale.ITALIAN);302out(f, BufferedWriter.class);303} catch (FileNotFoundException x) {304fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");305} catch (UnsupportedEncodingException x) {306fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");307} catch (Exception x) {308fail("new Formatter(new PrintStream(\"foo\"), \"UTF-16BE\", Locale.ITALIAN");309}310311String csn = Charset.defaultCharset().newEncoder().canEncode('\u00a3') ?312"ASCII" : "ISO-8859-1";313try {314ByteArrayOutputStream bs[] = { new ByteArrayOutputStream(),315new ByteArrayOutputStream(),316new ByteArrayOutputStream()317};318new Formatter((Appendable) new PrintStream(bs[0], true, csn)).format("\u00a3");319new Formatter((OutputStream)new PrintStream(bs[1], true, csn)).format("\u00a3");320new Formatter( new PrintStream(bs[2], true, csn)).format("\u00a3");321if (Arrays.equals(bs[0].toByteArray(), bs[1].toByteArray())) {322fail("arrays shouldn't match: " + bs[0].toByteArray());323} else {324pass();325}326if (! Arrays.equals(bs[0].toByteArray(), bs[2].toByteArray())) {327fail("arrays should match: " + bs[0].toByteArray() + " != "328+ bs[2].toByteArray());329} else {330pass();331}332} catch (UnsupportedEncodingException x) {333fail("new PrintStream(newByteArrayOutputStream, true, " + csn + ")", x);334}335336// Formatter(OutputStream os)337try {338new Formatter((OutputStream) null);339fail("new Formatter((OutputStream) null)");340} catch (NullPointerException x) {341pass();342} catch (Exception x) {343fail("new Formatter((OutputStream) null)", x);344}345346try (Formatter f = new Formatter((OutputStream) new PrintStream("foo"))) {347pass();348locale(f);349out(f, BufferedWriter.class);350} catch (Exception x) {351fail("new Formatter((OutputStream) new PrintStream(\"foo\")", x);352}353354// Formatter(OutputStream os, String csn)355try {356new Formatter((OutputStream) null, "ISO-8859-1");357fail("new Formatter((OutputStream) null, \"ISO-8859-1\")");358} catch (NullPointerException x) {359pass();360} catch (Exception x) {361fail("new Formatter((OutputStream) null, \"ISO-8859-1\")", x);362}363364try (PrintStream ps = new PrintStream("foo")) {365new Formatter((OutputStream) ps, null);366fail("new Formatter((OutputStream) new PrintStream(\"foo\"), null");367} catch (NullPointerException x) {368pass();369} catch (Exception x) {370fail("new Formatter((OutputStream) new PrintStream(\"foo\"), null",371x);372}373374try (PrintStream ps = new PrintStream("foo")) {375new Formatter(ps, "bar");376fail("new Formatter(new PrintStream(\"foo\"), \"bar\")");377} catch (UnsupportedEncodingException x) {378pass();379} catch (Exception x) {380fail("new Formatter(new PrintStream(\"foo\"), \"bar\")", x);381}382383try (Formatter f = new Formatter(new PrintStream("foo"), "UTF-8")) {384pass();385locale(f);386out(f, BufferedWriter.class);387} catch (Exception x) {388fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\")", x);389}390391// Formatter(OutputStream os, String csn, Locale l)392try {393new Formatter((OutputStream) null, "ISO-8859-1", Locale.UK);394fail("new Formatter((OutputStream) null, \"ISO-8859-1\", Locale.UK)");395} catch (NullPointerException x) {396pass();397} catch (Exception x) {398fail("new Formatter((OutputStream) null, \"ISO-8859-1\", Locale.UK)",399x);400}401402try (PrintStream ps = new PrintStream("foo")) {403new Formatter(ps, (String)null, Locale.UK);404fail("new Formatter(new PrintStream(\"foo\"), (String)null, Locale.UK)");405} catch (NullPointerException x) {406pass();407} catch (Exception x) {408fail("new Formatter(new PrintStream(\"foo\"), (String)null, Locale.UK)",409x);410}411412// Formatter(OutputStream os, Charset charset, Locale l)413try (PrintStream ps = new PrintStream("foo")) {414new Formatter(ps, (Charset)null, Locale.UK);415fail("new Formatter(new PrintStream(\"foo\"), (Charset)null, Locale.UK)");416} catch (NullPointerException x) {417pass();418} catch (Exception x) {419fail("new Formatter(new PrintStream(\"foo\"), (Charset)null, Locale.UK)",420x);421}422423try (PrintStream ps = new PrintStream("foo")) {424new Formatter(ps, "bar", Locale.UK);425fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)");426} catch (UnsupportedEncodingException x) {427pass();428} catch (Exception x) {429fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)",430x);431}432433try (Formatter f = new Formatter(new PrintStream("foo"), "UTF-8", Locale.UK)) {434pass();435out(f, BufferedWriter.class);436locale(f, Locale.UK);437} catch (Exception x) {438fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\"), Locale.UK",439x);440}441442// PrintStream(String fileName)443try (PrintStream ps = new PrintStream("foo")) {444pass();445} catch (Exception x) {446fail("new PrintStream(\"foo\")", x);447}448449// PrintStream(String fileName, String csn)450try {451new PrintStream("foo", (String)null);452fail("new PrintStream(\"foo\", (String)null)");453} catch (NullPointerException x) {454pass();455} catch (Exception x) {456fail("new PrintStream(\"foo\", (String)null)", x);457}458459// PrintStream(String fileName, Charset charset)460try {461new PrintStream("foo", (Charset)null);462fail("new PrintStream(\"foo\", (Charset)null)");463} catch (NullPointerException x) {464pass();465} catch (Exception x) {466fail("new PrintStream(\"foo\", (Charset)null)", x);467}468469// PrintStream(File file)470try (PrintStream ps = new PrintStream(new File("foo"))) {471pass();472} catch (Exception x) {473fail("new PrintStream(new File(\"foo\"))", x);474}475476// PrintStream(File file, String csn)477try {478new PrintStream(new File("foo"), (String)null);479fail("new PrintStream(new File(\"foo\"), (String)null)");480} catch (NullPointerException x) {481pass();482} catch (Exception x) {483fail("new PrintStream(new File(\"foo\"), (String)null)", x);484}485486// PrintStream(File file, Charset charset)487try {488new PrintStream(new File("foo"), (Charset)null);489fail("new PrintStream(new File(\"foo\"), (Charset)null)");490} catch (NullPointerException x) {491pass();492} catch (Exception x) {493fail("new PrintStream(new File(\"foo\"), (Charset)null)", x);494}495496// PrintWriter(String fileName)497try (PrintWriter pw = new PrintWriter("foo")) {498pass();499} catch (Exception x) {500fail("new PrintWriter(\"foo\")", x);501}502503// PrintWriter(String fileName, String csn)504try {505new PrintWriter("foo", (String)null);506fail("new PrintWriter(\"foo\"), (String)null");507} catch (NullPointerException x) {508pass();509} catch (Exception x) {510fail("new PrintWriter(\"foo\"), (String)null", x);511}512513// PrintWriter(String fileName, Charset charset)514try {515new PrintWriter("foo", (Charset)null);516fail("new PrintWriter(\"foo\"), (Charset)null");517} catch (NullPointerException x) {518pass();519} catch (Exception x) {520fail("new PrintWriter(\"foo\"), (Charset)null", x);521}522523// PrintWriter(File file)524try (PrintWriter pw = new PrintWriter(new File("foo"))) {525pass();526} catch (Exception x) {527fail("new PrintWriter(new File(\"foo\"))", x);528}529530// PrintWriter(File file, String csn)531try {532new PrintWriter(new File("foo"), (String)null);533fail("new PrintWriter(new File(\"foo\")), (String)null");534} catch (NullPointerException x) {535pass();536} catch (Exception x) {537fail("new PrintWriter(new File(\"foo\")), (String)null", x);538}539540// PrintWriter(File file, Charset charset)541try {542new PrintWriter(new File("foo"), (Charset)null);543fail("new PrintWriter(new File(\"foo\")), (Charset)null");544} catch (NullPointerException x) {545pass();546} catch (Exception x) {547fail("new PrintWriter(new File(\"foo\")), (Charset)null", x);548}549550if (fail != 0)551throw new RuntimeException((fail + pass) + " tests: "552+ fail + " failure(s), first", first);553else554System.out.println("all " + (fail + pass) + " tests passed");555}556}557558559