Path: blob/master/test/jdk/java/nio/charset/Charset/NIOCharsetAvailabilityTest.java
41155 views
/*1* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4777124 6920545 6911753 807392426* @summary Verify that all Charset subclasses are available through the API27* @modules jdk.charsets28*/2930import java.net.URI;31import java.nio.charset.Charset;32import java.nio.file.FileSystem;33import java.nio.file.FileSystems;34import java.nio.file.Files;35import java.nio.file.Path;36import java.util.HashSet;37import java.util.Set;38import java.util.stream.Collectors;39import java.util.stream.Stream;4041public class NIOCharsetAvailabilityTest {4243public static void main(String[] args) throws Exception {4445// build the set of all Charset subclasses in the46// two known charset implementation packages47FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));48Set<Class> charsets =49Stream.concat(Files.walk(fs.getPath("/modules/java.base/sun/nio/cs/")),50Files.walk(fs.getPath("/modules/jdk.charsets/sun/nio/cs/ext/")))51.map( p -> p.subpath(2, p.getNameCount()).toString())52.filter( s -> s.indexOf("$") == -1 && s.endsWith(".class"))53.map( s -> {54try {55return Class.forName(s.substring(0, s.length() - 6)56.replace('/', '.'));57} catch (Exception x) {58throw new RuntimeException(x);59}60})61.filter( clz -> {62Class superclazz = clz.getSuperclass();63while (superclazz != null && !superclazz.equals(Object.class)) {64if (superclazz.equals(Charset.class)) {65return true;66} else {67superclazz = superclazz.getSuperclass();68}69}70return false;71})72.collect(Collectors.toCollection(HashSet::new));73// remove the charsets that the API says are available74Charset.availableCharsets()75.values()76.stream()77.forEach(cs -> {78if (!charsets.contains(cs.getClass())) {79System.out.println(" missing -> " + cs.getClass());80}81charsets.remove(cs.getClass());82});8384// remove the known pseudo-charsets that serve only to implement85// other charsets, but shouldn't be known to the public86charsets.remove(Class.forName("sun.nio.cs.Unicode"));87charsets.remove(Class.forName("sun.nio.cs.ext.ISO2022"));88charsets.remove(Class.forName("sun.nio.cs.ext.ISO2022_CN_GB"));89charsets.remove(Class.forName("sun.nio.cs.ext.ISO2022_CN_CNS"));90charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0208_MS932"));91charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0212_MS5022X"));92charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0208_MS5022X"));93try {94charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0208_Solaris"));95charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0212_Solaris"));96} catch (ClassNotFoundException x) {97// these two might be moved into stdcs98charsets.remove(Class.forName("sun.nio.cs.JIS_X_0208_Solaris"));99charsets.remove(Class.forName("sun.nio.cs.JIS_X_0212_Solaris"));100}101// report the charsets that are implemented but not available102if (charsets.size() > 0) {103charsets.stream()104.forEach( clz ->105System.out.println("Unused Charset subclass: " + clz));106throw new RuntimeException();107}108}109}110111112