Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/charset/StandardCharsets/Standard.java
38828 views
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
Set<String> charsets = new HashSet<>();
62
Field standardCharsetFields[] = StandardCharsets.class.getFields();
63
64
for(Field charsetField : standardCharsetFields) {
65
check(StandardCharsets.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
static byte[] serializedForm(Object obj) {
103
try {
104
ByteArrayOutputStream baos = new ByteArrayOutputStream();
105
new ObjectOutputStream(baos).writeObject(obj);
106
return baos.toByteArray();
107
} catch (IOException e) { throw new Error(e); }}
108
static Object readObject(byte[] bytes)
109
throws IOException, ClassNotFoundException {
110
InputStream is = new ByteArrayInputStream(bytes);
111
return new ObjectInputStream(is).readObject();}
112
@SuppressWarnings("unchecked")
113
static <T> T serialClone(T obj) {
114
try { return (T) readObject(serializedForm(obj)); }
115
catch (Exception e) { throw new Error(e); }}
116
117
}
118
119