|
1 | 1 | /*
|
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
23 | 23 |
|
24 | 24 | /*
|
25 | 25 | * @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. |
28 | 28 | * @author Mike Duigou
|
29 |
| - * @run main Standard |
| 29 | + * @run junit Standard |
30 | 30 | */
|
31 | 31 |
|
32 | 32 | import java.lang.reflect.Field;
|
33 | 33 | 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; |
36 | 36 | 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; |
39 | 47 |
|
40 | 48 | public class Standard {
|
41 | 49 |
|
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 | + }; |
45 | 55 |
|
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(); |
53 | 58 |
|
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 | + } |
60 | 79 |
|
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 | + } |
67 | 89 |
|
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 | + } |
70 | 107 |
|
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 -> { |
77 | 118 | 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); |
82 | 122 | }
|
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)); |
90 | 125 | }
|
91 | 126 |
|
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 | + ); |
108 | 140 | }
|
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 | 141 |
|
| 142 | + private static Stream<Field> charsetFields() { |
| 143 | + return Arrays.stream(standardCharsetFields); |
| 144 | + } |
124 | 145 | }
|
0 commit comments