Path: blob/master/test/jdk/java/nio/charset/StandardCharsets/Standard.java
41153 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()));5960check(Charset.forName("US-ASCII") == StandardCharsets.US_ASCII);61check(Charset.forName("ISO-8859-1") == StandardCharsets.ISO_8859_1);62check(Charset.forName("UTF-8") == StandardCharsets.UTF_8);63check(Charset.forName("UTF-16BE") == StandardCharsets.UTF_16BE);64check(Charset.forName("UTF-16LE") == StandardCharsets.UTF_16LE);65check(Charset.forName("UTF-16") == StandardCharsets.UTF_16);6667Set<String> charsets = new HashSet<>();68Field standardCharsetFields[] = StandardCharsets.class.getFields();6970for(Field charsetField : standardCharsetFields) {71check(StandardCharsets.class == charsetField.getDeclaringClass());72check(Modifier.isFinal(charsetField.getModifiers()));73check(Modifier.isStatic(charsetField.getModifiers()));74check(Modifier.isPublic(charsetField.getModifiers()));75Object value;76try {77value = charsetField.get(null);78} catch(IllegalAccessException failure) {79unexpected(failure);80continue;81}82check(value instanceof Charset);83charsets.add(((Charset)value).name());84}8586check(charsets.containsAll(Arrays.asList(standardCharsets)));87charsets.removeAll(Arrays.asList(standardCharsets));88check(charsets.isEmpty());89}9091//--------------------- Infrastructure ---------------------------92static volatile int passed = 0, failed = 0;93static void pass() { passed++; }94static void fail() { failed++; Thread.dumpStack(); }95static void fail(String msg) { System.out.println(msg); fail(); }96static void unexpected(Throwable t) { failed++; t.printStackTrace(); }97static void check(boolean cond) { if (cond) pass(); else fail(); }98static void equal(Object x, Object y) {99if (x == null ? y == null : x.equals(y)) pass();100else {System.out.println(x + " not equal to " + y); fail();}}101static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}102public static void main(String[] args) throws Throwable {103try { realMain(args); } catch (Throwable t) { unexpected(t); }104105System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);106if (failed > 0) throw new Exception("Some tests failed");107}108static byte[] serializedForm(Object obj) {109try {110ByteArrayOutputStream baos = new ByteArrayOutputStream();111new ObjectOutputStream(baos).writeObject(obj);112return baos.toByteArray();113} catch (IOException e) { throw new Error(e); }}114static Object readObject(byte[] bytes)115throws IOException, ClassNotFoundException {116InputStream is = new ByteArrayInputStream(bytes);117return new ObjectInputStream(is).readObject();}118@SuppressWarnings("unchecked")119static <T> T serialClone(T obj) {120try { return (T) readObject(serializedForm(obj)); }121catch (Exception e) { throw new Error(e); }}122123}124125126