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