jdk/test/java/net/URLClassLoader/closetest/GetResourceAsStream.java
author dsamersoff
Thu, 17 Oct 2013 16:08:01 +0400
changeset 21069 728330d2593a
parent 7983 fb83b663cadc
child 21664 81740736e62f
permissions -rw-r--r--
8025812: tmtools/jmap/heap_config tests fail on Linux-ia32 because it Cant attach to the core file Summary: Coredump store memsz elf field rounded up to page Reviewed-by: dholmes, sla
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7983
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     1
/*
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     4
 *
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     8
 *
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    13
 * accompanied this code).
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    14
 *
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    18
 *
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    21
 * questions.
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    22
 */
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    23
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    24
/**
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    25
 * @test
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    26
 * @bug 6899919
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    27
 * @run shell build2.sh
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    28
 * @run main/othervm GetResourceAsStream
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    29
 */
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    30
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    31
import java.io.*;
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    32
import java.net.*;
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    33
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    34
public class GetResourceAsStream extends Common {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    35
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    36
/*
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    37
 * We simply test various scenarios with class/resource files
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    38
 * and make sure the files can be deleted after closing
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    39
 * the loader. Therefore, the test will only really be verified
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    40
 * on Windows. It will still run correctly on other platforms
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    41
 */
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    42
    public static void main (String args[]) throws Exception {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    43
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    44
        String workdir = System.getProperty("test.classes");
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    45
        if (workdir == null) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    46
            workdir = args[0];
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    47
        }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    48
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    49
        /* the jar we copy for each test */
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    50
        File srcfile = new File (workdir, "foo.jar");
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    51
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    52
        /* the jar we use for the test */
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    53
        File testfile = new File (workdir, "test.jar");
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    54
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    55
        copyFile (srcfile, testfile);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    56
        test (testfile, false, false);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    57
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    58
        copyFile (srcfile, testfile);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    59
        test (testfile, true, false);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    60
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    61
        copyFile (srcfile, testfile);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    62
        test (testfile, true, true);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    63
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    64
        // repeat test using a directory of files
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    65
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    66
        File testdir= new File (workdir, "testdir");
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    67
        File srcdir= new File (workdir, "test3");
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    68
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    69
        copyDir (srcdir, testdir);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    70
        test (testdir, true, false);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    71
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    72
    }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    73
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    74
    // create a loader on jarfile (or directory)
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    75
    // load a class , then look for a resource
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    76
    // then close the loader
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    77
    // check further new classes/resources cannot be loaded
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    78
    // check jar (or dir) can be deleted
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    79
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    80
    static void test (File file, boolean loadclass, boolean readall)
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    81
        throws Exception
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    82
    {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    83
        URL[] urls = new URL[] {file.toURL()};
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    84
        System.out.println ("Doing tests with URL: " + urls[0]);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    85
        URLClassLoader loader = new URLClassLoader (urls);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    86
        if (loadclass) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    87
            Class testclass = loadClass ("com.foo.TestClass", loader, true);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    88
        }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    89
        InputStream s = loader.getResourceAsStream ("hello.txt");
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    90
        s.read();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    91
        if (readall) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    92
            while (s.read() != -1) ;
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    93
            s.close();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    94
        }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    95
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    96
        loader.close ();
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    97
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    98
        // shouuld not find bye.txt now
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
    99
        InputStream s1 = loader.getResourceAsStream("bye.txt");
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   100
        if (s1 != null) {
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   101
            throw new RuntimeException ("closed loader returned resource");
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   102
        }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   103
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   104
        // now check we can delete the path
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   105
        rm_minus_rf (file);
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   106
        System.out.println (" ... OK");
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   107
    }
fb83b663cadc 6829919: URLClassLoader.close() doesn't close resource file if getResourceAsStream(...) was called before
michaelm
parents:
diff changeset
   108
}