1 /*
   2  * Copyright (c) 2011, 2014, 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.lang.reflect.Field;
  33 import java.lang.reflect.Modifier;
  34 import java.io.*;
  35 import java.nio.charset.*;
  36 import java.util.Arrays;
  37 import java.util.HashSet;
  38 import java.util.Set;
  39 
  40 public class Standard {
  41 
  42     private final static String standardCharsets[] = {
  43         "US-ASCII", "ISO-8859-1", "UTF-8",
  44         "UTF-16BE", "UTF-16LE", "UTF-16" };
  45 
  46     public static void realMain(String[] args) {
  47         check(StandardCharsets.US_ASCII instanceof Charset);
  48         check(StandardCharsets.ISO_8859_1 instanceof Charset);
  49         check(StandardCharsets.UTF_8 instanceof Charset);
  50         check(StandardCharsets.UTF_16BE instanceof Charset);
  51         check(StandardCharsets.UTF_16LE instanceof Charset);
  52         check(StandardCharsets.UTF_16 instanceof Charset);
  53 
  54         check("US-ASCII".equals(StandardCharsets.US_ASCII.name()));
  55         check("ISO-8859-1".equals(StandardCharsets.ISO_8859_1.name()));
  56         check("UTF-8".equals(StandardCharsets.UTF_8.name()));
  57         check("UTF-16BE".equals(StandardCharsets.UTF_16BE.name()));
  58         check("UTF-16LE".equals(StandardCharsets.UTF_16LE.name()));
  59         check("UTF-16".equals(StandardCharsets.UTF_16.name()));
  60 
  61         check(Charset.forName("US-ASCII") == StandardCharsets.US_ASCII);
  62         check(Charset.forName("ISO-8859-1") == StandardCharsets.ISO_8859_1);
  63         check(Charset.forName("UTF-8") == StandardCharsets.UTF_8);
  64         check(Charset.forName("UTF-16BE") == StandardCharsets.UTF_16BE);
  65         check(Charset.forName("UTF-16LE") == StandardCharsets.UTF_16LE);
  66         check(Charset.forName("UTF-16") == StandardCharsets.UTF_16);
  67 
  68         Set<String> charsets = new HashSet<>();
  69         Field standardCharsetFields[] = StandardCharsets.class.getFields();
  70 
  71         for(Field charsetField : standardCharsetFields) {
  72             check(StandardCharsets.class == charsetField.getDeclaringClass());
  73             check(Modifier.isFinal(charsetField.getModifiers()));
  74             check(Modifier.isStatic(charsetField.getModifiers()));
  75             check(Modifier.isPublic(charsetField.getModifiers()));
  76             Object value;
  77             try {
  78                 value = charsetField.get(null);
  79             } catch(IllegalAccessException failure) {
  80                 unexpected(failure);
  81                 continue;
  82             }
  83             check(value instanceof Charset);
  84             charsets.add(((Charset)value).name());
  85         }
  86 
  87         check(charsets.containsAll(Arrays.asList(standardCharsets)));
  88         charsets.removeAll(Arrays.asList(standardCharsets));
  89         check(charsets.isEmpty());
  90     }
  91 
  92     //--------------------- Infrastructure ---------------------------
  93     static volatile int passed = 0, failed = 0;
  94     static void pass() { passed++; }
  95     static void fail() { failed++; Thread.dumpStack(); }
  96     static void fail(String msg) { System.out.println(msg); fail(); }
  97     static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
  98     static void check(boolean cond) { if (cond) pass(); else fail(); }
  99     static void equal(Object x, Object y) {
 100         if (x == null ? y == null : x.equals(y)) pass();
 101         else {System.out.println(x + " not equal to " + y); fail();}}
 102     static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}
 103     public static void main(String[] args) throws Throwable {
 104         try { realMain(args); } catch (Throwable t) { unexpected(t); }
 105 
 106         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 107         if (failed > 0) throw new Exception("Some tests failed");
 108     }
 109     static byte[] serializedForm(Object obj) {
 110         try {
 111             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 112             new ObjectOutputStream(baos).writeObject(obj);
 113             return baos.toByteArray();
 114         } catch (IOException e) { throw new Error(e); }}
 115     static Object readObject(byte[] bytes)
 116         throws IOException, ClassNotFoundException {
 117         InputStream is = new ByteArrayInputStream(bytes);
 118         return new ObjectInputStream(is).readObject();}
 119     @SuppressWarnings("unchecked")
 120     static <T> T serialClone(T obj) {
 121         try { return (T) readObject(serializedForm(obj)); }
 122         catch (Exception e) { throw new Error(e); }}
 123 
 124 }