1 /*
2  * Copyright (c) 2017, 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  * @summary Tests split packages
27  * @library ../lib
28  * @build CompilerUtils
29  * @modules jdk.jdeps/com.sun.tools.jdeps
30  * @run testng SplitPackage
31  */
32 
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.Map;
38 import java.util.Set;
39 import java.util.stream.Collectors;
40 
41 import com.sun.tools.jdeps.DepsAnalyzer;
42 import com.sun.tools.jdeps.JdepsConfiguration;
43 import org.testng.annotations.BeforeTest;
44 import org.testng.annotations.Test;
45 
46 import static org.testng.Assert.assertTrue;
47 
48 public class SplitPackage {
49     private static final String TEST_SRC = System.getProperty("test.src");
50 
51     private static final Path CLASSES_DIR = Paths.get("classes");
52     private static final Path PATCHES_DIR = Paths.get(TEST_SRC, "patches");
53 
54     private static final String SPLIT_PKG_NAME = "java.sql";
55     private static final String MODULE_NAME  = "java.sql";
56     /**
57      * Compiles classes used by the test
58      */
59     @BeforeTest
compileAll()60     public void compileAll() throws Exception {
61         CompilerUtils.cleanDir(CLASSES_DIR);
62         assertTrue(CompilerUtils.compile(PATCHES_DIR, CLASSES_DIR, "--patch-module", "java.sql=" + PATCHES_DIR));
63     }
64 
65     @Test
runTest()66     public void runTest() throws Exception {
67         // split package detected because of java.sql is in the root set
68         runTest(MODULE_NAME, SPLIT_PKG_NAME);
69         runTest("ALL-SYSTEM", SPLIT_PKG_NAME);
70         // default
71         runTest(null, SPLIT_PKG_NAME);
72 
73         // Test jdeps classes
74         runTest("ALL-DEFAULT", SPLIT_PKG_NAME);
75 
76     }
77 
runTest(String root, String... splitPackages)78     private void runTest(String root, String... splitPackages) throws Exception {
79         String cmd = String.format("jdeps -verbose:class --add-modules %s %s%n",
80             root, CLASSES_DIR);
81 
82         try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) {
83             jdeps.verbose("-verbose:class")
84                 .addRoot(CLASSES_DIR);
85             if (root != null)
86                 jdeps.addmods(Set.of(root));
87 
88             JdepsConfiguration config = jdeps.configuration();
89             Map<String, Set<String>> pkgs = config.splitPackages();
90 
91             final Set<String> expected;
92             if (splitPackages != null) {
93                 expected = Arrays.stream(splitPackages).collect(Collectors.toSet());
94             } else {
95                 expected = Collections.emptySet();
96             }
97 
98             if (!pkgs.keySet().equals(expected)) {
99                 throw new RuntimeException(splitPackages.toString());
100             }
101 
102             DepsAnalyzer analyzer = jdeps.getDepsAnalyzer();
103 
104             assertTrue(analyzer.run());
105 
106             jdeps.dumpOutput(System.err);
107         }
108     }
109 }
110