1 /* 2 * Copyright (c) 2012, 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 8003562 27 * @summary Basic tests for jdeps tool 28 * @build Test p.Foo 29 * @run main Basic 30 */ 31 32 import java.io.File; 33 import java.io.IOException; 34 import java.io.PrintWriter; 35 import java.io.StringWriter; 36 import java.util.*; 37 import java.util.regex.*; 38 39 public class Basic { 40 public static void main(String... args) throws Exception { 41 int errors = 0; 42 43 errors += new Basic().run(); 44 45 if (errors > 0) 46 throw new Exception(errors + " errors found"); 47 } 48 49 Basic() { 50 } 51 52 int run() throws IOException { 53 File testDir = new File(System.getProperty("test.classes", ".")); 54 test(new File(testDir, "Test.class"), 55 new String[] {"java.lang", "p"}); 56 test(new File(testDir, "p"), 57 new String[] {"java.lang", "java.util"}); 58 return errors; 59 } 60 61 void test(File file, String[] expect) { 62 String[] deps = jdeps(file); 63 checkEqual("dependencies", expect, deps); 64 } 65 66 String[] jdeps(File file) { 67 StringWriter sw = new StringWriter(); 68 PrintWriter pw = new PrintWriter(sw); 69 String[] args = { file.getPath() }; 70 System.err.println("jdeps " + Arrays.toString(args)); 71 int rc = com.sun.tools.jdeps.Main.run(args, pw); 72 pw.close(); 73 String out = sw.toString(); 74 if (!out.isEmpty()) 75 System.err.println(out); 76 if (rc != 0) 77 throw new Error("jdeps failed: rc=" + rc); 78 return findDeps(out); 79 } 80 81 // Pattern used to parse lines 82 private static Pattern linePattern 83 = Pattern.compile(".*\r?\n"); 84 private static Pattern pattern 85 = Pattern.compile(" +-> (\\S+) +(.*)"); 86 87 // Use the linePattern to break the given String into lines, applying 88 // the pattern to each line to see if we have a match 89 private static String[] findDeps(String out) { 90 List<String> result = new ArrayList<>(); 91 Matcher lm = linePattern.matcher(out); // Line matcher 92 Matcher pm = null; // Pattern matcher 93 int lines = 0; 94 while (lm.find()) { 95 lines++; 96 CharSequence cs = lm.group(); // The current line 97 if (pm == null) 98 pm = pattern.matcher(cs); 99 else 100 pm.reset(cs); 101 if (pm.find()) 102 result.add(pm.group(1)); 103 if (lm.end() == out.length()) 104 break; 105 } 106 return result.toArray(new String[0]); 107 } 108 109 void checkEqual(String label, String[] expect, String[] found) { 110 Set<String> s1 = new HashSet<>(Arrays.asList(expect)); 111 Set<String> s2 = new HashSet<>(Arrays.asList(found)); 112 113 if (!s1.equals(s2)) 114 error("Unexpected " + label + " found: '" + s2 + "', expected: '" + s1 + "'"); 115 } 116 117 void error(String msg) { 118 System.err.println("Error: " + msg); 119 errors++; 120 } 121 122 int errors; 123 }