Path: blob/master/test/jdk/java/nio/charset/spi/CharsetTest.java
41152 views
/*1* Copyright (c) 2010, 2017, 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*/2223import java.io.InputStreamReader;24import java.io.PrintStream;25import java.nio.charset.Charset;26import java.nio.charset.UnsupportedCharsetException;27import java.util.Iterator;28import java.util.SortedMap;2930public class CharsetTest {3132private static PrintStream out = System.err;33private static final SortedMap available = Charset.availableCharsets();3435private static void fail(String csn, String msg) {36throw new RuntimeException(csn + ": " + msg);37}3839private static void testPositive(String csn) {40if (!Charset.isSupported(csn))41fail(csn, "Not supported");4243Charset cs = Charset.forName(csn);44out.println(csn + " --> " + cs.getClass().getName());45out.println(" " + cs.name() + " " + cs.aliases());4647if (!available.containsKey(cs.name()))48fail(csn, "Not in available charsets: " + available.keySet());49if (!((Charset)available.get(cs.name())).equals(cs))50fail(csn, "Available charset != looked-up charset");5152if (csn.equalsIgnoreCase("FOO")) {53if (!(cs instanceof FooCharset))54fail(csn, "instanceof failed");55}56}5758private static void testNegative(String csn) {59if (Charset.isSupported(csn))60fail(csn, "Supported");61if (available.containsKey(csn))62fail(csn, "Available");63try {64Charset.forName(csn);65} catch (UnsupportedCharsetException x) {66out.println(csn + " not supported, as expected");67return;68}69fail(csn, "Lookup succeeded");70}7172public static void main(String [] args) {7374out.println("Default: "75+ new InputStreamReader(System.in).getEncoding());7677out.print("Available:");78for (Iterator i = available.keySet().iterator(); i.hasNext();)79out.print(" " + (String)i.next());80out.println();8182for (int i = 0; i < args.length; i++) {83String a = args[i];84boolean not = a.startsWith("!");85String csn = (not ? a.substring(1) : a);86if (not)87testNegative(csn);88else89testPositive(csn);90}91}9293}949596