1/*
2 * Copyright (c) 2010, 2015, 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 4777124 6920545 6911753 8073924
27 * @summary Verify that all Charset subclasses are available through the API
28 * @modules jdk.charsets
29 */
30
31import java.net.URI;
32import java.nio.charset.Charset;
33import java.nio.file.FileSystem;
34import java.nio.file.FileSystems;
35import java.nio.file.Files;
36import java.nio.file.Path;
37import java.util.HashSet;
38import java.util.Set;
39import java.util.stream.Collectors;
40import java.util.stream.Stream;
41
42public class NIOCharsetAvailabilityTest {
43
44    public static void main(String[] args) throws Exception {
45
46        // build the set of all Charset subclasses in the
47        // two known charset implementation packages
48        FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
49        Set<Class> charsets =
50            Stream.concat(Files.walk(fs.getPath("/modules/java.base/sun/nio/cs/")),
51                          Files.walk(fs.getPath("/modules/jdk.charsets/sun/nio/cs/ext/")))
52                 .map( p -> p.subpath(2, p.getNameCount()).toString())
53                 .filter( s ->  s.indexOf("$") == -1 && s.endsWith(".class"))
54                 .map( s -> {
55                     try {
56                         return Class.forName(s.substring(0, s.length() - 6)
57                                               .replace('/', '.'));
58                     } catch (Exception x) {
59                         throw new RuntimeException(x);
60                     }
61                  })
62                 .filter( clz -> {
63                     Class superclazz = clz.getSuperclass();
64                     while (superclazz != null && !superclazz.equals(Object.class)) {
65                         if (superclazz.equals(Charset.class)) {
66                             return true;
67                         } else {
68                             superclazz = superclazz.getSuperclass();
69                         }
70                     }
71                     return false;
72                  })
73                 .collect(Collectors.toCollection(HashSet::new));
74        // remove the charsets that the API says are available
75        Charset.availableCharsets()
76               .values()
77               .stream()
78               .forEach(cs -> {
79                   if (!charsets.contains(cs.getClass())) {
80                       System.out.println(" missing -> " + cs.getClass());
81                   }
82                   charsets.remove(cs.getClass());
83                });
84
85        // remove the known pseudo-charsets that serve only to implement
86        // other charsets, but shouldn't be known to the public
87        charsets.remove(Class.forName("sun.nio.cs.Unicode"));
88        charsets.remove(Class.forName("sun.nio.cs.ext.ISO2022"));
89        charsets.remove(Class.forName("sun.nio.cs.ext.ISO2022_CN_GB"));
90        charsets.remove(Class.forName("sun.nio.cs.ext.ISO2022_CN_CNS"));
91        charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0208_MS932"));
92        charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0212_MS5022X"));
93        charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0208_MS5022X"));
94        try {
95            charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0208_Solaris"));
96            charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0212_Solaris"));
97        } catch (ClassNotFoundException x) {
98            // these two might be moved into stdcs
99            charsets.remove(Class.forName("sun.nio.cs.JIS_X_0208_Solaris"));
100            charsets.remove(Class.forName("sun.nio.cs.JIS_X_0212_Solaris"));
101        }
102        // report the charsets that are implemented but not available
103        if (charsets.size() > 0) {
104            charsets.stream()
105                    .forEach( clz ->
106                        System.out.println("Unused Charset subclass: " + clz));
107            throw new RuntimeException();
108        }
109    }
110}
111