Basic.java revision 2942:08092deced3f
1114472Sru/* 2146515Sru * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 321495Sjmacd * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4114472Sru * 5146515Sru * This code is free software; you can redistribute it and/or modify it 621495Sjmacd * under the terms of the GNU General Public License version 2 only, as 721495Sjmacd * published by the Free Software Foundation. 821495Sjmacd * 921495Sjmacd * This code is distributed in the hope that it will be useful, but WITHOUT 1021495Sjmacd * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1121495Sjmacd * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1221495Sjmacd * version 2 for more details (a copy is included in the LICENSE file that 1321495Sjmacd * accompanied this code). 1421495Sjmacd * 1521495Sjmacd * You should have received a copy of the GNU General Public License version 1621495Sjmacd * 2 along with this work; if not, write to the Free Software Foundation, 1721495Sjmacd * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1821495Sjmacd * 1921495Sjmacd * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2021495Sjmacd * or visit www.oracle.com if you need additional information or have any 2142660Smarkm * questions. 2242660Smarkm */ 2321495Sjmacd 2456160Sru/* 2556160Sru * @test 2621495Sjmacd * @bug 8003562 8005428 8015912 8027481 8048063 8068937 2721495Sjmacd * @summary Basic tests for jdeps tool 2821495Sjmacd * @modules java.management 2921495Sjmacd * jdk.jdeps/com.sun.tools.jdeps 3021495Sjmacd * @build Test p.Foo p.Bar p.C p.SubClass q.Gee javax.activity.NotCompactProfile 3121495Sjmacd * @run main Basic 3221495Sjmacd */ 3321495Sjmacd 3421495Sjmacdimport java.io.File; 3521495Sjmacdimport java.io.IOException; 3621495Sjmacdimport java.io.PrintWriter; 3721495Sjmacdimport java.io.StringWriter; 3821495Sjmacdimport java.nio.file.Files; 3921495Sjmacdimport java.nio.file.Path; 40146515Sruimport java.nio.file.Paths; 4121495Sjmacdimport java.util.*; 4221495Sjmacdimport java.util.regex.*; 4321495Sjmacdimport static java.nio.file.StandardCopyOption.*; 4421495Sjmacd 4521495Sjmacdpublic class Basic { 4621495Sjmacd public static void main(String... args) throws Exception { 4721495Sjmacd int errors = 0; 48146515Sru errors += new Basic().run(); 49146515Sru if (errors > 0) 5021495Sjmacd throw new Exception(errors + " errors found"); 5121495Sjmacd } 5221495Sjmacd 5321495Sjmacd int run() throws IOException { 5442660Smarkm File testDir = new File(System.getProperty("test.classes", ".")); 5521495Sjmacd // test a .class file 5642660Smarkm test(new File(testDir, "Test.class"), 5742660Smarkm new String[] {"java.lang", "p"}, 5821495Sjmacd new String[] {"compact1", "not found"}); 5942660Smarkm // test a directory 6021495Sjmacd // also test non-SE javax.activity class dependency 6121495Sjmacd test(new File(testDir, "p"), 6221495Sjmacd new String[] {"java.lang", "java.util", "java.lang.management", "javax.activity", "javax.crypto"}, 6321495Sjmacd new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1"}, 6421495Sjmacd new String[] {"-classpath", testDir.getPath()}); 6542660Smarkm // test class-level dependency output 6642660Smarkm test(new File(testDir, "Test.class"), 6742660Smarkm new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"}, 6842660Smarkm new String[] {"compact1", "compact1", "not found", "not found"}, 6942660Smarkm new String[] {"-verbose:class"}); 7042660Smarkm // test -filter:none option 7142660Smarkm test(new File(testDir, "p"), 7242660Smarkm new String[] {"java.lang", "java.util", "java.lang.management", "javax.activity", "javax.crypto", "p"}, 7342660Smarkm new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1", "p"}, 7442660Smarkm new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:none"}); 7521495Sjmacd // test -filter:archive option 7621495Sjmacd test(new File(testDir, "p"), 7721495Sjmacd new String[] {"java.lang", "java.util", "java.lang.management", "javax.activity", "javax.crypto"}, 7821495Sjmacd new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1"}, 7921495Sjmacd new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:archive"}); 8021495Sjmacd // test -p option 8121495Sjmacd test(new File(testDir, "Test.class"), 8221495Sjmacd new String[] {"p.Foo", "p.Bar"}, 8321495Sjmacd new String[] {"not found", "not found"}, 8421495Sjmacd new String[] {"-verbose:class", "-p", "p"}); 8521495Sjmacd // test -e option 8621495Sjmacd test(new File(testDir, "Test.class"), 8721495Sjmacd new String[] {"p.Foo", "p.Bar"}, 8821495Sjmacd new String[] {"not found", "not found"}, 8921495Sjmacd new String[] {"-verbose:class", "-e", "p\\..*"}); 9021495Sjmacd test(new File(testDir, "Test.class"), 9121495Sjmacd new String[] {"java.lang"}, 9221495Sjmacd new String[] {"compact1"}, 9321495Sjmacd new String[] {"-verbose:package", "-e", "java\\.lang\\..*"}); 9421495Sjmacd 9521495Sjmacd // parse p.C, p.SubClass and q.* 9621495Sjmacd // p.SubClass have no dependency other than p.C 9721495Sjmacd // q.Gee depends on p.SubClass that should be found 9821495Sjmacd test(testDir, 9921495Sjmacd new String[] {"java.lang", "p"}, 10021495Sjmacd new String[] {"compact1", testDir.getName()}, 10121495Sjmacd new String[] {"-include", "p.C|p.SubClass|q\\..*"}); 10221495Sjmacd test(testDir, 10321495Sjmacd new String[] {"java.lang", "p"}, 10421495Sjmacd new String[] {"compact1", testDir.getName()}, 10521495Sjmacd new String[] {"-classpath", testDir.getPath(), "-include", "p.C|p.SubClass|q\\..*"}); 10621495Sjmacd 10721495Sjmacd // test -classpath and -include options 10821495Sjmacd test(null, 10921495Sjmacd new String[] {"java.lang", "java.util", "java.lang.management", 11021495Sjmacd "javax.activity", "javax.crypto"}, 11121495Sjmacd new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1"}, 11221495Sjmacd new String[] {"-classpath", testDir.getPath(), "-include", "p.+|Test.class"}); 113114472Sru test(new File(testDir, "Test.class"), 114114472Sru new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"}, 115114472Sru new String[] {"compact1", "compact1", testDir.getName(), testDir.getName()}, 116114472Sru new String[] {"-v", "-classpath", testDir.getPath(), "Test.class"}); 117114472Sru 118114472Sru // split package p - move p/Foo.class to dir1 and p/Bar.class to dir2 119114472Sru Path testClassPath = testDir.toPath(); 12021495Sjmacd Path dirP = testClassPath.resolve("p"); 12121495Sjmacd Path dir1 = testClassPath.resolve("dir1"); 12221495Sjmacd Path subdir1P = dir1.resolve("p"); 12321495Sjmacd Path dir2 = testClassPath.resolve("dir2"); 12421495Sjmacd Path subdir2P = dir2.resolve("p"); 12521495Sjmacd if (!Files.exists(subdir1P)) 126146515Sru Files.createDirectories(subdir1P); 127146515Sru if (!Files.exists(subdir2P)) 128146515Sru Files.createDirectories(subdir2P); 129146515Sru Files.move(dirP.resolve("Foo.class"), subdir1P.resolve("Foo.class"), REPLACE_EXISTING); 130146515Sru Files.move(dirP.resolve("Bar.class"), subdir2P.resolve("Bar.class"), REPLACE_EXISTING); 131146515Sru StringBuilder cpath = new StringBuilder(testDir.toString()); 132146515Sru cpath.append(File.pathSeparator).append(dir1.toString()); 133146515Sru cpath.append(File.pathSeparator).append(dir2.toString()); 134146515Sru test(new File(testDir, "Test.class"), 135146515Sru new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"}, 136146515Sru new String[] {"compact1", "compact1", dir1.toFile().getName(), dir2.toFile().getName()}, 137146515Sru new String[] {"-v", "-classpath", cpath.toString(), "Test.class"}); 138146515Sru return errors; 139146515Sru } 140146515Sru 141146515Sru void test(File file, String[] expect, String[] profiles) { 142146515Sru test(file, expect, profiles, new String[0]); 143146515Sru } 144146515Sru 145146515Sru void test(File file, String[] expect, String[] profiles, String[] options) { 14621495Sjmacd List<String> args = new ArrayList<>(Arrays.asList(options)); 147146515Sru if (file != null) { 148146515Sru args.add(file.getPath()); 14921495Sjmacd } 15021495Sjmacd List<String> argsWithDashP = new ArrayList<>(); 15121495Sjmacd argsWithDashP.add("-P"); 15242660Smarkm argsWithDashP.addAll(args); 153146515Sru // test without -P 15421495Sjmacd checkResult("dependencies", expect, jdeps(args.toArray(new String[0])).keySet()); 15521495Sjmacd // test with -P 15621495Sjmacd checkResult("profiles", expect, profiles, jdeps(argsWithDashP.toArray(new String[0]))); 15721495Sjmacd } 15821495Sjmacd 15921495Sjmacd Map<String,String> jdeps(String... args) { 16042660Smarkm StringWriter sw = new StringWriter(); 16142660Smarkm PrintWriter pw = new PrintWriter(sw); 16242660Smarkm System.err.println("jdeps " + Arrays.toString(args)); 16342660Smarkm int rc = com.sun.tools.jdeps.Main.run(args, pw); 16442660Smarkm pw.close(); 16542660Smarkm String out = sw.toString(); 16642660Smarkm if (!out.isEmpty()) 16742660Smarkm System.err.println(out); 16842660Smarkm if (rc != 0) 169114472Sru throw new Error("jdeps failed: rc=" + rc); 170114472Sru return findDeps(out); 171116525Sru } 17221495Sjmacd 17321495Sjmacd // Pattern used to parse lines 17421495Sjmacd private static Pattern linePattern = Pattern.compile(".*\r?\n"); 17521495Sjmacd private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)"); 17621495Sjmacd 17721495Sjmacd // Use the linePattern to break the given String into lines, applying 17821495Sjmacd // the pattern to each line to see if we have a match 17921495Sjmacd private static Map<String,String> findDeps(String out) { 18021495Sjmacd Map<String,String> result = new LinkedHashMap<>(); 18121495Sjmacd Matcher lm = linePattern.matcher(out); // Line matcher 18221495Sjmacd Matcher pm = null; // Pattern matcher 18321495Sjmacd int lines = 0; 18421495Sjmacd while (lm.find()) { 18521495Sjmacd lines++; 18621495Sjmacd CharSequence cs = lm.group(); // The current line 18721495Sjmacd if (pm == null) 18821495Sjmacd pm = pattern.matcher(cs); 18921495Sjmacd else 19021495Sjmacd pm.reset(cs); 19121495Sjmacd if (pm.find()) 19221495Sjmacd result.put(pm.group(1), pm.group(2).trim()); 19321495Sjmacd if (lm.end() == out.length()) 19421495Sjmacd break; 19521495Sjmacd } 19621495Sjmacd return result; 19721495Sjmacd } 19856160Sru 19921495Sjmacd void checkResult(String label, String[] expect, Collection<String> found) { 20056160Sru List<String> list = Arrays.asList(expect); 20121495Sjmacd if (!isEqual(list, found)) 20221495Sjmacd error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'"); 20321495Sjmacd } 20442660Smarkm 20556160Sru void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) { 20656160Sru if (expect.length != profiles.length) 20756160Sru error("Invalid expected names and profiles"); 20856160Sru 20956160Sru // check the dependencies 21056160Sru checkResult(label, expect, result.keySet()); 21156160Sru // check profile information 21256160Sru checkResult(label, profiles, result.values()); 21356160Sru for (int i=0; i < expect.length; i++) { 21456160Sru String profile = result.get(expect[i]); 21556160Sru if (!profile.equals(profiles[i])) 21621495Sjmacd error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'"); 21756160Sru } 21821495Sjmacd } 21921495Sjmacd 22021495Sjmacd boolean isEqual(List<String> expected, Collection<String> found) { 22121495Sjmacd if (expected.size() != found.size()) 22221495Sjmacd return false; 223116525Sru 22421495Sjmacd List<String> list = new ArrayList<>(found); 225114472Sru list.removeAll(expected); 226114472Sru return list.isEmpty(); 227114472Sru } 22821495Sjmacd 22942660Smarkm void error(String msg) { 230114472Sru System.err.println("Error: " + msg); 23121495Sjmacd errors++; 23242660Smarkm } 23321495Sjmacd 23421495Sjmacd int errors; 23521495Sjmacd} 23656160Sru