Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/charset/Charset/NIOCharsetAvailabilityTest.java
38821 views
/*1* Copyright (c) 2010, 2012, 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 691175326* @summary Verify that all Charset subclasses are available through the API27*/2829import java.io.File;30import java.io.FileInputStream;31import java.io.FileNotFoundException;32import java.io.IOException;33import java.net.URI;34import java.net.URISyntaxException;35import java.net.URL;36import java.net.URLClassLoader;37import java.nio.charset.Charset;38import java.util.Collection;39import java.util.HashSet;40import java.util.Iterator;41import java.util.Set;42import java.util.Vector;43import java.util.zip.ZipEntry;44import java.util.zip.ZipInputStream;45import sun.misc.Launcher;464748public class NIOCharsetAvailabilityTest {4950public static void main(String[] args) throws Exception {51// build the set of all Charset subclasses in the52// two known charset implementation packages53Set charsets = new HashSet();54addCharsets(charsets, "sun.nio.cs");55addCharsets(charsets, "sun.nio.cs.ext");5657// remove the charsets that the API says are available58Collection availableCharsets = Charset.availableCharsets().values();59Iterator iter = availableCharsets.iterator();60while (iter.hasNext()) {61charsets.remove(((Charset) iter.next()).getClass());62}6364// remove the known pseudo-charsets that serve only to implement65// other charsets, but shouldn't be known to the public66charsets.remove(Class.forName("sun.nio.cs.Unicode"));67charsets.remove(Class.forName("sun.nio.cs.ext.ISO2022"));68charsets.remove(Class.forName("sun.nio.cs.ext.ISO2022_CN_GB"));69charsets.remove(Class.forName("sun.nio.cs.ext.ISO2022_CN_CNS"));70charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0208_Solaris"));71charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0208_MS932"));72charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0212_MS5022X"));73charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0212_Solaris"));74charsets.remove(Class.forName("sun.nio.cs.ext.JIS_X_0208_MS5022X"));7576// report the charsets that are implemented but not available77iter = charsets.iterator();78while (iter.hasNext()) {79System.out.println("Unused Charset subclass: " + ((Class) iter.next()).getName());80}81if (charsets.size() > 0) {82throw new RuntimeException();83}84}8586private static Vector classPathSegments = new Vector();8788private static void addCharsets(Set charsets, final String packageName)89throws Exception {9091String classPath =92(String) java.security.AccessController.doPrivileged(93new sun.security.action.GetPropertyAction("sun.boot.class.path"));94String s =95(String) java.security.AccessController.doPrivileged(96new sun.security.action.GetPropertyAction("java.class.path"));9798// Search combined system and application class path99if (s != null && s.length() != 0) {100classPath += File.pathSeparator + s;101}102while (classPath != null && classPath.length() != 0) {103int i = classPath.lastIndexOf(java.io.File.pathSeparatorChar);104String dir = classPath.substring(i + 1);105if (i == -1) {106classPath = null;107} else {108classPath = classPath.substring(0, i);109}110classPathSegments.insertElementAt(dir, 0);111}112113// add extensions from the extension class loader114ClassLoader appLoader = Launcher.getLauncher().getClassLoader();115URLClassLoader extLoader = (URLClassLoader) appLoader.getParent();116URL[] urls = extLoader.getURLs();117for (int i = 0; i < urls.length; i++) {118try {119URI uri = new URI(urls[i].toString());120classPathSegments.insertElementAt(uri.getPath(), 0);121} catch (URISyntaxException e) {122}123}124125String[] classList = (String[])126java.security.AccessController.doPrivileged(127new java.security.PrivilegedAction() {128public Object run() {129return getClassList(packageName, "");130}131});132133for (int i = 0; i < classList.length; i++) {134try {135Class clazz = Class.forName(packageName + "." + classList[i]);136Class superclazz = clazz.getSuperclass();137while (superclazz != null && !superclazz.equals(Object.class)) {138if (superclazz.equals(Charset.class)) {139charsets.add(clazz);140break;141} else {142superclazz = superclazz.getSuperclass();143}144}145} catch (ClassNotFoundException e) {146}147}148}149150private static final char ZIPSEPARATOR = '/';151152/**153* Walk through CLASSPATH and find class list from a package.154* The class names start with prefix string155* @param package name, class name prefix156* @return class list in an array of String157*/158private static String[] getClassList(String pkgName, String prefix) {159Vector listBuffer = new Vector();160String packagePath = pkgName.replace('.', File.separatorChar)161+ File.separatorChar;162String zipPackagePath = pkgName.replace('.', ZIPSEPARATOR)163+ ZIPSEPARATOR;164for (int i = 0; i < classPathSegments.size(); i++){165String onePath = (String) classPathSegments.elementAt(i);166File f = new File(onePath);167if (!f.exists())168continue;169if (f.isFile())170scanFile(f, zipPackagePath, listBuffer, prefix);171else if (f.isDirectory()) {172String fullPath;173if (onePath.endsWith(File.separator))174fullPath = onePath + packagePath;175else176fullPath = onePath + File.separatorChar + packagePath;177File dir = new File(fullPath);178if (dir.exists() && dir.isDirectory())179scanDir(dir, listBuffer, prefix);180}181}182String[] classNames = new String[listBuffer.size()];183listBuffer.copyInto(classNames);184return classNames;185}186187private static void addClass (String className, Vector listBuffer, String prefix) {188if (className != null && className.startsWith(prefix)189&& !listBuffer.contains(className))190listBuffer.addElement(className);191}192193private static String midString(String str, String pre, String suf) {194String midStr;195if (str.startsWith(pre) && str.endsWith(suf))196midStr = str.substring(pre.length(), str.length() - suf.length());197else198midStr = null;199return midStr;200}201202private static void scanDir(File dir, Vector listBuffer, String prefix) {203String[] fileList = dir.list();204for (int i = 0; i < fileList.length; i++) {205addClass(midString(fileList[i], "", ".class"), listBuffer, prefix);206}207}208209private static void scanFile(File f, String packagePath, Vector listBuffer,210String prefix) {211try {212ZipInputStream zipFile = new ZipInputStream(new FileInputStream(f));213ZipEntry entry;214while ((entry = zipFile.getNextEntry()) != null) {215String eName = entry.getName();216if (eName.startsWith(packagePath)) {217if (eName.endsWith(".class")) {218addClass(midString(eName, packagePath, ".class"),219listBuffer, prefix);220}221}222}223} catch (FileNotFoundException e) {224System.out.println("file not found:" + e);225} catch (IOException e) {226System.out.println("file IO Exception:" + e);227} catch (Exception e) {228System.out.println("Exception:" + e);229}230}231}232233234