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  * @bug 8173374
  27  * @summary Tests module dot graph
  28  * @modules java.desktop
  29  *          java.sql
  30  *          jdk.jdeps/com.sun.tools.jdeps
  31  * @run testng DotFileTest
  32  */
  33 
  34 import java.nio.file.Files;
  35 import java.nio.file.Path;
  36 import java.nio.file.Paths;
  37 import java.util.Set;
  38 
  39 import java.util.regex.Matcher;
  40 import java.util.regex.Pattern;
  41 import java.util.spi.ToolProvider;
  42 import java.util.stream.Collectors;
  43 
  44 import org.testng.annotations.DataProvider;
  45 import org.testng.annotations.Test;
  46 
  47 import static org.testng.Assert.assertTrue;
  48 import static org.testng.Assert.assertEquals;
  49 
  50 public class DotFileTest {
  51     private static final ToolProvider JDEPS = ToolProvider.findFirst("jdeps")
  52         .orElseThrow(() -> new RuntimeException("jdeps not found"));
  53 
  54     private static final Path DOTS_DIR = Paths.get("dots");
  55     private static final Path SPEC_DIR = Paths.get("spec");
  56 
  57     @DataProvider(name = "modules")
  58     public Object[][] modules() {
  59         return new Object[][]{
  60             {"java.desktop", Set.of("java.datatransfer -> java.base",
  61                                     "java.desktop -> java.datatransfer",
  62                                     "java.desktop -> java.prefs",
  63                                     "java.prefs -> java.xml",
  64                                     "java.xml -> java.base" )
  65             },
  66             { "java.sql",    Set.of("java.logging -> java.base",
  67                                     "java.sql -> java.logging",
  68                                     "java.sql -> java.xml",
  69                                     "java.xml -> java.base" )
  70             }
  71         };
  72     }
  73     @DataProvider(name = "specVersion")
  74     public Object[][] specVersion() {
  75         return new Object[][]{
  76             {"java.desktop", Set.of("java.datatransfer -> java.base",
  77                                     "java.desktop -> java.datatransfer",
  78                                     "java.desktop -> java.xml",
  79                                     "java.xml -> java.base")
  80             },
  81             { "java.sql",    Set.of("java.logging -> java.base",
  82                                     "java.sql -> java.logging",
  83                                     "java.sql -> java.xml",
  84                                     "java.xml -> java.base" )
  85             }
  86         };
  87     }
  88 
  89     @Test(dataProvider = "modules")
  90     public void test(String name, Set<String> edges) throws Exception {
  91         String[] options = new String[] {
  92             "-dotoutput", DOTS_DIR.toString(),
  93             "-s", "-m", name
  94         };
  95         assertTrue(JDEPS.run(System.out, System.out, options) == 0);
  96 
  97         Path path = DOTS_DIR.resolve(name + ".dot");
  98         assertTrue(Files.exists(path));
  99         Set<String> lines = Files.readAllLines(path).stream()
 100                                  .filter(l -> l.contains(" -> "))
 101                                  .map(this::split)
 102                                  .collect(Collectors.toSet());
 103         assertEquals(lines, edges);
 104     }
 105 
 106     @Test(dataProvider = "specVersion")
 107     public void testAPIOnly(String name, Set<String> edges) throws Exception {
 108         String[] options = new String[]{
 109             "-dotoutput", SPEC_DIR.toString(),
 110             "-s", "-apionly",
 111             "-m", name
 112         };
 113         assertTrue(JDEPS.run(System.out, System.out, options) == 0);
 114 
 115         Path path = SPEC_DIR.resolve(name + ".dot");
 116         assertTrue(Files.exists(path));
 117         Set<String> lines = Files.readAllLines(path).stream()
 118                                  .filter(l -> l.contains(" -> "))
 119                                  .map(this::split)
 120                                  .collect(Collectors.toSet());
 121         assertEquals(lines, edges);
 122     }
 123 
 124     static Pattern PATTERN = Pattern.compile(" *\"(\\S+)\" -> \"(\\S+)\" .*");
 125     String split(String line) {
 126         Matcher pm = PATTERN.matcher(line);
 127         assertTrue(pm.find());
 128         return String.format("%s -> %s", pm.group(1), pm.group(2));
 129     }
 130 }