jdk/test/java/nio/charset/StandardCharset/Standard.java
changeset 9526 a80328f995f1
child 9540 2cb19a76b1bf
equal deleted inserted replaced
9521:e18f95eadb3c 9526:a80328f995f1
       
     1 /*
       
     2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 4884238
       
    27  * @summary Test standard charset name constants.
       
    28  * @author Mike Duigou
       
    29  * @run main Standard
       
    30  */
       
    31 
       
    32 import java.nio.charset.*;
       
    33 
       
    34 public class Standard {
       
    35 
       
    36     public static void realMain(String[] args) {
       
    37         check(StandardCharset.US_ASCII instanceof Charset);
       
    38         check(StandardCharset.ISO_8859_1 instanceof Charset);
       
    39         check(StandardCharset.UTF_8 instanceof Charset);
       
    40         check(StandardCharset.UTF_16BE instanceof Charset);
       
    41         check(StandardCharset.UTF_16LE instanceof Charset);
       
    42         check(StandardCharset.UTF_16 instanceof Charset);
       
    43 
       
    44         check("US-ASCII".equals(StandardCharset.US_ASCII.name());
       
    45         check("ISO-8859-1".equals(StandardCharset.ISO_8859_1.name());
       
    46         check("UTF-8".equals(StandardCharset.UTF_8.name());
       
    47         check("UTF-16BE".equals(StandardCharset.UTF_16BE.name());
       
    48         check("UTF-16LE".equals(StandardCharset.UTF_16LE.name());
       
    49         check("UTF-16".equals(StandardCharset.UTF_16.name());
       
    50     }
       
    51 
       
    52     //--------------------- Infrastructure ---------------------------
       
    53     static volatile int passed = 0, failed = 0;
       
    54     static void pass() { passed++; }
       
    55     static void fail() { failed++; Thread.dumpStack(); }
       
    56     static void fail(String msg) { System.out.println(msg); fail(); }
       
    57     static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
       
    58     static void check(boolean cond) { if (cond) pass(); else fail(); }
       
    59     static void equal(Object x, Object y) {
       
    60         if (x == null ? y == null : x.equals(y)) pass();
       
    61         else {System.out.println(x + " not equal to " + y); fail();}}
       
    62     static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}
       
    63     public static void main(String[] args) throws Throwable {
       
    64         try { realMain(args); } catch (Throwable t) { unexpected(t); }
       
    65 
       
    66         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
       
    67         if (failed > 0) throw new Exception("Some tests failed");
       
    68     }
       
    69     private static abstract class Fun {abstract void f() throws Throwable;}
       
    70     private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
       
    71           for (Fun f : fs)
       
    72               try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
       
    73               catch (Throwable t) {
       
    74                   if (k.isAssignableFrom(t.getClass())) pass();
       
    75                   else unexpected(t);}}
       
    76     static byte[] serializedForm(Object obj) {
       
    77         try {
       
    78             ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
    79             new ObjectOutputStream(baos).writeObject(obj);
       
    80             return baos.toByteArray();
       
    81         } catch (IOException e) { throw new Error(e); }}
       
    82     static Object readObject(byte[] bytes)
       
    83         throws IOException, ClassNotFoundException {
       
    84         InputStream is = new ByteArrayInputStream(bytes);
       
    85         return new ObjectInputStream(is).readObject();}
       
    86     @SuppressWarnings("unchecked")
       
    87     static <T> T serialClone(T obj) {
       
    88         try { return (T) readObject(serializedForm(obj)); }
       
    89         catch (Exception e) { throw new Error(e); }}
       
    90 
       
    91 }