Path: blob/master/test/jdk/java/util/ServiceLoader/basic/ServiceLoaderBasicTest.java
41153 views
/*1* Copyright (c) 2018, 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*/2223/*24* @test25* @bug 4640520 6354623 719849626* @summary Unit test for java.util.ServiceLoader27* @library /test/lib28* @build jdk.test.lib.process.*29* jdk.test.lib.util.JarUtils30* Basic Load FooService FooProvider1 FooProvider2 FooProvider3 BarProvider31* @run testng ServiceLoaderBasicTest32*/333435import java.io.File;36import java.nio.file.Files;37import java.nio.file.Path;38import java.util.ArrayList;39import java.util.List;4041import jdk.test.lib.JDKToolFinder;42import jdk.test.lib.Utils;43import jdk.test.lib.process.ProcessTools;44import jdk.test.lib.util.JarUtils;4546import org.testng.annotations.BeforeClass;47import org.testng.annotations.DataProvider;48import org.testng.annotations.Test;4950import static java.nio.file.StandardOpenOption.CREATE;51import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;52import static java.util.Arrays.asList;5354public class ServiceLoaderBasicTest {5556private static final String METAINFO = "META-INF/services/FooService";57private static final Path XTEST_CONFIG = Path.of("x.test").resolve(METAINFO);58private static final Path XMETA_CONFIG = Path.of("x.meta").resolve(METAINFO);59private static final Path P2JAR = Path.of("p2.jar");60private static final Path P2DUPJAR = Path.of("p2dup.jar");61private static final Path P3JAR = Path.of("x.ext", "p3.jar");6263private static final String XTEST = File.pathSeparator + "x.test";64private static final String XMETA = File.pathSeparator + "x.meta";65private static final String P2 = File.pathSeparator + P2JAR.toString();66private static final String P2DUP = File.pathSeparator + P2DUPJAR.toString();67private static final String P3 = File.pathSeparator + P3JAR.toString();6869private static final String XTEST_CP = Utils.TEST_CLASS_PATH + XTEST;70private static final String P2_CP = Utils.TEST_CLASS_PATH + P2;71private static final String P2DUP_CP = P2_CP + P2DUP;72private static final String P3P2_CP = Utils.TEST_CLASS_PATH + P3 + P2;73private static final String XTESTP2_CP = XTEST_CP + P2;74private static final String P3XTEST_CP = Utils.TEST_CLASS_PATH + P3 + XTEST;75private static final String P3XTESTP2_CP = P3XTEST_CP + P2;76private static final String XMETA_CP = Utils.TEST_CLASS_PATH + XMETA;77private static final String XMETAXTEST_CP = XMETA_CP + XTEST;78private static final String XTESTXMETA_CP = XTEST_CP + XMETA;79private static final String XTESTXMETAP2_CP = XTESTXMETA_CP + P2;8081@BeforeClass82public void initialize() throws Exception {83createProviderConfig(XTEST_CONFIG, "FooProvider1");84createProviderConfig(XMETA_CONFIG, "FooProvider42");85createJar(P2JAR, "FooProvider2", List.of("FooProvider2"));86createJar(P3JAR, "FooProvider3", List.of("FooProvider3", "FooService"));87Files.copy(P2JAR, P2DUPJAR, REPLACE_EXISTING);88}8990@DataProvider91public Object[][] testCases() {92return new Object[][]{93// CLI options, Test, Runtime arguments94// Success cases95{List.of("-cp", XTESTP2_CP, "Basic")},96{List.of("-cp", XTEST_CP, "Load", "FooProvider1")},97{List.of("-cp", P2_CP, "Load", "FooProvider2")},98{List.of("-cp", P2DUP_CP, "Load", "FooProvider2")},99{List.of("-cp", P3P2_CP, "Load", "FooProvider3", "FooProvider2")},100{List.of("-cp", XTESTP2_CP, "Load", "FooProvider1", "FooProvider2")},101{List.of("-cp", P3XTEST_CP, "Load", "FooProvider3", "FooProvider1")},102{List.of("-cp", P3XTESTP2_CP, "Load", "FooProvider3",103"FooProvider1",104"FooProvider2")},105// Failures followed by successes106{List.of("-cp", XTESTXMETA_CP, "Load", "FooProvider1", "fail")},107{List.of("-cp", XMETAXTEST_CP, "Load", "fail", "FooProvider1")},108{List.of("-cp", XTESTXMETAP2_CP, "Load", "FooProvider1", "fail", "FooProvider2")}109};110}111112@DataProvider113public Object[][] negativeTestCases() {114return new Object[][]{115{"blah blah"},116{"9234"},117{"X!"},118{"BarProvider"},119{"FooProvider42"}120};121}122123@Test(dataProvider = "testCases")124public void testProvider(List<String> args) throws Throwable {125runJava(args);126}127128@Test(dataProvider = "negativeTestCases")129public void testBadProvider(String providerName) throws Throwable {130Files.write(XMETA_CONFIG, providerName.getBytes());131runJava(List.of("-cp", XMETA_CP, "Load", "fail"));132}133134private void runJava(List<String> opts) throws Throwable {135List<String> cmds = new ArrayList<>();136cmds.add(JDKToolFinder.getJDKTool("java"));137cmds.addAll(asList(Utils.getTestJavaOpts()));138cmds.addAll(opts);139140ProcessTools.executeCommand(cmds.stream()141.filter(t -> !t.isEmpty())142.toArray(String[]::new))143.shouldHaveExitValue(0);144}145146private void createProviderConfig(Path config, String providerName) throws Exception {147Files.createDirectories(config.getParent());148Files.write(config, providerName.getBytes(), CREATE);149}150151private void createJar(Path jar, String provider, List<String> files) throws Exception {152Path xdir = Path.of(provider);153createProviderConfig(xdir.resolve(METAINFO), provider);154155for (String f : files) {156Path source = Path.of(Utils.TEST_CLASSES, f + ".class");157Path target = xdir.resolve(source.getFileName());158Files.copy(source, target, REPLACE_EXISTING);159}160JarUtils.createJarFile(jar, xdir);161}162163}164165166