1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 6899919
  27  * @library /test/lib
  28  * @modules jdk.compiler
  29  * @build jdk.test.lib.compiler.CompilerUtils
  30  *        jdk.test.lib.util.FileUtils
  31  *        jdk.test.lib.util.JarUtils
  32  *        jdk.test.lib.Platform
  33  * @run main/othervm GetResourceAsStream
  34  */
  35 
  36 import java.io.File;
  37 import java.io.IOException;
  38 import java.io.InputStream;
  39 import java.net.URL;
  40 import java.net.URLClassLoader;
  41 import java.nio.file.Files;
  42 import java.nio.file.Path;
  43 import java.nio.file.Paths;
  44 import java.nio.file.StandardCopyOption;
  45 import java.nio.file.StandardOpenOption;
  46 import jdk.test.lib.compiler.CompilerUtils;
  47 import jdk.test.lib.util.JarUtils;
  48 
  49 public class GetResourceAsStream extends Common {
  50     private static  final String WORK_DIR = System.getProperty("user.dir");
  51 
  52 /*
  53  * We simply test various scenarios with class/resource files
  54  * and make sure the files can be deleted after closing
  55  * the loader. Therefore, the test will only really be verified
  56  * on Windows. It will still run correctly on other platforms
  57  */
  58     public static void main (String args[]) throws Exception {
  59         setup();
  60 
  61         /* the jar we copy for each test */
  62         File srcfile = new File(WORK_DIR, "foo.jar");
  63 
  64         /* the jar we use for the test */
  65         File testfile = new File(WORK_DIR, "test.jar");
  66 
  67         copyFile(srcfile, testfile);
  68         test(testfile, false, false);
  69 
  70         copyFile(srcfile, testfile);
  71         test(testfile, true, false);
  72 
  73         copyFile(srcfile, testfile);
  74         test(testfile, true, true);
  75 
  76         // repeat test using a directory of files
  77 
  78         File testdir = new File(WORK_DIR, "testdir");
  79         File srcdir = new File(WORK_DIR, "test3");
  80 
  81         copyDir(srcdir, testdir);
  82         test(testdir, true, false);
  83     }
  84 
  85     // create a loader on jarfile (or directory)
  86     // load a class , then look for a resource
  87     // then close the loader
  88     // check further new classes/resources cannot be loaded
  89     // check jar (or dir) can be deleted
  90 
  91     static void test (File file, boolean loadclass, boolean readall)
  92         throws Exception
  93     {
  94         URL[] urls = new URL[] {file.toURI().toURL()};
  95         System.out.println ("Doing tests with URL: " + urls[0]);
  96         URLClassLoader loader = new URLClassLoader (urls);
  97         if (loadclass) {
  98             Class testclass = loadClass ("com.foo.TestClass", loader, true);
  99         }
 100         InputStream s = loader.getResourceAsStream ("hello.txt");
 101         s.read();
 102         if (readall) {
 103             while (s.read() != -1) ;
 104             s.close();
 105         }
 106 
 107         loader.close ();
 108 
 109         // should not find bye.txt now
 110         InputStream s1 = loader.getResourceAsStream("bye.txt");
 111         if (s1 != null) {
 112             throw new RuntimeException ("closed loader returned resource");
 113         }
 114 
 115         // now check we can delete the path
 116         rm_minus_rf (file);
 117         System.out.println (" ... OK");
 118     }
 119 
 120     /**
 121      * Prepare jars files for the tests
 122      */
 123     private static void setup () throws IOException {
 124         Path classes = Paths.get(WORK_DIR);
 125         Path testSrc = Paths.get(System.getProperty("test.src"),
 126                 "test1", "com", "foo", "TestClass.java");
 127         Path targetDir = classes.resolve("test3");
 128         Path testTarget = targetDir.resolve("TestClass.java");
 129         Files.createDirectories(targetDir);
 130         Files.copy(testSrc, testTarget, StandardCopyOption.REPLACE_EXISTING);
 131         // Compile sources for corresponding test
 132         CompilerUtils.compile(targetDir, targetDir);
 133         // Prepare txt files
 134         Files.write(targetDir.resolve("hello.txt"), "Hello world".getBytes(),
 135                     StandardOpenOption.CREATE);
 136         Files.write(targetDir.resolve("bye.txt"), "Bye world".getBytes(),
 137                     StandardOpenOption.CREATE);
 138         // Create jar
 139         JarUtils.createJarFile(classes.resolve("foo.jar"), targetDir);
 140     }
 141 }