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