test/jdk/java/net/URLClassLoader/ClassLoad.java
changeset 57686 70f5cbb711a9
parent 47216 71c04702a3d5
equal deleted inserted replaced
57685:e4cc5231ce2d 57686:70f5cbb711a9
     1 /*
     1 /*
     2  * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @bug 4151665
    26  * @bug 4151665
    27  * @modules jdk.httpserver
    27  * @modules jdk.httpserver
       
    28  * @library /test/lib
    28  * @summary Test for FileNotFoundException when loading bogus class
    29  * @summary Test for FileNotFoundException when loading bogus class
    29  */
    30  */
    30 
    31 
    31 import java.io.InputStream;
    32 import java.io.InputStream;
    32 import java.io.IOException;
    33 import java.io.IOException;
       
    34 import java.net.InetAddress;
    33 import java.net.InetSocketAddress;
    35 import java.net.InetSocketAddress;
    34 import java.net.URL;
    36 import java.net.URL;
    35 import java.net.URLClassLoader;
    37 import java.net.URLClassLoader;
    36 import com.sun.net.httpserver.HttpExchange;
    38 import com.sun.net.httpserver.HttpExchange;
    37 import com.sun.net.httpserver.HttpHandler;
    39 import com.sun.net.httpserver.HttpHandler;
    38 import com.sun.net.httpserver.HttpServer;
    40 import com.sun.net.httpserver.HttpServer;
       
    41 import jdk.test.lib.net.URIBuilder;
    39 
    42 
    40 public class ClassLoad {
    43 public class ClassLoad {
    41      public static void main(String[] args) throws Exception {
    44      public static void main(String[] args) throws Exception {
    42          boolean error = true;
    45          boolean error = true;
    43 
    46 
    44          // Start a dummy server to return 404
    47          // Start a dummy server to return 404
    45          HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    48          HttpServer server = HttpServer.create();
       
    49          server.bind(new InetSocketAddress(
       
    50                  InetAddress.getLoopbackAddress(), 0), 0);
    46          HttpHandler handler = new HttpHandler() {
    51          HttpHandler handler = new HttpHandler() {
    47              public void handle(HttpExchange t) throws IOException {
    52              public void handle(HttpExchange t) throws IOException {
    48                  InputStream is = t.getRequestBody();
    53                  InputStream is = t.getRequestBody();
    49                  while (is.read() != -1);
    54                  while (is.read() != -1);
    50                  t.sendResponseHeaders (404, -1);
    55                  t.sendResponseHeaders (404, -1);
    54          server.createContext("/", handler);
    59          server.createContext("/", handler);
    55          server.start();
    60          server.start();
    56 
    61 
    57          // Client request
    62          // Client request
    58          try {
    63          try {
    59              URL url = new URL("http://localhost:" + server.getAddress().getPort());
    64              URL url = URIBuilder.newBuilder()
       
    65                      .scheme("http")
       
    66                      .loopback()
       
    67                      .port(server.getAddress().getPort())
       
    68                      .toURL();
    60              String name = args.length >= 2 ? args[1] : "foo.bar.Baz";
    69              String name = args.length >= 2 ? args[1] : "foo.bar.Baz";
    61              ClassLoader loader = new URLClassLoader(new URL[] { url });
    70              ClassLoader loader = new URLClassLoader(new URL[] { url });
    62              System.out.println(url);
    71              System.out.println(url);
    63              Class c = loader.loadClass(name);
    72              Class c = loader.loadClass(name);
    64              System.out.println("Loaded class \"" + c.getName() + "\".");
    73              System.out.println("Loaded class \"" + c.getName() + "\".");