Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Formatter/Constructors.java
38812 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, null, Locale.UK);404fail("new Formatter(new PrintStream(\"foo\"), null, Locale.UK)");405} catch (NullPointerException x) {406pass();407} catch (Exception x) {408fail("new Formatter(new PrintStream(\"foo\"), null, Locale.UK)",409x);410}411412try (PrintStream ps = new PrintStream("foo")) {413new Formatter(ps, "bar", Locale.UK);414fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)");415} catch (UnsupportedEncodingException x) {416pass();417} catch (Exception x) {418fail("new Formatter(new PrintStream(\"foo\"), \"bar\", Locale.UK)",419x);420}421422try (Formatter f = new Formatter(new PrintStream("foo"), "UTF-8", Locale.UK)) {423pass();424out(f, BufferedWriter.class);425locale(f, Locale.UK);426} catch (Exception x) {427fail("new Formatter(new PrintStream(\"foo\"), \"UTF-8\"), Locale.UK",428x);429}430431// PrintStream(String fileName)432try (PrintStream ps = new PrintStream("foo")) {433pass();434} catch (Exception x) {435fail("new PrintStream(\"foo\")", x);436}437438// PrintStream(String fileName, String csn)439try {440new PrintStream("foo", null);441fail("new PrintStream(\"foo\", null)");442} catch (NullPointerException x) {443pass();444} catch (Exception x) {445fail("new PrintStream(\"foo\", null)", x);446}447448// PrintStream(File file)449try (PrintStream ps = new PrintStream(new File("foo"))) {450pass();451} catch (Exception x) {452fail("new PrintStream(new File(\"foo\"))", x);453}454455// PrintStream(File file, String csn)456try {457new PrintStream(new File("foo"), null);458fail("new PrintStream(new File(\"foo\"), null)");459} catch (NullPointerException x) {460pass();461} catch (Exception x) {462fail("new PrintStream(new File(\"foo\"), null)", x);463}464465// PrintWriter(String fileName)466try (PrintWriter pw = new PrintWriter("foo")) {467pass();468} catch (Exception x) {469fail("new PrintWriter(\"foo\")", x);470}471472// PrintWriter(String fileName, String csn)473try {474new PrintWriter("foo", null);475fail("new PrintWriter(\"foo\"), null");476} catch (NullPointerException x) {477pass();478} catch (Exception x) {479fail("new PrintWriter(\"foo\"), null", x);480}481482// PrintWriter(File file)483try (PrintWriter pw = new PrintWriter(new File("foo"))) {484pass();485} catch (Exception x) {486fail("new PrintWriter(new File(\"foo\"))", x);487}488489// PrintWriter(File file, String csn)490try {491new PrintWriter(new File("foo"), null);492fail("new PrintWriter(new File(\"foo\")), null");493} catch (NullPointerException x) {494pass();495} catch (Exception x) {496fail("new PrintWriter(new File(\"foo\")), null", x);497}498499if (fail != 0)500throw new RuntimeException((fail + pass) + " tests: "501+ fail + " failure(s), first", first);502else503System.out.println("all " + (fail + pass) + " tests passed");504}505}506507508