Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/misc/URLClassPath/ClassnameCharTest.java
38838 views
/*1* Copyright (c) 2012, 2016, 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/* @test24* @bug 4957669 501787125* @compile -XDignore.symbol.file=true ClassnameCharTest.java26* @run main ClassnameCharTest27* @summary cannot load class names containing some JSR 202 characters;28* plugin does not escape unicode character in http request29*/3031import java.io.*;32import java.net.*;33import java.util.jar.*;34import com.sun.net.httpserver.*;35import sun.applet.AppletClassLoader;3637public class ClassnameCharTest {38static String FNPrefix = System.getProperty("test.src", ".") + File.separator;39static File classesJar = new File(FNPrefix + "testclasses.jar");40static HttpServer server;4142public static void realMain(String[] args) throws Exception {43server = HttpServer.create(new InetSocketAddress(0), 0);44server.createContext("/", new HttpHandler() {45@Override46public void handle(HttpExchange exchange) {47try {48String filename = exchange.getRequestURI().getPath();49System.out.println("getRequestURI = " + exchange.getRequestURI());50System.out.println("filename = " + filename);51try (FileInputStream fis = new FileInputStream(classesJar);52JarInputStream jis = new JarInputStream(fis)) {53JarEntry entry;54while ((entry = jis.getNextJarEntry()) != null) {55if (filename.endsWith(entry.getName())) {56ByteArrayOutputStream baos = new ByteArrayOutputStream();57byte[] buf = new byte[8092];58int count = 0;59while ((count = jis.read(buf)) != -1)60baos.write(buf, 0, count);61exchange.sendResponseHeaders(200, baos.size());62try (OutputStream os = exchange.getResponseBody()) {63baos.writeTo(os);64}65return;66}67}68fail("Failed to find " + filename);69}70} catch (IOException e) {71unexpected(e);72}73}74});75server.start();76try {77URL base = new URL("http://localhost:" + server.getAddress().getPort());78System.out.println ("Server: listening on " + base);79MyAppletClassLoader acl = new MyAppletClassLoader(base);80Class<?> class1 = acl.findClass("fo o");81System.out.println("class1 = " + class1);82pass();83// can't test the following class unless platform in unicode locale84// Class class2 = acl.findClass("\u624b\u518c");85// System.out.println("class2 = "+class2);86} finally {87server.stop(0);88}89}9091static class MyAppletClassLoader extends AppletClassLoader {92MyAppletClassLoader(URL base) {93super(base);94}9596@Override97public Class<?> findClass(String name) throws ClassNotFoundException {98return super.findClass(name);99}100}101102//--------------------- Infrastructure ---------------------------103static volatile int passed = 0, failed = 0;104105static boolean pass() {106passed++;107return true;108}109110static boolean fail() {111failed++;112if (server != null) {113server.stop(0);114}115Thread.dumpStack();116return false;117}118119static boolean fail(String msg) {120System.out.println(msg);121return fail();122}123124static void unexpected(Throwable t) {125failed++;126if (server != null) {127server.stop(0);128}129t.printStackTrace();130}131132static boolean check(boolean cond) {133if (cond) {134pass();135} else {136fail();137}138return cond;139}140141static boolean equal(Object x, Object y) {142if (x == null ? y == null : x.equals(y)) {143return pass();144} else {145return fail(x + " not equal to " + y);146}147}148149public static void main(String[] args) throws Throwable {150try {151realMain(args);152} catch (Throwable t) {153unexpected(t);154}155System.out.println("\nPassed = " + passed + " failed = " + failed);156if (failed > 0) {157throw new AssertionError("Some tests failed");158}159}160}161162163