1 /*
   2  * Copyright (c) 2016, 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 
  53     private static final String SPLIT_PKG_NAME = "javax.annotation";
  54     private static final String JAVA_ANNOTATIONS_COMMON = "java.annotations.common";
  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         // Test jdeps classes
  67         runTest(null);
  68         // Test jdeps --add-modules
  69         runTest(JAVA_ANNOTATIONS_COMMON, SPLIT_PKG_NAME);
  70     }
  71 
  72     private void runTest(String root, String... splitPackages) throws Exception {
  73         String cmd = String.format("jdeps -verbose:class --add-modules %s %s%n",
  74             root, CLASSES_DIR);
  75 
  76         try (JdepsUtil.Command jdeps = JdepsUtil.newCommand(cmd)) {
  77             jdeps.verbose("-verbose:class")
  78                 .addRoot(CLASSES_DIR);
  79             if (root != null)
  80                 jdeps.addmods(Set.of(root));
  81 
  82             JdepsConfiguration config = jdeps.configuration();
  83             Map<String, Set<String>> pkgs = config.splitPackages();
  84 
  85             final Set<String> expected;
  86             if (splitPackages != null) {
  87                 expected = Arrays.stream(splitPackages).collect(Collectors.toSet());
  88             } else {
  89                 expected = Collections.emptySet();
  90             }
  91 
  92             if (!pkgs.keySet().equals(expected)) {
  93                 throw new RuntimeException(splitPackages.toString());
  94             }
  95 
  96             // java.annotations.common is not observable
  97             DepsAnalyzer analyzer = jdeps.getDepsAnalyzer();
  98 
  99             assertTrue(analyzer.run());
 100 
 101             jdeps.dumpOutput(System.err);
 102         }
 103     }
 104 }