CloseTest.java 5.3 KB
Newer Older
1
/*
2
 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
22 23 24 25 26 27
 */

/**
 * @test
 * @bug 4167874
 * @library ../../../../com/sun/net/httpserver
28 29
 * @library /lib/testlibrary
 * @build FileServerHandler jdk.testlibrary.FileUtils
30 31 32 33 34 35 36 37 38 39
 * @run shell build.sh
 * @run main/othervm CloseTest
 * @summary URL-downloaded jar files can consume all available file descriptors
 */

import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import com.sun.net.httpserver.*;

40
public class CloseTest extends Common {
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

//
// needs two jar files test1.jar and test2.jar with following structure
//
// com/foo/TestClass
// com/foo/TestClass1
// com/foo/Resource1
// com/foo/Resource2
//
// and a directory hierarchy with the same structure/contents

    public static void main (String args[]) throws Exception {

        String workdir = System.getProperty("test.classes");
        if (workdir == null) {
            workdir = args[0];
        }
        if (!workdir.endsWith("/")) {
            workdir = workdir+"/";
        }

        startHttpServer (workdir+"serverRoot/");

        String testjar = workdir + "test.jar";
        copyFile (workdir+"test1.jar", testjar);
        test (testjar, 1);

        // repeat test with different implementation
        // of test.jar (whose TestClass.getValue() returns 2

        copyFile (workdir+"test2.jar", testjar);
        test (testjar, 2);

        // repeat test using a directory of files
        String testdir=workdir+"testdir/";
        rm_minus_rf (new File(testdir));
        copyDir (workdir+"test1/", testdir);
        test (testdir, 1);

        testdir=workdir+"testdir/";
        rm_minus_rf (new File(testdir));
        copyDir (workdir+"test2/", testdir);
        test (testdir, 2);
        getHttpServer().stop (3);
    }

    // create a loader on jarfile (or directory), plus a http loader
    // load a class , then look for a resource
    // also load a class from http loader
    // then close the loader
    // check further new classes/resources cannot be loaded
    // check jar (or dir) can be deleted
    // check existing classes can be loaded
    // check boot classes can be loaded

    static void test (String name, int expectedValue) throws Exception {
        URL url = new URL ("file", null, name);
        URL url2 = getServerURL();
        System.out.println ("Doing tests with URL: " + url + " and " + url2);
        URL[] urls = new URL[2];
        urls[0] =  url;
        urls[1] =  url2;
        URLClassLoader loader = new URLClassLoader (urls);
        Class testclass = loadClass ("com.foo.TestClass", loader, true);
        Class class2 = loadClass ("Test", loader, true); // from http
        class2.newInstance();
        Object test = testclass.newInstance();
        Method method = testclass.getDeclaredMethods()[0]; // int getValue();
        int res = (Integer) method.invoke (test);

        if (res != expectedValue) {
            throw new RuntimeException ("wrong value from getValue() ["+res+
                        "/"+expectedValue+"]");
        }

        // should find /resource1
        URL u1 = loader.findResource ("com/foo/Resource1");
        if (u1 == null) {
            throw new RuntimeException ("can't find com/foo/Resource1 in test1.jar");
        }
        loader.close ();

        // should NOT find /resource2 even though it is in jar
        URL u2 = loader.findResource ("com/foo/Resource2");
        if (u2 != null) {
            throw new RuntimeException ("com/foo/Resource2 unexpected in test1.jar");
        }

        // load tests
        loadClass ("com.foo.TestClass1", loader, false);
        loadClass ("com.foo.TestClass", loader, true);
W
weijun 已提交
132
        loadClass ("java.sql.Array", loader, true);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

        // now check we can delete the path
        rm_minus_rf (new File(name));
        System.out.println (" ... OK");
    }

    static HttpServer httpServer;

    static HttpServer getHttpServer() {
        return httpServer;
    }

    static URL getServerURL () throws Exception {
        int port = httpServer.getAddress().getPort();
        String s = "http://127.0.0.1:"+port+"/";
        return new URL(s);
    }

    static void startHttpServer (String docroot) throws Exception {
        httpServer = HttpServer.create (new InetSocketAddress(0), 10);
        HttpContext ctx = httpServer.createContext (
                "/", new FileServerHandler(docroot)
        );
        httpServer.start();
    }
}