Path: blob/master/test/jdk/tools/jlink/bindservices/SuggestProviders.java
41149 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.File;24import java.io.PrintWriter;25import java.io.StringWriter;26import java.nio.file.Files;27import java.nio.file.Path;28import java.nio.file.Paths;29import java.util.ArrayList;30import java.util.List;31import java.util.spi.ToolProvider;32import java.util.stream.Collectors;33import java.util.stream.Stream;34import jdk.test.lib.compiler.CompilerUtils;3536import org.testng.annotations.BeforeTest;37import org.testng.annotations.Test;38import static org.testng.Assert.*;3940/**41* @test42* @bug 817482643* @library /lib/testlibrary /test/lib44* @modules jdk.charsets jdk.compiler jdk.jlink45* @build SuggestProviders jdk.test.lib.compiler.CompilerUtils46* @run testng SuggestProviders47*/4849public class SuggestProviders {50private static final String JAVA_HOME = System.getProperty("java.home");51private static final String TEST_SRC = System.getProperty("test.src");5253private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");54private static final Path MODS_DIR = Paths.get("mods");5556private static final String MODULE_PATH =57Paths.get(JAVA_HOME, "jmods").toString() +58File.pathSeparator + MODS_DIR.toString();5960// the names of the modules in this test61private static String[] modules = new String[] {"m1", "m2", "m3"};626364private static boolean hasJmods() {65if (!Files.exists(Paths.get(JAVA_HOME, "jmods"))) {66System.err.println("Test skipped. NO jmods directory");67return false;68}69return true;70}7172/*73* Compiles all modules used by the test74*/75@BeforeTest76public void compileAll() throws Throwable {77if (!hasJmods()) return;7879for (String mn : modules) {80Path msrc = SRC_DIR.resolve(mn);81assertTrue(CompilerUtils.compile(msrc, MODS_DIR,82"--module-source-path", SRC_DIR.toString()));83}84}8586// check a subset of services used by java.base87private final List<String> JAVA_BASE_USES = List.of(88"uses java.lang.System$LoggerFinder",89"uses java.net.ContentHandlerFactory",90"uses java.net.spi.URLStreamHandlerProvider",91"uses java.nio.channels.spi.AsynchronousChannelProvider",92"uses java.nio.channels.spi.SelectorProvider",93"uses java.nio.charset.spi.CharsetProvider",94"uses java.nio.file.spi.FileSystemProvider",95"uses java.nio.file.spi.FileTypeDetector",96"uses java.security.Provider",97"uses java.util.spi.ToolProvider"98);99100private final List<String> JAVA_BASE_PROVIDERS = List.of(101"java.base provides java.nio.file.spi.FileSystemProvider used by java.base"102);103104private final List<String> SYSTEM_PROVIDERS = List.of(105"jdk.charsets provides java.nio.charset.spi.CharsetProvider used by java.base",106"jdk.compiler provides java.util.spi.ToolProvider used by java.base",107"jdk.compiler provides javax.tools.JavaCompiler used by java.compiler",108"jdk.jlink provides jdk.tools.jlink.plugin.Plugin used by jdk.jlink",109"jdk.jlink provides java.util.spi.ToolProvider used by java.base"110);111112private final List<String> APP_USES = List.of(113"uses p1.S",114"uses p2.T"115);116117private final List<String> APP_PROVIDERS = List.of(118"m1 provides p1.S used by m1",119"m2 provides p1.S used by m1",120"m2 provides p2.T used by m2",121"m3 provides p2.T used by m2",122"m3 provides p3.S not used by any observable module"123);124125@Test126public void suggestProviders() throws Throwable {127if (!hasJmods()) return;128129List<String> output = JLink.run("--module-path", MODULE_PATH,130"--suggest-providers").output();131132Stream<String> uses =133Stream.concat(JAVA_BASE_USES.stream(), APP_USES.stream());134Stream<String> providers =135Stream.concat(SYSTEM_PROVIDERS.stream(), APP_PROVIDERS.stream());136137assertTrue(output.containsAll(Stream.concat(uses, providers)138.collect(Collectors.toList())));139}140141/**142* find providers from the observable modules and --add-modules has no143* effect on the suggested providers144*/145@Test146public void observableModules() throws Throwable {147if (!hasJmods()) return;148149List<String> output = JLink.run("--module-path", MODULE_PATH,150"--add-modules", "m1",151"--suggest-providers").output();152153Stream<String> uses =154Stream.concat(JAVA_BASE_USES.stream(), Stream.of("uses p1.S"));155Stream<String> providers =156Stream.concat(SYSTEM_PROVIDERS.stream(), APP_PROVIDERS.stream());157158assertTrue(output.containsAll(Stream.concat(uses, providers)159.collect(Collectors.toList())));160}161162/**163* find providers from the observable modules with --limit-modules164*/165@Test166public void limitModules() throws Throwable {167if (!hasJmods()) return;168169List<String> output = JLink.run("--module-path", MODULE_PATH,170"--limit-modules", "m1",171"--suggest-providers").output();172173Stream<String> uses =174Stream.concat(JAVA_BASE_USES.stream(), Stream.of("uses p1.S"));175Stream<String> providers =176Stream.concat(JAVA_BASE_PROVIDERS.stream(),177Stream.of("m1 provides p1.S used by m1")178);179180assertTrue(output.containsAll(Stream.concat(uses, providers)181.collect(Collectors.toList())));182}183184@Test185public void providersForServices() throws Throwable {186if (!hasJmods()) return;187188List<String> output =189JLink.run("--module-path", MODULE_PATH,190"--suggest-providers",191"java.nio.charset.spi.CharsetProvider,p1.S").output();192193System.out.println(output);194Stream<String> expected = Stream.concat(195Stream.of("jdk.charsets provides java.nio.charset.spi.CharsetProvider used by java.base"),196Stream.of("m1 provides p1.S used by m1",197"m2 provides p1.S used by m1")198);199200assertTrue(output.containsAll(expected.collect(Collectors.toList())));201}202203@Test204public void unusedService() throws Throwable {205if (!hasJmods()) return;206207List<String> output =208JLink.run("--module-path", MODULE_PATH,209"--suggest-providers",210"p3.S").output();211212List<String> expected = List.of(213"m3 provides p3.S not used by any observable module"214);215assertTrue(output.containsAll(expected));216217// should not print other services m3 provides218assertFalse(output.contains("m3 provides p2.T used by m2"));219}220221@Test222public void nonExistentService() throws Throwable {223if (!hasJmods()) return;224225List<String> output =226JLink.run("--module-path", MODULE_PATH,227"--suggest-providers",228"nonExistentType").output();229230List<String> expected = List.of(231"No provider found for service specified to --suggest-providers: nonExistentType"232);233assertTrue(output.containsAll(expected));234}235236@Test237public void noSuggestProviders() throws Throwable {238if (!hasJmods()) return;239240List<String> output =241JLink.run("--module-path", MODULE_PATH,242"--bind-services",243"--suggest-providers").output();244245String expected = "--bind-services option is specified. No additional providers suggested.";246assertTrue(output.contains(expected));247248}249250@Test251public void suggestTypeNotRealProvider() throws Throwable {252if (!hasJmods()) return;253254List<String> output =255JLink.run("--module-path", MODULE_PATH,256"--add-modules", "m1",257"--suggest-providers",258"java.util.List").output();259260System.out.println(output);261List<String> expected = List.of(262"No provider found for service specified to --suggest-providers: java.util.List"263);264265assertTrue(output.containsAll(expected));266}267268@Test269public void addNonObservableModule() throws Throwable {270if (!hasJmods()) return;271272List<String> output =273JLink.run("--module-path", MODULE_PATH,274"--add-modules", "nonExistentModule",275"--suggest-providers",276"java.nio.charset.spi.CharsetProvider").output();277278System.out.println(output);279List<String> expected = List.of(280"jdk.charsets provides java.nio.charset.spi.CharsetProvider used by java.base"281);282283assertTrue(output.containsAll(expected));284}285286static class JLink {287static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")288.orElseThrow(() ->289new RuntimeException("jlink tool not found")290);291292static JLink run(String... options) {293JLink jlink = new JLink();294assertTrue(jlink.execute(options) == 0);295return jlink;296}297298final List<String> output = new ArrayList<>();299private int execute(String... options) {300System.out.println("jlink " +301Stream.of(options).collect(Collectors.joining(" ")));302303StringWriter writer = new StringWriter();304PrintWriter pw = new PrintWriter(writer);305int rc = JLINK_TOOL.run(pw, pw, options);306System.out.println(writer.toString());307Stream.of(writer.toString().split("\\v"))308.map(String::trim)309.forEach(output::add);310return rc;311}312313boolean contains(String s) {314return output.contains(s);315}316317List<String> output() {318return output;319}320}321}322323324