jdk/test/java/nio/charset/StandardCharsets/Standard.java
author katleman
Thu, 05 Jan 2012 08:42:37 -0800
changeset 11376 075fe3928b7f
parent 9685 b9cfe747eaa0
child 24692 268fbc344d53
permissions -rw-r--r--
Added tag jdk8-b20 for changeset 90e14ec89395
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9526
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
     1
/*
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
     4
 *
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
     8
 *
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    13
 * accompanied this code).
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    14
 *
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    18
 *
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    21
 * questions.
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    22
 */
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    23
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    24
/*
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    25
 * @test
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    26
 * @bug 4884238
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    27
 * @summary Test standard charset name constants.
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    28
 * @author Mike Duigou
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    29
 * @run main Standard
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    30
 */
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    31
9540
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    32
import java.lang.reflect.Field;
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    33
import java.lang.reflect.Modifier;
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    34
import java.io.*;
9526
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    35
import java.nio.charset.*;
9540
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    36
import java.util.Arrays;
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    37
import java.util.HashSet;
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    38
import java.util.Set;
9526
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    39
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    40
public class Standard {
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    41
9540
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    42
    private final static String standardCharsets[] = {
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    43
        "US-ASCII", "ISO-8859-1", "UTF-8",
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    44
        "UTF-16BE", "UTF-16LE", "UTF-16" };
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    45
9526
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    46
    public static void realMain(String[] args) {
9685
b9cfe747eaa0 7043513: Update test for StandardCharsets
mduigou
parents: 9540
diff changeset
    47
        check(StandardCharsets.US_ASCII instanceof Charset);
b9cfe747eaa0 7043513: Update test for StandardCharsets
mduigou
parents: 9540
diff changeset
    48
        check(StandardCharsets.ISO_8859_1 instanceof Charset);
b9cfe747eaa0 7043513: Update test for StandardCharsets
mduigou
parents: 9540
diff changeset
    49
        check(StandardCharsets.UTF_8 instanceof Charset);
b9cfe747eaa0 7043513: Update test for StandardCharsets
mduigou
parents: 9540
diff changeset
    50
        check(StandardCharsets.UTF_16BE instanceof Charset);
b9cfe747eaa0 7043513: Update test for StandardCharsets
mduigou
parents: 9540
diff changeset
    51
        check(StandardCharsets.UTF_16LE instanceof Charset);
b9cfe747eaa0 7043513: Update test for StandardCharsets
mduigou
parents: 9540
diff changeset
    52
        check(StandardCharsets.UTF_16 instanceof Charset);
9526
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    53
9685
b9cfe747eaa0 7043513: Update test for StandardCharsets
mduigou
parents: 9540
diff changeset
    54
        check("US-ASCII".equals(StandardCharsets.US_ASCII.name()));
b9cfe747eaa0 7043513: Update test for StandardCharsets
mduigou
parents: 9540
diff changeset
    55
        check("ISO-8859-1".equals(StandardCharsets.ISO_8859_1.name()));
b9cfe747eaa0 7043513: Update test for StandardCharsets
mduigou
parents: 9540
diff changeset
    56
        check("UTF-8".equals(StandardCharsets.UTF_8.name()));
b9cfe747eaa0 7043513: Update test for StandardCharsets
mduigou
parents: 9540
diff changeset
    57
        check("UTF-16BE".equals(StandardCharsets.UTF_16BE.name()));
b9cfe747eaa0 7043513: Update test for StandardCharsets
mduigou
parents: 9540
diff changeset
    58
        check("UTF-16LE".equals(StandardCharsets.UTF_16LE.name()));
b9cfe747eaa0 7043513: Update test for StandardCharsets
mduigou
parents: 9540
diff changeset
    59
        check("UTF-16".equals(StandardCharsets.UTF_16.name()));
9540
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    60
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    61
        Set<String> charsets = new HashSet<>();
9685
b9cfe747eaa0 7043513: Update test for StandardCharsets
mduigou
parents: 9540
diff changeset
    62
        Field standardCharsetFields[] = StandardCharsets.class.getFields();
9540
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    63
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    64
        for(Field charsetField : standardCharsetFields) {
9685
b9cfe747eaa0 7043513: Update test for StandardCharsets
mduigou
parents: 9540
diff changeset
    65
            check(StandardCharsets.class == charsetField.getDeclaringClass());
9540
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    66
            check(Modifier.isFinal(charsetField.getModifiers()));
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    67
            check(Modifier.isStatic(charsetField.getModifiers()));
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    68
            check(Modifier.isPublic(charsetField.getModifiers()));
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    69
            Object value;
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    70
            try {
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    71
                value = charsetField.get(null);
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    72
            } catch(IllegalAccessException failure) {
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    73
                unexpected(failure);
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    74
                continue;
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    75
            }
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    76
            check(value instanceof Charset);
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    77
            charsets.add(((Charset)value).name());
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    78
        }
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    79
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    80
        check(charsets.containsAll(Arrays.asList(standardCharsets)));
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    81
        charsets.removeAll(Arrays.asList(standardCharsets));
2cb19a76b1bf 7040572: Fix broken java/nio/charset/StandardCharset/Standard.java and add more tests.
mduigou
parents: 9526
diff changeset
    82
        check(charsets.isEmpty());
9526
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    83
    }
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    84
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    85
    //--------------------- Infrastructure ---------------------------
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    86
    static volatile int passed = 0, failed = 0;
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    87
    static void pass() { passed++; }
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    88
    static void fail() { failed++; Thread.dumpStack(); }
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    89
    static void fail(String msg) { System.out.println(msg); fail(); }
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    90
    static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    91
    static void check(boolean cond) { if (cond) pass(); else fail(); }
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    92
    static void equal(Object x, Object y) {
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    93
        if (x == null ? y == null : x.equals(y)) pass();
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    94
        else {System.out.println(x + " not equal to " + y); fail();}}
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    95
    static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    96
    public static void main(String[] args) throws Throwable {
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    97
        try { realMain(args); } catch (Throwable t) { unexpected(t); }
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    98
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
    99
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   100
        if (failed > 0) throw new Exception("Some tests failed");
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   101
    }
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   102
    private static abstract class Fun {abstract void f() throws Throwable;}
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   103
    private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   104
          for (Fun f : fs)
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   105
              try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   106
              catch (Throwable t) {
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   107
                  if (k.isAssignableFrom(t.getClass())) pass();
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   108
                  else unexpected(t);}}
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   109
    static byte[] serializedForm(Object obj) {
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   110
        try {
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   111
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   112
            new ObjectOutputStream(baos).writeObject(obj);
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   113
            return baos.toByteArray();
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   114
        } catch (IOException e) { throw new Error(e); }}
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   115
    static Object readObject(byte[] bytes)
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   116
        throws IOException, ClassNotFoundException {
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   117
        InputStream is = new ByteArrayInputStream(bytes);
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   118
        return new ObjectInputStream(is).readObject();}
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   119
    @SuppressWarnings("unchecked")
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   120
    static <T> T serialClone(T obj) {
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   121
        try { return (T) readObject(serializedForm(obj)); }
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   122
        catch (Exception e) { throw new Error(e); }}
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   123
a80328f995f1 4884238: Adds java.nio.charset.StandardCharset to provide static final constants for the standard charsets.
mduigou
parents:
diff changeset
   124
}