1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4640520 6354623 7198496
  27  * @summary Unit test for java.util.ServiceLoader
  28  * @library /test/lib
  29  * @build JarUtils jdk.test.lib.process.*
  30  *        Basic Load FooService FooProvider1 FooProvider2 FooProvider3 BarProvider
  31  * @run testng ServiceLoaderBasicTest
  32  */
  33 
  34 
  35 import java.io.File;
  36 import java.nio.file.Files;
  37 import java.nio.file.Path;
  38 import java.util.ArrayList;
  39 import java.util.List;
  40 
  41 import jdk.test.lib.JDKToolFinder;
  42 import jdk.test.lib.Utils;
  43 import jdk.test.lib.process.ProcessTools;
  44 
  45 import org.testng.annotations.BeforeClass;
  46 import org.testng.annotations.DataProvider;
  47 import org.testng.annotations.Test;
  48 
  49 import static java.nio.file.StandardOpenOption.CREATE;
  50 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
  51 import static java.util.Arrays.asList;
  52 
  53 public class ServiceLoaderBasicTest {
  54 
  55     private static final String METAINFO = "META-INF/services/FooService";
  56     private static final Path XTEST_CONFIG = Path.of("x.test").resolve(METAINFO);
  57     private static final Path XMETA_CONFIG = Path.of("x.meta").resolve(METAINFO);
  58     private static final Path P2JAR = Path.of("p2.jar");
  59     private static final Path P2DUPJAR = Path.of("p2dup.jar");
  60     private static final Path P3JAR = Path.of("x.ext", "p3.jar");
  61 
  62     private static final String XTEST = File.pathSeparator + "x.test";
  63     private static final String XMETA = File.pathSeparator + "x.meta";
  64     private static final String P2 = File.pathSeparator + P2JAR.toString();
  65     private static final String P2DUP = File.pathSeparator + P2DUPJAR.toString();
  66     private static final String P3 = File.pathSeparator + P3JAR.toString();
  67 
  68     private static final String XTEST_CP = Utils.TEST_CLASS_PATH + XTEST;
  69     private static final String P2_CP = Utils.TEST_CLASS_PATH + P2;
  70     private static final String P2DUP_CP = P2_CP + P2DUP;
  71     private static final String P3P2_CP = Utils.TEST_CLASS_PATH + P3 + P2;
  72     private static final String XTESTP2_CP = XTEST_CP + P2;
  73     private static final String P3XTEST_CP = Utils.TEST_CLASS_PATH + P3 + XTEST;
  74     private static final String P3XTESTP2_CP = P3XTEST_CP + P2;
  75     private static final String XMETA_CP = Utils.TEST_CLASS_PATH + XMETA;
  76     private static final String XMETAXTEST_CP = XMETA_CP + XTEST;
  77     private static final String XTESTXMETA_CP = XTEST_CP + XMETA;
  78     private static final String XTESTXMETAP2_CP = XTESTXMETA_CP + P2;
  79 
  80     @BeforeClass
  81     public void initialize() throws Exception {
  82         createProviderConfig(XTEST_CONFIG, "FooProvider1");
  83         createProviderConfig(XMETA_CONFIG, "FooProvider42");
  84         createJar(P2JAR, "FooProvider2", List.of("FooProvider2"));
  85         createJar(P3JAR, "FooProvider3", List.of("FooProvider3", "FooService"));
  86         Files.copy(P2JAR, P2DUPJAR, REPLACE_EXISTING);
  87     }
  88 
  89     @DataProvider
  90     public Object[][] testCases() {
  91         return new Object[][]{
  92             //       CLI options,            Test,       Runtime arguments
  93             // Success cases
  94             {List.of("-cp", XTESTP2_CP,      "Basic")},
  95             {List.of("-cp", XTEST_CP,        "Load",     "FooProvider1")},
  96             {List.of("-cp", P2_CP,           "Load",     "FooProvider2")},
  97             {List.of("-cp", P2DUP_CP,        "Load",     "FooProvider2")},
  98             {List.of("-cp", P3P2_CP,         "Load",     "FooProvider3", "FooProvider2")},
  99             {List.of("-cp", XTESTP2_CP,      "Load",     "FooProvider1", "FooProvider2")},
 100             {List.of("-cp", P3XTEST_CP,      "Load",     "FooProvider3", "FooProvider1")},
 101             {List.of("-cp", P3XTESTP2_CP,    "Load",     "FooProvider3",
 102                                                          "FooProvider1",
 103                                                          "FooProvider2")},
 104             // Failures followed by successes
 105             {List.of("-cp", XTESTXMETA_CP,   "Load",     "FooProvider1", "fail")},
 106             {List.of("-cp", XMETAXTEST_CP,   "Load",     "fail", "FooProvider1")},
 107             {List.of("-cp", XTESTXMETAP2_CP, "Load",     "FooProvider1", "fail", "FooProvider2")}
 108         };
 109     }
 110 
 111     @DataProvider
 112     public Object[][] negativeTestCases() {
 113         return new Object[][]{
 114             {"blah blah"},
 115             {"9234"},
 116             {"X!"},
 117             {"BarProvider"},
 118             {"FooProvider42"}
 119         };
 120     }
 121 
 122     @Test(dataProvider = "testCases")
 123     public void testProvider(List<String> args) throws Throwable {
 124         runJava(args);
 125     }
 126 
 127     @Test(dataProvider = "negativeTestCases")
 128     public void testBadProvider(String providerName) throws Throwable {
 129         Files.write(XMETA_CONFIG, providerName.getBytes());
 130         runJava(List.of("-cp", XMETA_CP, "Load", "fail"));
 131     }
 132 
 133     private void runJava(List<String> opts) throws Throwable {
 134         List<String> cmds = new ArrayList<>();
 135         cmds.add(JDKToolFinder.getJDKTool("java"));
 136         cmds.addAll(asList(Utils.getTestJavaOpts()));
 137         cmds.addAll(opts);
 138 
 139         ProcessTools.executeCommand(cmds.stream()
 140                     .filter(t -> !t.isEmpty())
 141                     .toArray(String[]::new))
 142                     .shouldHaveExitValue(0);
 143     }
 144 
 145     private void createProviderConfig(Path config, String providerName) throws Exception {
 146         Files.createDirectories(config.getParent());
 147         Files.write(config, providerName.getBytes(), CREATE);
 148     }
 149 
 150     private void createJar(Path jar, String provider, List<String> files) throws Exception {
 151         Path xdir = Path.of(provider);
 152         createProviderConfig(xdir.resolve(METAINFO), provider);
 153 
 154         for (String f : files) {
 155             Path source = Path.of(Utils.TEST_CLASSES, f + ".class");
 156             Path target = xdir.resolve(source.getFileName());
 157             Files.copy(source, target, REPLACE_EXISTING);
 158         }
 159         JarUtils.createJarFile(jar, xdir);
 160     }
 161 
 162 }