changeset 14519:ffe12af5613c

Add tests for ClassLoader.getResourceXXX
author mli
date Tue, 01 Dec 2015 12:04:53 +0000
parents e45ef8281247
children dbd2f64d6653
files test/java/lang/ClassLoader/getResource/modules/Main.java test/java/lang/ClassLoader/getResource/modules/ResourcesTest.java test/java/lang/ClassLoader/getResource/modules/src/m1/module-info.java test/java/lang/ClassLoader/getResource/modules/src/m1/p1/Main.java test/java/lang/ClassLoader/getResource/modules/src/m2/module-info.java test/java/lang/ClassLoader/getResource/modules/src/m2/p2/Main.java test/java/lang/ClassLoader/getResource/modules/src/m3/module-info.java test/java/lang/ClassLoader/getResource/modules/src/m3/p3/Main.java
diffstat 8 files changed, 397 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/lang/ClassLoader/getResource/modules/Main.java	Tue Dec 01 12:04:53 2015 +0000
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * 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.
+ *
+ * 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.
+ */
+
+import java.lang.module.Configuration;
+import java.lang.module.ModuleReference;
+import java.lang.reflect.Layer;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+/**
+ * Basic test of ClassLoader getResource and getResourceAsStream when
+ * invoked from code in named modules.
+ */
+
+public class Main {
+
+    static final String NAME = "myresource";
+
+    public static void main(String[] args) throws IOException {
+
+        // create m1/myresource containing "m1"
+        Path file = directoryFor("m1").resolve(NAME);
+        Files.write(file, "m1".getBytes("UTF-8"));
+
+        // create m2/myresource containing "m2"
+        file = directoryFor("m2").resolve(NAME);
+        Files.write(file, "m2".getBytes("UTF-8"));
+
+        // check that m3/myresource does not exist
+        assertTrue(Files.notExists(directoryFor("m3").resolve(NAME)));
+
+        // invoke ClassLoader getResource from the unnamed module
+        assertNull(Main.class.getClassLoader().getResource("/" + NAME));
+
+        // invoke ClassLoader getResource from modules m1-m3
+        // Resources in a named module are private to that module.
+        // ClassLoader.getResource should not find resource in named modules.
+        assertNull(p1.Main.getResourceInClassLoader("/" + NAME));
+        assertNull(p2.Main.getResourceInClassLoader("/" + NAME));
+        assertNull(p3.Main.getResourceInClassLoader("/" + NAME));
+
+        // invoke ClassLoader getResourceAsStream from the unnamed module
+        assertNull(Main.class.getClassLoader().getResourceAsStream("/" + NAME));
+
+        // invoke ClassLoader getResourceAsStream from modules m1-m3
+        // Resources in a named module are private to that module.
+        // ClassLoader.getResourceAsStream should not find resource in named modules.
+        assertNull(p1.Main.getResourceAsStreamInClassLoader("/" + NAME));
+        assertNull(p2.Main.getResourceAsStreamInClassLoader("/" + NAME));
+        assertNull(p3.Main.getResourceAsStreamInClassLoader("/" + NAME));
+
+        // SecurityManager case
+        System.setSecurityManager(new SecurityManager());
+
+        assertNull(Main.class.getClassLoader().getResource("/" + NAME));
+        assertNull(p1.Main.getResourceInClassLoader("/" + NAME));
+        assertNull(p2.Main.getResourceInClassLoader("/" + NAME));
+        assertNull(p3.Main.getResourceInClassLoader("/" + NAME));
+
+        assertNull(Main.class.getClassLoader().getResourceAsStream("/" + NAME));
+        assertNull(p1.Main.getResourceAsStreamInClassLoader("/" + NAME));
+        assertNull(p2.Main.getResourceAsStreamInClassLoader("/" + NAME));
+        assertNull(p3.Main.getResourceAsStreamInClassLoader("/" + NAME));
+
+        System.out.println("Success!");
+    }
+
+    /**
+     * Returns the directory for the given module (by name).
+     */
+    static Path directoryFor(String mn) {
+        Configuration cf = Layer.boot().configuration();
+        ModuleReference mref = cf.findModule(mn).orElse(null);
+        if (mref == null)
+            throw new RuntimeException("not found: " + mn);
+        Path dir = Paths.get(mref.location().get());
+        if (!Files.isDirectory(dir))
+            throw new RuntimeException("not a directory: " + dir);
+        return dir;
+    }
+
+    static void assertTrue(boolean condition) {
+        if (!condition) throw new RuntimeException();
+    }
+
+    static void assertNull(Object o) {
+        assertTrue(o == null);
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/lang/ClassLoader/getResource/modules/ResourcesTest.java	Tue Dec 01 12:04:53 2015 +0000
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * 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.
+ *
+ * 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.
+ */
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import static jdk.testlibrary.ProcessTools.executeTestJava;
+
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+/**
+ * @test
+ * @bug 8087335
+ * @library /jdk/jigsaw/lib /lib/testlibrary
+ * @modules jdk.compiler
+ * @build ResourcesTest CompilerUtils jdk.testlibrary.*
+ * @run testng ResourcesTest
+ * @summary Driver for basic test of ClassLoader getResource and getResourceAsStream
+ */
+
+@Test
+public class ResourcesTest {
+
+    private static final String TEST_SRC = System.getProperty("test.src");
+
+    private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
+    private static final Path CLASSES_DIR = Paths.get("classes");
+    private static final Path MODS_DIR = Paths.get("mods");
+
+    /**
+     * Compiles the modules used by the test and the test Main
+     */
+    @BeforeTest
+    public void compileAll() throws Exception {
+        boolean compiled;
+
+        // javac -modulesource mods -d mods src/**
+        compiled = CompilerUtils
+            .compile(SRC_DIR,
+                     MODS_DIR,
+                     "-modulesourcepath", SRC_DIR.toString());
+        assertTrue(compiled);
+
+        // javac -mp mods -d classes Main.java
+        compiled = CompilerUtils
+            .compile(Paths.get(TEST_SRC, "Main.java"),
+                     CLASSES_DIR,
+                     "-mp", MODS_DIR.toString());
+        assertTrue(compiled);
+    }
+
+    /**
+     * Run the test
+     */
+    public void runTest() throws Exception {
+        int exitValue
+            = executeTestJava("-mp", MODS_DIR.toString(),
+                              "-addmods", "m1,m2,m3",
+                               "-cp", CLASSES_DIR.toString(),
+                              "Main")
+                .outputTo(System.out)
+                .errorTo(System.out)
+                .getExitValue();
+
+        assertTrue(exitValue == 0);
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/lang/ClassLoader/getResource/modules/src/m1/module-info.java	Tue Dec 01 12:04:53 2015 +0000
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * 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.
+ *
+ * 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.
+ */
+
+module m1 {
+    exports p1;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/lang/ClassLoader/getResource/modules/src/m1/p1/Main.java	Tue Dec 01 12:04:53 2015 +0000
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * 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.
+ *
+ * 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.
+ */
+
+package p1;
+
+import java.io.InputStream;
+import java.net.URL;
+
+public class Main {
+    private Main() { }
+
+    public static URL getResourceInClassLoader(String name) {
+        return Main.class.getClassLoader().getResource(name);
+    }
+
+    public static InputStream getResourceAsStreamInClassLoader(String name) {
+        return Main.class.getClassLoader().getResourceAsStream(name);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/lang/ClassLoader/getResource/modules/src/m2/module-info.java	Tue Dec 01 12:04:53 2015 +0000
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * 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.
+ *
+ * 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.
+ */
+
+module m2 {
+    exports p2;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/lang/ClassLoader/getResource/modules/src/m2/p2/Main.java	Tue Dec 01 12:04:53 2015 +0000
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * 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.
+ *
+ * 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.
+ */
+
+package p2;
+
+import java.io.InputStream;
+import java.net.URL;
+
+public class Main {
+    private Main() { }
+
+    public static URL getResourceInClassLoader(String name) {
+        return Main.class.getClassLoader().getResource(name);
+    }
+
+    public static InputStream getResourceAsStreamInClassLoader(String name) {
+        return Main.class.getClassLoader().getResourceAsStream(name);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/lang/ClassLoader/getResource/modules/src/m3/module-info.java	Tue Dec 01 12:04:53 2015 +0000
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * 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.
+ *
+ * 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.
+ */
+
+module m3 {
+    exports p3;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/lang/ClassLoader/getResource/modules/src/m3/p3/Main.java	Tue Dec 01 12:04:53 2015 +0000
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * 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.
+ *
+ * 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.
+ */
+
+package p3;
+
+import java.io.InputStream;
+import java.net.URL;
+
+public class Main {
+    private Main() { }
+
+    public static URL getResourceInClassLoader(String name) {
+        return Main.class.getClassLoader().getResource(name);
+    }
+
+    public static InputStream getResourceAsStreamInClassLoader(String name) {
+        return Main.class.getClassLoader().getResourceAsStream(name);
+    }
+}