Skip to content

Commit fff35ed

Browse files
author
Amos Shi
committed
8310683: Refactor StandardCharset/standard.java to use JUnit
Backport-of: 4e84d4dc514192e4cdf8e2c7c474847832987ab9
1 parent 6ccff7f commit fff35ed

File tree

1 file changed

+100
-79
lines changed

1 file changed

+100
-79
lines changed
Lines changed: 100 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,102 +23,123 @@
2323

2424
/*
2525
* @test
26-
* @bug 4884238
27-
* @summary Test standard charset name constants.
26+
* @bug 4884238 8310047
27+
* @summary Test standard charset name constants and class qualities.
2828
* @author Mike Duigou
29-
* @run main Standard
29+
* @run junit Standard
3030
*/
3131

3232
import java.lang.reflect.Field;
3333
import java.lang.reflect.Modifier;
34-
import java.io.*;
35-
import java.nio.charset.*;
34+
import java.nio.charset.Charset;
35+
import java.nio.charset.StandardCharsets;
3636
import java.util.Arrays;
37-
import java.util.HashSet;
38-
import java.util.Set;
37+
import java.util.List;
38+
import java.util.stream.Stream;
39+
40+
import org.junit.jupiter.api.Test;
41+
import org.junit.jupiter.params.ParameterizedTest;
42+
import org.junit.jupiter.params.provider.Arguments;
43+
import org.junit.jupiter.params.provider.MethodSource;
44+
45+
import static org.junit.jupiter.api.Assertions.assertEquals;
46+
import static org.junit.jupiter.api.Assertions.assertTrue;
3947

4048
public class Standard {
4149

42-
private final static String standardCharsets[] = {
43-
"US-ASCII", "ISO-8859-1", "UTF-8",
44-
"UTF-16BE", "UTF-16LE", "UTF-16" };
50+
// These are the charsets StandardCharsets.java is expected to contain.
51+
private static final String[] expectedCharsets = {
52+
"US-ASCII", "ISO-8859-1", "UTF-8",
53+
"UTF-16BE", "UTF-16LE", "UTF-16"
54+
};
4555

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);
56+
private static final Field[] standardCharsetFields =
57+
StandardCharsets.class.getFields();
5358

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()));
59+
/**
60+
* Validates that the Charset constants from the data provider
61+
* are of type Charset.
62+
*/
63+
@ParameterizedTest
64+
@MethodSource("charsetProvider")
65+
public void typeTest(Charset charset) {
66+
// Doubly checked, as it is validated when passed as a param
67+
assertTrue(charset instanceof Charset);
68+
}
69+
70+
/**
71+
* Validates that calling .name() on a Charset constant is equal
72+
* to the matching String value from the data provider.
73+
*/
74+
@ParameterizedTest
75+
@MethodSource("charsetProvider")
76+
public void nameMethodTest(Charset charset, String charString) {
77+
assertEquals(charset.name(), charString);
78+
}
6079

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);
80+
/**
81+
* Validates that calling Charset.forName() on a String is equal
82+
* to the matching Charset constant from the data provider.
83+
*/
84+
@ParameterizedTest
85+
@MethodSource("charsetProvider")
86+
public void forNameMethodTest(Charset charset, String charString) {
87+
assertEquals(Charset.forName(charString), charset);
88+
}
6789

68-
Set<String> charsets = new HashSet<>();
69-
Field standardCharsetFields[] = StandardCharsets.class.getFields();
90+
/**
91+
* Validates the qualities of a StandardCharsets field are as expected:
92+
* The field is final, static, public, and one can access
93+
* the underlying value of the field.
94+
*/
95+
@ParameterizedTest
96+
@MethodSource("charsetFields")
97+
public void charsetModifiersTest(Field charsetField) throws IllegalAccessException {
98+
// Check modifiers
99+
assertEquals(StandardCharsets.class, charsetField.getDeclaringClass());
100+
assertTrue(Modifier.isFinal(charsetField.getModifiers()));
101+
assertTrue(Modifier.isStatic(charsetField.getModifiers()));
102+
assertTrue(Modifier.isPublic(charsetField.getModifiers()));
103+
// Check that the value can be accessed, and it is a Charset
104+
Object valueOfField = charsetField.get(null);
105+
assertTrue(valueOfField instanceof Charset);
106+
}
70107

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;
108+
/**
109+
* Validates that the Charsets contained in StandardCharsets are equal
110+
* to the expected Charsets list defined in the test. This test should fail if
111+
* either the actual or expected (standard) Charsets are modified, and
112+
* the others are not.
113+
*/
114+
@Test
115+
public void correctCharsetsTest() {
116+
// Grab the value from each Standard Charset field
117+
List<String> actualCharsets = charsetFields().map(field -> {
77118
try {
78-
value = charsetField.get(null);
79-
} catch(IllegalAccessException failure) {
80-
unexpected(failure);
81-
continue;
119+
return ((Charset) field.get(null)).name();
120+
} catch (IllegalAccessException e) {
121+
throw new RuntimeException("Can not test correctCharsetsTest() due to %s", e);
82122
}
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());
123+
}).toList();
124+
assertEquals(actualCharsets, Arrays.asList(expectedCharsets));
90125
}
91126

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");
127+
/**
128+
* Provides the constant Charset and associated String value of
129+
* the standard charsets.
130+
*/
131+
private static Stream<Arguments> charsetProvider() {
132+
return Stream.of(
133+
Arguments.of(StandardCharsets.US_ASCII, "US-ASCII"),
134+
Arguments.of(StandardCharsets.ISO_8859_1, "ISO-8859-1"),
135+
Arguments.of(StandardCharsets.UTF_8, "UTF-8"),
136+
Arguments.of(StandardCharsets.UTF_16BE, "UTF-16BE"),
137+
Arguments.of(StandardCharsets.UTF_16LE, "UTF-16LE"),
138+
Arguments.of(StandardCharsets.UTF_16, "UTF-16")
139+
);
108140
}
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); }}
123141

142+
private static Stream<Field> charsetFields() {
143+
return Arrays.stream(standardCharsetFields);
144+
}
124145
}

0 commit comments

Comments
 (0)