2115N/A/*
2362N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2115N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2115N/A *
2115N/A * This code is free software; you can redistribute it and/or modify it
2115N/A * under the terms of the GNU General Public License version 2 only, as
2115N/A * published by the Free Software Foundation.
2115N/A *
2115N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2115N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2115N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2115N/A * version 2 for more details (a copy is included in the LICENSE file that
2115N/A * accompanied this code).
2115N/A *
2115N/A * You should have received a copy of the GNU General Public License version
2115N/A * 2 along with this work; if not, write to the Free Software Foundation,
2115N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2115N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2115N/A */
2115N/A
2115N/Aimport java.io.*;
2115N/Aimport java.nio.charset.*;
2115N/Aimport java.util.*;
2115N/A
2115N/A
2115N/Apublic class Test {
2115N/A
2115N/A private static PrintStream out = System.err;
2115N/A private static final SortedMap available = Charset.availableCharsets();
2115N/A
2115N/A private static void fail(String csn, String msg) {
2115N/A throw new RuntimeException(csn + ": " + msg);
2115N/A }
2115N/A
2115N/A private static void testPositive(String csn) {
2115N/A if (!Charset.isSupported(csn))
2115N/A fail(csn, "Not supported");
2115N/A
2115N/A Charset cs = Charset.forName(csn);
2115N/A out.println(csn + " --> " + cs.getClass().getName());
2115N/A out.println(" " + cs.name() + " " + cs.aliases());
2115N/A
2115N/A if (!available.containsKey(cs.name()))
2115N/A fail(csn, "Not in available charsets: " + available.keySet());
2115N/A if (!((Charset)available.get(cs.name())).equals(cs))
2115N/A fail(csn, "Available charset != looked-up charset");
2115N/A
2115N/A if (csn.equalsIgnoreCase("FOO")) {
2115N/A if (!(cs instanceof FooCharset))
2115N/A fail(csn, "instanceof failed");
2115N/A }
2115N/A }
2115N/A
2115N/A private static void testNegative(String csn) {
2115N/A if (Charset.isSupported(csn))
2115N/A fail(csn, "Supported");
2115N/A if (available.containsKey(csn))
2115N/A fail(csn, "Available");
2115N/A try {
2115N/A Charset.forName(csn);
2115N/A } catch (UnsupportedCharsetException x) {
2115N/A out.println(csn + " not supported, as expected");
2115N/A return;
2115N/A }
2115N/A fail(csn, "Lookup succeeded");
2115N/A }
2115N/A
2115N/A public static void main(String [] args) {
2115N/A
2115N/A out.println("Default: "
2115N/A + new InputStreamReader(System.in).getEncoding());
2115N/A
2115N/A out.print("Available:");
2115N/A for (Iterator i = available.keySet().iterator(); i.hasNext();)
2115N/A out.print(" " + (String)i.next());
2115N/A out.println();
2115N/A
2115N/A for (int i = 0; i < args.length; i++) {
2115N/A String a = args[i];
2115N/A boolean not = a.startsWith("!");
2115N/A String csn = (not ? a.substring(1) : a);
2115N/A if (not)
2115N/A testNegative(csn);
2115N/A else
2115N/A testPositive(csn);
2115N/A }
2115N/A }
2115N/A
2115N/A}