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 4991526 6514993 7197662
  27  * @summary Unit test for Preferences jar providers
  28  * @library /test/lib
  29  * @build jdk.test.lib.util.JarUtils jdk.test.lib.process.*
  30  *        PrefsSpi StubPreferencesFactory StubPreferences 
  31  * @run testng PrefsSpiTest
  32  */
  33 
  34 import java.io.File;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.util.ArrayList;
  38 import java.util.List;
  39 
  40 import jdk.test.lib.JDKToolFinder;
  41 import jdk.test.lib.Utils;
  42 import jdk.test.lib.process.ProcessTools;
  43 import jdk.test.lib.util.JarUtils;
  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.StandardCopyOption.REPLACE_EXISTING;
  50 import static java.nio.file.StandardOpenOption.CREATE;
  51 import static java.util.Arrays.asList;
  52 
  53 import static jdk.test.lib.Utils.TEST_CLASSES;
  54 import static jdk.test.lib.Utils.TEST_CLASS_PATH;
  55 
  56 public class PrefsSpiTest {
  57 
  58     private static final Path SPIJAR = Path.of("extDir", "PrefsSpi.jar");
  59     private static final String SPIJAR_CP = TEST_CLASS_PATH
  60             + File.pathSeparator + SPIJAR.toString();
  61 
  62     @BeforeClass
  63     public void initialize() throws Exception {
  64         Path xdir = Path.of("jarDir");
  65 
  66         Path config = xdir.resolve("META-INF/services/java.util.prefs.PreferencesFactory");
  67         Files.createDirectories(config.getParent());
  68         Files.write(config, "StubPreferencesFactory".getBytes(), CREATE);
  69 
  70         String[] files = {"StubPreferencesFactory.class", "StubPreferences.class"};
  71         for (String f : files) {
  72             Path source = Path.of(TEST_CLASSES, f);
  73             Path target = xdir.resolve(source.getFileName());
  74             Files.copy(source, target, REPLACE_EXISTING);
  75         }
  76 
  77         JarUtils.createJarFile(SPIJAR, xdir);
  78     }
  79 
  80     @DataProvider
  81     public Object[][] testCases() {
  82         return new Object[][]{
  83             // CLI options,                        runtime arguments
  84             {List.of("-cp", SPIJAR_CP,
  85                      "-Djava.util.prefs.PreferencesFactory=StubPreferencesFactory"),
  86                                                    List.of("StubPreferences")},
  87             {List.of("-cp", TEST_CLASS_PATH),      List.of("java.util.prefs.*")},
  88             {List.of("-cp", SPIJAR_CP),            List.of("StubPreferences")}
  89         };
  90     }
  91 
  92     @Test(dataProvider = "testCases")
  93     public void testProvider(List<String> opts, List<String> expected) throws Throwable {
  94         List<String> args = new ArrayList<>();
  95         args.add(JDKToolFinder.getJDKTool("java"));
  96         args.addAll(asList(Utils.getTestJavaOpts()));
  97         args.addAll(opts);
  98         args.add("-Djava.util.prefs.userRoot=.");
  99         args.add(PrefsSpi.class.getName());
 100         args.addAll(expected);
 101 
 102         ProcessTools.executeCommand(args.stream()
 103                                         .filter(t -> !t.isEmpty())
 104                                         .toArray(String[]::new))
 105                     .shouldHaveExitValue(0);
 106     }
 107 
 108 }