Path: blob/master/test/jdk/java/lang/ClassLoader/ResourcesStreamTest.java
41149 views
/*1* Copyright (c) 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.IOException;24import java.io.UncheckedIOException;25import java.net.URL;26import java.util.Collections;27import java.util.Enumeration;28import java.util.List;29import java.util.stream.Collectors;30import java.util.stream.Stream;3132/*33* @test34* @bug 816123035* @summary Test java.lang.ClassLoader.resources() method36*37* @build ResourcesStreamTest38* @run main ResourcesStreamTest39*/40public class ResourcesStreamTest {4142public static void main(String[] args) throws Exception {43testSuccess();44testFailure();45}4647public static void testSuccess() throws Exception {48// failing part first49try {50ClassLoader cl = new FailingClassLoader();51// should create the stream pipe52Stream<URL> stream = cl.resources("the name");53// expect function to throw an exception when calling the method54stream.forEach(System.out::println);55throw new Exception("expected UncheckedIOException not thrown");56} catch (UncheckedIOException uio) {57String causeMessage = uio.getCause().getMessage();58if (!"the name".equals(causeMessage))59throw new Exception("unexpected cause message: " + causeMessage);60}61}6263public static void testFailure() throws Exception {64ClassLoader cl = new SuccessClassLoader();65long count = cl.resources("the name").count();66if (count != 1)67throw new Exception("expected resource is null or empty");6869cl.resources("the name")70.filter(url -> "file:/somefile".equals(url.toExternalForm()))71.findFirst()72.orElseThrow(() -> new Exception("correct URL not found"));73}7475public static class SuccessClassLoader extends ClassLoader {76@Override77public Enumeration<URL> getResources(String name) throws IOException {78URL url = new URL("file:/somefile");79return Collections.enumeration(Collections.singleton(url));80}81}8283public static class FailingClassLoader extends ClassLoader {84@Override85public Enumeration<URL> getResources(String name) throws IOException {86throw new IOException(name);87}88}89}909192