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.Platform
  32  *        JarUtils
  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 
  48 public class GetResourceAsStream extends Common {
  49     private static  final String WORK_DIR = System.getProperty("user.dir");
  50 
  51 /*
  52  * We simply test various scenarios with class/resource files
  53  * and make sure the files can be deleted after closing
  54  * the loader. Therefore, the test will only really be verified
  55  * on Windows. It will still run correctly on other platforms
  56  */
  57     public static void main (String args[]) throws Exception {
  58         setup();
  59 
  60         /* the jar we copy for each test */
  61         File srcfile = new File(WORK_DIR, "foo.jar");
  62 
  63         /* the jar we use for the test */
  64         File testfile = new File(WORK_DIR, "test.jar");
  65 
  66         copyFile(srcfile, testfile);
  67         test(testfile, false, false);
  68 
  69         copyFile(srcfile, testfile);
  70         test(testfile, true, false);
  71 
  72         copyFile(srcfile, testfile);
  73         test(testfile, true, true);
  74 
  75         // repeat test using a directory of files
  76 
  77         File testdir = new File(WORK_DIR, "testdir");
  78         File srcdir = new File(WORK_DIR, "test3");
  79 
  80         copyDir(srcdir, testdir);
  81         test(testdir, true, false);
  82     }
  83 
  84     // create a loader on jarfile (or directory)
  85     // load a class , then look for a resource
  86     // then close the loader
  87     // check further new classes/resources cannot be loaded
  88     // check jar (or dir) can be deleted
  89 
  90     static void test (File file, boolean loadclass, boolean readall)
  91         throws Exception
  92     {
  93         URL[] urls = new URL[] {file.toURI().toURL()};
  94         System.out.println ("Doing tests with URL: " + urls[0]);
  95         URLClassLoader loader = new URLClassLoader (urls);
  96         if (loadclass) {
  97             Class testclass = loadClass ("com.foo.TestClass", loader, true);
  98         }
  99         InputStream s = loader.getResourceAsStream ("hello.txt");
 100         s.read();
 101         if (readall) {
 102             while (s.read() != -1) ;
 103             s.close();
 104         }
 105 
 106         loader.close ();
 107 
 108         // should not find bye.txt now
 109         InputStream s1 = loader.getResourceAsStream("bye.txt");
 110         if (s1 != null) {
 111             throw new RuntimeException ("closed loader returned resource");
 112         }
 113 
 114         // now check we can delete the path
 115         rm_minus_rf (file);
 116         System.out.println (" ... OK");
 117     }
 118 
 119     /**
 120      * Prepare jars files for the tests
 121      */
 122     private static void setup () throws IOException {
 123         Path classes = Paths.get(WORK_DIR);
 124         Path testSrc = Paths.get(System.getProperty("test.src"),
 125                 "test1", "com", "foo", "TestClass.java");
 126         Path targetDir = classes.resolve("test3");
 127         Path testTarget = targetDir.resolve("TestClass.java");
 128         Files.createDirectories(targetDir);
 129         Files.copy(testSrc, testTarget, StandardCopyOption.REPLACE_EXISTING);
 130         // Compile sources for corresponding test
 131         CompilerUtils.compile(targetDir, targetDir);
 132         // Prepare txt files
 133         Files.write(targetDir.resolve("hello.txt"), "Hello world".getBytes(),
 134                     StandardOpenOption.CREATE);
 135         Files.write(targetDir.resolve("bye.txt"), "Bye world".getBytes(),
 136                     StandardOpenOption.CREATE);
 137         // Create jar
 138         JarUtils.createJarFile(classes.resolve("foo.jar"), targetDir);
 139     }
 140 }