Path: blob/master/test/jdk/java/lang/ClassLoader/getResource/automaticmodules/Main.java
41159 views
/*1* Copyright (c) 2017, 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.IOException;24import java.io.InputStream;25import java.lang.module.ModuleReader;26import java.lang.module.ModuleReference;27import java.lang.module.ResolvedModule;28import java.net.URL;29import java.util.Enumeration;3031/**32* Usage: Main $MODULE33*34* Finds $MODULE in the boot layer and then tests that it can locate every35* resource in the module content (JAR file).36*/3738public class Main {3940static void testFind(String name) throws Exception {41// getResource42URL url = Main.class.getClassLoader().getResource(name);43if (url == null)44throw new RuntimeException("Unable to locate: " + name);45System.out.println(name + " => " + url);4647// getResources48Enumeration<URL> urls = Main.class.getClassLoader().getResources(name);49if (!urls.hasMoreElements())50throw new RuntimeException("Unable to locate: " + name);51URL first = urls.nextElement();52if (!first.toURI().equals(url.toURI()))53throw new RuntimeException("found " + first + " ???");5455// getResourceAsStream56if (!url.toString().endsWith("/")) {57InputStream in = Main.class.getClassLoader().getResourceAsStream(name);58if (in == null)59throw new RuntimeException("Unable to locate: " + name);60in.close();61}62}6364static void testFindUnchecked(String name) {65try {66testFind(name);67} catch (Exception e) {68throw new RuntimeException(e);69}70}7172public static void main(String[] args) throws Exception {73String mn = args[0];7475ModuleReference mref = ModuleLayer.boot()76.configuration()77.findModule(mn)78.map(ResolvedModule::reference)79.orElseThrow(() -> new RuntimeException(mn + " not resolved!!"));8081try (ModuleReader reader = mref.open()) {82reader.list().forEach(name -> {83testFindUnchecked(name);8485// if the resource is a directory then find without trailing slash86if (name.endsWith("/")) {87testFindUnchecked(name.substring(0, name.length() - 1));88}89});90}91}92}939495