Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/charset/StandardCharsets/Standard.java
38828 views
/*1* Copyright (c) 2011, 2014, 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*/2223/*24* @test25* @bug 488423826* @summary Test standard charset name constants.27* @author Mike Duigou28* @run main Standard29*/3031import java.lang.reflect.Field;32import java.lang.reflect.Modifier;33import java.io.*;34import java.nio.charset.*;35import java.util.Arrays;36import java.util.HashSet;37import java.util.Set;3839public class Standard {4041private final static String standardCharsets[] = {42"US-ASCII", "ISO-8859-1", "UTF-8",43"UTF-16BE", "UTF-16LE", "UTF-16" };4445public static void realMain(String[] args) {46check(StandardCharsets.US_ASCII instanceof Charset);47check(StandardCharsets.ISO_8859_1 instanceof Charset);48check(StandardCharsets.UTF_8 instanceof Charset);49check(StandardCharsets.UTF_16BE instanceof Charset);50check(StandardCharsets.UTF_16LE instanceof Charset);51check(StandardCharsets.UTF_16 instanceof Charset);5253check("US-ASCII".equals(StandardCharsets.US_ASCII.name()));54check("ISO-8859-1".equals(StandardCharsets.ISO_8859_1.name()));55check("UTF-8".equals(StandardCharsets.UTF_8.name()));56check("UTF-16BE".equals(StandardCharsets.UTF_16BE.name()));57check("UTF-16LE".equals(StandardCharsets.UTF_16LE.name()));58check("UTF-16".equals(StandardCharsets.UTF_16.name()));5960Set<String> charsets = new HashSet<>();61Field standardCharsetFields[] = StandardCharsets.class.getFields();6263for(Field charsetField : standardCharsetFields) {64check(StandardCharsets.class == charsetField.getDeclaringClass());65check(Modifier.isFinal(charsetField.getModifiers()));66check(Modifier.isStatic(charsetField.getModifiers()));67check(Modifier.isPublic(charsetField.getModifiers()));68Object value;69try {70value = charsetField.get(null);71} catch(IllegalAccessException failure) {72unexpected(failure);73continue;74}75check(value instanceof Charset);76charsets.add(((Charset)value).name());77}7879check(charsets.containsAll(Arrays.asList(standardCharsets)));80charsets.removeAll(Arrays.asList(standardCharsets));81check(charsets.isEmpty());82}8384//--------------------- Infrastructure ---------------------------85static volatile int passed = 0, failed = 0;86static void pass() { passed++; }87static void fail() { failed++; Thread.dumpStack(); }88static void fail(String msg) { System.out.println(msg); fail(); }89static void unexpected(Throwable t) { failed++; t.printStackTrace(); }90static void check(boolean cond) { if (cond) pass(); else fail(); }91static void equal(Object x, Object y) {92if (x == null ? y == null : x.equals(y)) pass();93else {System.out.println(x + " not equal to " + y); fail();}}94static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}95public static void main(String[] args) throws Throwable {96try { realMain(args); } catch (Throwable t) { unexpected(t); }9798System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);99if (failed > 0) throw new Exception("Some tests failed");100}101static byte[] serializedForm(Object obj) {102try {103ByteArrayOutputStream baos = new ByteArrayOutputStream();104new ObjectOutputStream(baos).writeObject(obj);105return baos.toByteArray();106} catch (IOException e) { throw new Error(e); }}107static Object readObject(byte[] bytes)108throws IOException, ClassNotFoundException {109InputStream is = new ByteArrayInputStream(bytes);110return new ObjectInputStream(is).readObject();}111@SuppressWarnings("unchecked")112static <T> T serialClone(T obj) {113try { return (T) readObject(serializedForm(obj)); }114catch (Exception e) { throw new Error(e); }}115116}117118119