Path: blob/master/test/jdk/java/lang/ClassLoader/getResource/modules/Main.java
41159 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.InputStream;24import java.lang.module.Configuration;25import java.lang.module.ResolvedModule;26import java.io.IOException;27import java.net.URL;28import java.nio.file.Files;29import java.nio.file.Path;30import java.nio.file.Paths;31import java.util.ArrayList;32import java.util.Enumeration;33import java.util.List;3435/**36* Basic test of ClassLoader getResource and getResourceAsStream when37* invoked from code in named modules.38*/3940public class Main {4142static final String NAME = "myresource";4344public static void main(String[] args) throws IOException {4546// create resources in m147createResource("m1", Paths.get("."), "m1");48createResource("m1", Paths.get("p1"), "m1/p1");49createResource("m1", Paths.get("p1", "impl"), "m1/p1.impl");50createResource("m1", Paths.get("p1", "resources"), "m1/p1.resources");5152// create resources in m253createResource("m2", Paths.get("."), "m2");54createResource("m2", Paths.get("p2"), "m2/p2");55createResource("m2", Paths.get("p2", "impl"), "m2/p2.impl");56createResource("m2", Paths.get("p2", "resources"), "m2/p2.resources");575859// invoke ClassLoader getResource from the unnamed module60ClassLoader thisLoader = Main.class.getClassLoader();6162URL url = thisLoader.getResource(NAME);63assertNotNull(url);6465url = thisLoader.getResource("p1/" + NAME);66assertNull(url);6768url = thisLoader.getResource("p1/impl/" + NAME);69assertNull(url);7071url = thisLoader.getResource("p1/resources/" + NAME);72assertEquals(readAllAsString(url), "m1/p1.resources");7374url = thisLoader.getResource("p2/" + NAME);75assertNull(url);7677url = thisLoader.getResource("p2/impl/" + NAME);78assertNull(url);7980url = thisLoader.getResource("p2/resources/" + NAME);81assertNull(url);828384// invoke ClassLoader getResource from module m185url = p1.Main.getResourceInClassLoader(NAME);86assertNotNull(url);8788url = p1.Main.getResourceInClassLoader("p1/" + NAME);89assertNull(url);9091url = p1.Main.getResourceInClassLoader("p1/impl/" + NAME);92assertNull(url);9394url = p1.Main.getResourceInClassLoader("p1/resources/" + NAME);95assertEquals(readAllAsString(url), "m1/p1.resources");9697url = p1.Main.getResourceInClassLoader("p2/" + NAME);98assertNull(url);99100url = p1.Main.getResourceInClassLoader("p2/impl/" + NAME);101assertNull(url);102103url = p1.Main.getResourceInClassLoader("p2/resources/" + NAME);104assertNull(url);105106107// invoke ClassLoader getResource from module m2108url = p2.Main.getResourceInClassLoader(NAME);109assertNotNull(url);110111url = p2.Main.getResourceInClassLoader("p1/" + NAME);112assertNull(url);113114url = p2.Main.getResourceInClassLoader("p1/impl/" + NAME);115assertNull(url);116117url = p2.Main.getResourceInClassLoader("p1/resources/" + NAME);118assertEquals(readAllAsString(url), "m1/p1.resources");119120url = p2.Main.getResourceInClassLoader("p2/" + NAME);121assertNull(url);122123url = p2.Main.getResourceInClassLoader("p2/impl/" + NAME);124assertNull(url);125126url = p2.Main.getResourceInClassLoader("p2/resources/" + NAME);127assertNull(url);128129130// invoke ClassLoader getResources from the unnamed module131Enumeration<URL> urls = thisLoader.getResources(NAME);132List<String> resources = readAllAsStrings(urls);133assertTrue(resources.size() == 2);134assertTrue(resources.contains("m1"));135assertTrue(resources.contains("m2"));136137urls = thisLoader.getResources("p1/" + NAME);138assertTrue(readAllAsStrings(urls).isEmpty());139140urls = thisLoader.getResources("p1/impl/" + NAME);141assertTrue(readAllAsStrings(urls).isEmpty());142143urls = thisLoader.getResources("p1/resources/" + NAME);144resources = readAllAsStrings(urls);145assertTrue(resources.size() == 1);146assertTrue(resources.contains("m1/p1.resources"));147148urls = thisLoader.getResources("p2/" + NAME);149assertTrue(readAllAsStrings(urls).isEmpty());150151urls = thisLoader.getResources("p2/impl/" + NAME);152assertTrue(readAllAsStrings(urls).isEmpty());153154urls = thisLoader.getResources("p2/resources/" + NAME);155assertTrue(readAllAsStrings(urls).isEmpty());156157158// invoke ClassLoader getResources from m1159urls = p1.Main.getResourcesInClassLoader(NAME);160resources = readAllAsStrings(urls);161assertTrue(resources.size() == 2);162assertTrue(resources.contains("m1"));163assertTrue(resources.contains("m2"));164165urls = p1.Main.getResourcesInClassLoader("p1/" + NAME);166assertTrue(readAllAsStrings(urls).isEmpty());167168urls = p1.Main.getResourcesInClassLoader("p1/impl/" + NAME);169assertTrue(readAllAsStrings(urls).isEmpty());170171urls = p1.Main.getResourcesInClassLoader("p1/resources/" + NAME);172resources = readAllAsStrings(urls);173assertTrue(resources.size() == 1);174assertTrue(resources.contains("m1/p1.resources"));175176urls = p1.Main.getResourcesInClassLoader("p2/" + NAME);177assertTrue(readAllAsStrings(urls).isEmpty());178179urls = p1.Main.getResourcesInClassLoader("p2/impl/" + NAME);180assertTrue(readAllAsStrings(urls).isEmpty());181182urls = p1.Main.getResourcesInClassLoader("p2/resources/" + NAME);183assertTrue(readAllAsStrings(urls).isEmpty());184185186// invoke ClassLoader getResources from m2187urls = p2.Main.getResourcesInClassLoader(NAME);188resources = readAllAsStrings(urls);189assertTrue(resources.size() == 2);190assertTrue(resources.contains("m1"));191assertTrue(resources.contains("m2"));192193urls = p2.Main.getResourcesInClassLoader("p1/" + NAME);194assertTrue(readAllAsStrings(urls).isEmpty());195196urls = p2.Main.getResourcesInClassLoader("p1/impl/" + NAME);197assertTrue(readAllAsStrings(urls).isEmpty());198199urls = p2.Main.getResourcesInClassLoader("p1/resources/" + NAME);200resources = readAllAsStrings(urls);201assertTrue(resources.size() == 1);202assertTrue(resources.contains("m1/p1.resources"));203204urls = p2.Main.getResourcesInClassLoader("p2/" + NAME);205assertTrue(readAllAsStrings(urls).isEmpty());206207urls = p2.Main.getResourcesInClassLoader("p2/impl/" + NAME);208assertTrue(readAllAsStrings(urls).isEmpty());209210urls = p2.Main.getResourcesInClassLoader("p2/resources/" + NAME);211assertTrue(readAllAsStrings(urls).isEmpty());212213214// invoke ClassLoader getResourceAsStream from the unnamed module215InputStream in = thisLoader.getResourceAsStream(NAME);216assertNotNull(in);217218in = thisLoader.getResourceAsStream("p1/" + NAME);219assertNull(in);220221in = thisLoader.getResourceAsStream("p1/impl/" + NAME);222assertNull(in);223224in = thisLoader.getResourceAsStream("p1/resources/" + NAME);225assertEquals(readAllAsString(in), "m1/p1.resources");226227in = thisLoader.getResourceAsStream("p2/" + NAME);228assertNull(in);229230in = thisLoader.getResourceAsStream("p2/impl/" + NAME);231assertNull(in);232233in = thisLoader.getResourceAsStream("p2/resources/" + NAME);234assertNull(in);235236237// invoke ClassLoader getResource from modules m1238in = p1.Main.getResourceAsStreamInClassLoader(NAME);239assertNotNull(in);240241in = p1.Main.getResourceAsStreamInClassLoader("p1/" + NAME);242assertNull(in);243244in = p1.Main.getResourceAsStreamInClassLoader("p1/impl/" + NAME);245assertNull(in);246247in = p1.Main.getResourceAsStreamInClassLoader("p1/resources/" + NAME);248assertEquals(readAllAsString(in), "m1/p1.resources");249250in = p1.Main.getResourceAsStreamInClassLoader("p2/" + NAME);251assertNull(in);252253in = p1.Main.getResourceAsStreamInClassLoader("p2/impl/" + NAME);254assertNull(in);255256in = p1.Main.getResourceAsStreamInClassLoader("p2/resources/" + NAME);257assertNull(in);258259260// invoke ClassLoader getResource from modules m2261in = p2.Main.getResourceAsStreamInClassLoader(NAME);262assertNotNull(in);263264in = p2.Main.getResourceAsStreamInClassLoader("p1/" + NAME);265assertNull(in);266267in = p2.Main.getResourceAsStreamInClassLoader("p1/impl/" + NAME);268assertNull(in);269270in = p2.Main.getResourceAsStreamInClassLoader("p1/resources/" + NAME);271assertEquals(readAllAsString(in), "m1/p1.resources");272273in = p2.Main.getResourceAsStreamInClassLoader("p2/" + NAME);274assertNull(in);275276in = p2.Main.getResourceAsStreamInClassLoader("p2/impl/" + NAME);277assertNull(in);278279in = p2.Main.getResourceAsStreamInClassLoader("p2/resources/" + NAME);280assertNull(in);281282283// SecurityManager case284System.setSecurityManager(new SecurityManager());285286assertNull(Main.class.getClassLoader().getResource("/" + NAME));287assertNull(p1.Main.getResourceInClassLoader("/" + NAME));288assertNull(p2.Main.getResourceInClassLoader("/" + NAME));289290assertNull(Main.class.getClassLoader().getResourceAsStream("/" + NAME));291assertNull(p1.Main.getResourceAsStreamInClassLoader("/" + NAME));292assertNull(p2.Main.getResourceAsStreamInClassLoader("/" + NAME));293294System.out.println("Success!");295}296297/**298* Create a resource in the sub-directory of the given exploded module299*/300static void createResource(String mn, Path subdir, String msg) throws IOException {301Path dir = directoryFor(mn).resolve(subdir);302Path file = dir.resolve(NAME);303Files.write(file, msg.getBytes("UTF-8"));304}305306/**307* Returns the directory for the given module (by name).308*/309static Path directoryFor(String mn) {310Configuration cf = ModuleLayer.boot().configuration();311ResolvedModule resolvedModule = cf.findModule(mn).orElse(null);312if (resolvedModule == null)313throw new RuntimeException("not found: " + mn);314Path dir = Paths.get(resolvedModule.reference().location().get());315if (!Files.isDirectory(dir))316throw new RuntimeException("not a directory: " + dir);317return dir;318}319320static String readAllAsString(InputStream in) throws IOException {321if (in == null)322return null;323try (in) {324return new String(in.readAllBytes(), "UTF-8");325}326}327328static String readAllAsString(URL url) throws IOException {329if (url == null)330return null;331InputStream in = url.openStream();332return readAllAsString(url.openStream());333}334335static List<String> readAllAsStrings(Enumeration<URL> urls) throws IOException {336List<String> result = new ArrayList<>();337while (urls.hasMoreElements()) {338URL url = urls.nextElement();339result.add(readAllAsString(url));340}341return result;342}343344static void assertTrue(boolean condition) {345if (!condition) throw new RuntimeException();346}347348static void assertFalse(boolean condition) {349if (condition) throw new RuntimeException();350}351352static void assertNull(Object o) {353assertTrue(o == null);354}355356static void assertNotNull(Object o) {357assertTrue(o != null);358}359360static void assertEquals(Object actual, Object expected) {361if (expected == null) {362assertNull(actual);363} else {364assertTrue(expected.equals(actual));365}366}367368static void assertNotEquals(Object actual, Object expected) {369if (expected == null) {370assertNotNull(actual);371} else {372assertTrue(!expected.equals(actual));373}374}375}376377378379