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.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(StandardCharset.US_ASCII instanceof Charset); |
|
48 check(StandardCharset.ISO_8859_1 instanceof Charset); |
|
49 check(StandardCharset.UTF_8 instanceof Charset); |
|
50 check(StandardCharset.UTF_16BE instanceof Charset); |
|
51 check(StandardCharset.UTF_16LE instanceof Charset); |
|
52 check(StandardCharset.UTF_16 instanceof Charset); |
|
53 |
|
54 check("US-ASCII".equals(StandardCharset.US_ASCII.name())); |
|
55 check("ISO-8859-1".equals(StandardCharset.ISO_8859_1.name())); |
|
56 check("UTF-8".equals(StandardCharset.UTF_8.name())); |
|
57 check("UTF-16BE".equals(StandardCharset.UTF_16BE.name())); |
|
58 check("UTF-16LE".equals(StandardCharset.UTF_16LE.name())); |
|
59 check("UTF-16".equals(StandardCharset.UTF_16.name())); |
|
60 |
|
61 Set<String> charsets = new HashSet<>(); |
|
62 Field standardCharsetFields[] = StandardCharset.class.getFields(); |
|
63 |
|
64 for(Field charsetField : standardCharsetFields) { |
|
65 check(StandardCharset.class == charsetField.getDeclaringClass()); |
|
66 check(Modifier.isFinal(charsetField.getModifiers())); |
|
67 check(Modifier.isStatic(charsetField.getModifiers())); |
|
68 check(Modifier.isPublic(charsetField.getModifiers())); |
|
69 Object value; |
|
70 try { |
|
71 value = charsetField.get(null); |
|
72 } catch(IllegalAccessException failure) { |
|
73 unexpected(failure); |
|
74 continue; |
|
75 } |
|
76 check(value instanceof Charset); |
|
77 charsets.add(((Charset)value).name()); |
|
78 } |
|
79 |
|
80 check(charsets.containsAll(Arrays.asList(standardCharsets))); |
|
81 charsets.removeAll(Arrays.asList(standardCharsets)); |
|
82 check(charsets.isEmpty()); |
|
83 } |
|
84 |
|
85 //--------------------- Infrastructure --------------------------- |
|
86 static volatile int passed = 0, failed = 0; |
|
87 static void pass() { passed++; } |
|
88 static void fail() { failed++; Thread.dumpStack(); } |
|
89 static void fail(String msg) { System.out.println(msg); fail(); } |
|
90 static void unexpected(Throwable t) { failed++; t.printStackTrace(); } |
|
91 static void check(boolean cond) { if (cond) pass(); else fail(); } |
|
92 static void equal(Object x, Object y) { |
|
93 if (x == null ? y == null : x.equals(y)) pass(); |
|
94 else {System.out.println(x + " not equal to " + y); fail();}} |
|
95 static void equal2(Object x, Object y) {equal(x, y); equal(y, x);} |
|
96 public static void main(String[] args) throws Throwable { |
|
97 try { realMain(args); } catch (Throwable t) { unexpected(t); } |
|
98 |
|
99 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
100 if (failed > 0) throw new Exception("Some tests failed"); |
|
101 } |
|
102 private static abstract class Fun {abstract void f() throws Throwable;} |
|
103 private static void THROWS(Class<? extends Throwable> k, Fun... fs) { |
|
104 for (Fun f : fs) |
|
105 try { f.f(); fail("Expected " + k.getName() + " not thrown"); } |
|
106 catch (Throwable t) { |
|
107 if (k.isAssignableFrom(t.getClass())) pass(); |
|
108 else unexpected(t);}} |
|
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 } |
|