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.transaction.xa -> java.base",
  68                                     "java.sql -> java.logging",
  69                                     "java.sql -> java.transaction.xa",
  70                                     "java.sql -> java.xml",
  71                                     "java.xml -> java.base" )
  72             }
  73         };
  74     }
  75     @DataProvider(name = "specVersion")
  76     public Object[][] specVersion() {
  77         return new Object[][]{
  78             {"java.desktop", Set.of("java.datatransfer -> java.base",
  79                                     "java.desktop -> java.datatransfer",
  80                                     "java.desktop -> java.xml",
  81                                     "java.xml -> java.base")
  82             },
  83             { "java.sql",    Set.of("java.logging -> java.base",
  84                                     "java.transaction.xa -> java.base",
  85                                     "java.sql -> java.logging",
  86                                     "java.sql -> java.transaction.xa",
  87                                     "java.sql -> java.xml",
  88                                     "java.xml -> java.base" )
  89             }
  90         };
  91     }
  92 
  93     @Test(dataProvider = "modules")
  94     public void test(String name, Set<String> edges) throws Exception {
  95         String[] options = new String[] {
  96             "-dotoutput", DOTS_DIR.toString(),
  97             "-s", "-m", name
  98         };
  99         assertTrue(JDEPS.run(System.out, System.out, options) == 0);
 100 
 101         Path path = DOTS_DIR.resolve(name + ".dot");
 102         assertTrue(Files.exists(path));
 103         Set<String> lines = Files.readAllLines(path).stream()
 104                                  .filter(l -> l.contains(" -> "))
 105                                  .map(this::split)
 106                                  .collect(Collectors.toSet());
 107         assertEquals(lines, edges);
 108     }
 109 
 110     @Test(dataProvider = "specVersion")
 111     public void testAPIOnly(String name, Set<String> edges) throws Exception {
 112         String[] options = new String[]{
 113             "-dotoutput", SPEC_DIR.toString(),
 114             "-s", "-apionly",
 115             "-m", name
 116         };
 117         assertTrue(JDEPS.run(System.out, System.out, options) == 0);
 118 
 119         Path path = SPEC_DIR.resolve(name + ".dot");
 120         assertTrue(Files.exists(path));
 121         Set<String> lines = Files.readAllLines(path).stream()
 122                                  .filter(l -> l.contains(" -> "))
 123                                  .map(this::split)
 124                                  .collect(Collectors.toSet());
 125         assertEquals(lines, edges);
 126     }
 127 
 128     static Pattern PATTERN = Pattern.compile(" *\"(\\S+)\" -> \"(\\S+)\" .*");
 129     String split(String line) {
 130         Matcher pm = PATTERN.matcher(line);
 131         assertTrue(pm.find());
 132         return String.format("%s -> %s", pm.group(1), pm.group(2));
 133     }
 134 }