Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/charset/spi/Test.java
38828 views
/*1* Copyright (c) 2010, 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.*;24import java.nio.charset.*;25import java.util.*;262728public class Test {2930private static PrintStream out = System.err;31private static final SortedMap available = Charset.availableCharsets();3233private static void fail(String csn, String msg) {34throw new RuntimeException(csn + ": " + msg);35}3637private static void testPositive(String csn) {38if (!Charset.isSupported(csn))39fail(csn, "Not supported");4041Charset cs = Charset.forName(csn);42out.println(csn + " --> " + cs.getClass().getName());43out.println(" " + cs.name() + " " + cs.aliases());4445if (!available.containsKey(cs.name()))46fail(csn, "Not in available charsets: " + available.keySet());47if (!((Charset)available.get(cs.name())).equals(cs))48fail(csn, "Available charset != looked-up charset");4950if (csn.equalsIgnoreCase("FOO")) {51if (!(cs instanceof FooCharset))52fail(csn, "instanceof failed");53}54}5556private static void testNegative(String csn) {57if (Charset.isSupported(csn))58fail(csn, "Supported");59if (available.containsKey(csn))60fail(csn, "Available");61try {62Charset.forName(csn);63} catch (UnsupportedCharsetException x) {64out.println(csn + " not supported, as expected");65return;66}67fail(csn, "Lookup succeeded");68}6970public static void main(String [] args) {7172out.println("Default: "73+ new InputStreamReader(System.in).getEncoding());7475out.print("Available:");76for (Iterator i = available.keySet().iterator(); i.hasNext();)77out.print(" " + (String)i.next());78out.println();7980for (int i = 0; i < args.length; i++) {81String a = args[i];82boolean not = a.startsWith("!");83String csn = (not ? a.substring(1) : a);84if (not)85testNegative(csn);86else87testPositive(csn);88}89}9091}929394