1 /*
   2  * Copyright (c) 2014, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @bug 8056258 8048609
  29  * @summary Ensures that the DependencyCollector covers various cases.
  30  * @library /tools/lib
  31  * @build Wrapper ToolBox
  32  * @run main Wrapper DependencyCollection
  33  */
  34 
  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.io.PrintWriter;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.util.Arrays;
  41 import java.util.Comparator;
  42 import java.util.HashSet;
  43 import java.util.List;
  44 import java.util.Set;
  45 import java.util.regex.Matcher;
  46 import java.util.regex.Pattern;
  47 import java.util.stream.Collectors;
  48 
  49 import javax.tools.StandardJavaFileManager;
  50 
  51 import com.sun.tools.javac.api.JavacTaskImpl;
  52 import com.sun.tools.javac.api.JavacTool;
  53 import com.sun.tools.javac.code.Symbol;
  54 import com.sun.tools.javac.code.Symbol.ClassSymbol;
  55 import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
  56 import com.sun.tools.javac.util.Assert;
  57 import com.sun.tools.javac.util.Context;
  58 import com.sun.tools.sjavac.comp.SmartFileManager;
  59 import com.sun.tools.sjavac.comp.dependencies.DependencyCollector;
  60 
  61 public class DependencyCollection {
  62 
  63     public static void main(String[] args) throws IOException {
  64         Path src = Paths.get(ToolBox.testSrc, "test-input", "src");
  65         Context context = new Context();
  66         JavacTool javac = JavacTool.create();
  67         try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {
  68             SmartFileManager smartFileManager = new SmartFileManager(fm);
  69             smartFileManager.setSymbolFileEnabled(false);
  70             List<File> srcList = Arrays.asList(src.resolve("pkg/Test.java").toFile());
  71             JavacTaskImpl task = (JavacTaskImpl) javac.getTask(
  72                     new PrintWriter(System.out),
  73                     smartFileManager,
  74                     null,
  75                     Arrays.asList("-d", "classes",
  76                                   "-sourcepath", src.toAbsolutePath().toString()),
  77                     null,
  78                     fm.getJavaFileObjectsFromFiles(srcList),
  79                     context);
  80             DependencyCollector dc = new DependencyCollector(context);
  81             task.addTaskListener(dc);
  82             task.doCall();
  83 
  84             // Find Tset.java compilation unit
  85             JCCompilationUnit testCU = null;
  86             for (JCCompilationUnit cu : dc.collectedDependencies.keySet()) {
  87                 if (cu.getSourceFile().getName().endsWith("/Test.java"))
  88                     testCU = cu;
  89             }
  90             Assert.check(testCU != null, "Could not find Test.java compilation unit.");
  91             Set<ClassSymbol> foundDependencies = dc.collectedDependencies.get(testCU).get("pkg.Test");
  92 
  93             // Print dependencies
  94             foundDependencies.stream()
  95                              .sorted(Comparator.comparing(DependencyCollection::extractNumber))
  96                              .forEach(p -> System.out.println("    " + p));
  97 
  98             // Check result
  99             Set<Integer> found = foundDependencies.stream()
 100                                                   .map(DependencyCollection::extractNumber)
 101                                                   .collect(Collectors.toSet());
 102             found.remove(-1); // Dependencies with no number (java.lang etc)
 103             Set<Integer> expected = new HashSet<>();
 104             for (int i = 2; i <= 32; i++) {
 105                 if (i == 15)   // Case 15 correspond to the type of a
 106                     continue;  // throw-away return value.
 107                 expected.add(i);
 108             }
 109 
 110             Set<Integer> missing = new HashSet<>(expected);
 111             missing.removeAll(found);
 112             if (missing.size() > 0) {
 113                 System.out.println("Missing dependencies:");
 114                 missing.forEach(i -> System.out.println("    Dependency " + i));
 115             }
 116 
 117             Set<Integer> unexpected = new HashSet<>(found);
 118             unexpected.removeAll(expected);
 119             if (unexpected.size() > 0) {
 120                 System.out.println("Unexpected dependencies found:");
 121                 unexpected.forEach(i -> System.out.println("    Dependency " + i));
 122             }
 123 
 124             if (missing.size() > 0 || unexpected.size() > 0)
 125                 throw new AssertionError("Missing and/or unexpected dependencies found.");
 126 
 127             System.out.println("Test passed");
 128         }
 129     }
 130 
 131     public static int extractNumber(Symbol p) {
 132         Matcher m = Pattern.compile("\\d+").matcher(p.name.toString());
 133         if (!m.find())
 134             return -1;
 135         return Integer.parseInt(m.group());
 136     }
 137 }