Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/jdeps/Basic.java
32285 views
/*1* Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8003562 8005428 8015912 8027481 8048063 806893726* @summary Basic tests for jdeps tool27* @build Test p.Foo p.Bar p.C p.SubClass q.Gee javax.activity.NotCompactProfile28* @run main Basic29*/3031import java.io.File;32import java.io.IOException;33import java.io.PrintWriter;34import java.io.StringWriter;35import java.nio.file.Files;36import java.nio.file.Path;37import java.nio.file.Paths;38import java.util.*;39import java.util.regex.*;40import static java.nio.file.StandardCopyOption.*;4142public class Basic {43private static boolean symbolFileExist = initProfiles();44private static boolean initProfiles() {45// check if ct.sym exists; if not use the profiles.properties file46Path home = Paths.get(System.getProperty("java.home"));47if (home.endsWith("jre")) {48home = home.getParent();49}50Path ctsym = home.resolve("lib").resolve("ct.sym");51boolean symbolExists = ctsym.toFile().exists();52if (!symbolExists) {53Path testSrcProfiles =54Paths.get(System.getProperty("test.src", "."), "profiles.properties");55if (!testSrcProfiles.toFile().exists())56throw new Error(testSrcProfiles + " does not exist");57System.out.format("%s doesn't exist.%nUse %s to initialize profiles info%n",58ctsym, testSrcProfiles);59System.setProperty("jdeps.profiles", testSrcProfiles.toString());60}61return symbolExists;62}6364public static void main(String... args) throws Exception {65int errors = 0;66errors += new Basic().run();67if (errors > 0)68throw new Exception(errors + " errors found");69}7071int run() throws IOException {72File testDir = new File(System.getProperty("test.classes", "."));73// test a .class file74test(new File(testDir, "Test.class"),75new String[] {"java.lang", "p"},76new String[] {"compact1", "not found"});77// test a directory78// also test non-SE javax.activity class dependency79test(new File(testDir, "p"),80new String[] {"java.lang", "java.util", "java.lang.management", "javax.activity", "javax.crypto"},81new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1"},82new String[] {"-classpath", testDir.getPath()});83// test class-level dependency output84test(new File(testDir, "Test.class"),85new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},86new String[] {"compact1", "compact1", "not found", "not found"},87new String[] {"-verbose:class"});88// test -filter:none option89test(new File(testDir, "p"),90new String[] {"java.lang", "java.util", "java.lang.management", "javax.activity", "javax.crypto", "p"},91new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1", "p"},92new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:none"});93// test -filter:archive option94test(new File(testDir, "p"),95new String[] {"java.lang", "java.util", "java.lang.management", "javax.activity", "javax.crypto"},96new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1"},97new String[] {"-classpath", testDir.getPath(), "-verbose:package", "-filter:archive"});98// test -p option99test(new File(testDir, "Test.class"),100new String[] {"p.Foo", "p.Bar"},101new String[] {"not found", "not found"},102new String[] {"-verbose:class", "-p", "p"});103// test -e option104test(new File(testDir, "Test.class"),105new String[] {"p.Foo", "p.Bar"},106new String[] {"not found", "not found"},107new String[] {"-verbose:class", "-e", "p\\..*"});108test(new File(testDir, "Test.class"),109new String[] {"java.lang"},110new String[] {"compact1"},111new String[] {"-verbose:package", "-e", "java\\.lang\\..*"});112113// parse p.C, p.SubClass and q.*114// p.SubClass have no dependency other than p.C115// q.Gee depends on p.SubClass that should be found116test(testDir,117new String[] {"java.lang", "p"},118new String[] {"compact1", testDir.getName()},119new String[] {"-include", "p.C|p.SubClass|q\\..*"});120test(testDir,121new String[] {"java.lang", "p"},122new String[] {"compact1", testDir.getName()},123new String[] {"-classpath", testDir.getPath(), "-include", "p.C|p.SubClass|q\\..*"});124125126// test -classpath and -include options127test(null,128new String[] {"java.lang", "java.util", "java.lang.management",129"javax.activity", "javax.crypto"},130new String[] {"compact1", "compact1", "compact3", testDir.getName(), "compact1"},131new String[] {"-classpath", testDir.getPath(), "-include", "p.+|Test.class"});132test(new File(testDir, "Test.class"),133new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},134new String[] {"compact1", "compact1", testDir.getName(), testDir.getName()},135new String[] {"-v", "-classpath", testDir.getPath(), "Test.class"});136137// split package p - move p/Foo.class to dir1 and p/Bar.class to dir2138Path testClassPath = testDir.toPath();139Path dirP = testClassPath.resolve("p");140Path dir1 = testClassPath.resolve("dir1");141Path subdir1P = dir1.resolve("p");142Path dir2 = testClassPath.resolve("dir2");143Path subdir2P = dir2.resolve("p");144if (!Files.exists(subdir1P))145Files.createDirectories(subdir1P);146if (!Files.exists(subdir2P))147Files.createDirectories(subdir2P);148Files.move(dirP.resolve("Foo.class"), subdir1P.resolve("Foo.class"), REPLACE_EXISTING);149Files.move(dirP.resolve("Bar.class"), subdir2P.resolve("Bar.class"), REPLACE_EXISTING);150StringBuilder cpath = new StringBuilder(testDir.toString());151cpath.append(File.pathSeparator).append(dir1.toString());152cpath.append(File.pathSeparator).append(dir2.toString());153test(new File(testDir, "Test.class"),154new String[] {"java.lang.Object", "java.lang.String", "p.Foo", "p.Bar"},155new String[] {"compact1", "compact1", dir1.toFile().getName(), dir2.toFile().getName()},156new String[] {"-v", "-classpath", cpath.toString(), "Test.class"});157return errors;158}159160void test(File file, String[] expect, String[] profiles) {161test(file, expect, profiles, new String[0]);162}163164void test(File file, String[] expect, String[] profiles, String[] options) {165List<String> args = new ArrayList<>(Arrays.asList(options));166if (file != null) {167args.add(file.getPath());168}169List<String> argsWithDashP = new ArrayList<>();170argsWithDashP.add("-P");171argsWithDashP.addAll(args);172// test without -P173checkResult("dependencies", expect, jdeps(args.toArray(new String[0])).keySet());174// test with -P175checkResult("profiles", expect, profiles, jdeps(argsWithDashP.toArray(new String[0])));176}177178Map<String,String> jdeps(String... args) {179StringWriter sw = new StringWriter();180PrintWriter pw = new PrintWriter(sw);181System.err.println("jdeps " + Arrays.toString(args));182int rc = com.sun.tools.jdeps.Main.run(args, pw);183pw.close();184String out = sw.toString();185if (!out.isEmpty())186System.err.println(out);187if (rc != 0)188throw new Error("jdeps failed: rc=" + rc);189return findDeps(out);190}191192// Pattern used to parse lines193private static Pattern linePattern = Pattern.compile(".*\r?\n");194private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)");195196// Use the linePattern to break the given String into lines, applying197// the pattern to each line to see if we have a match198private static Map<String,String> findDeps(String out) {199Map<String,String> result = new LinkedHashMap<>();200Matcher lm = linePattern.matcher(out); // Line matcher201Matcher pm = null; // Pattern matcher202int lines = 0;203while (lm.find()) {204lines++;205CharSequence cs = lm.group(); // The current line206if (pm == null)207pm = pattern.matcher(cs);208else209pm.reset(cs);210if (pm.find())211result.put(pm.group(1), pm.group(2).trim());212if (lm.end() == out.length())213break;214}215return result;216}217218void checkResult(String label, String[] expect, Collection<String> found) {219List<String> list = Arrays.asList(expect);220if (!isEqual(list, found))221error("Unexpected " + label + " found: '" + found + "', expected: '" + list + "'");222}223224void checkResult(String label, String[] expect, String[] profiles, Map<String,String> result) {225if (expect.length != profiles.length)226error("Invalid expected names and profiles");227228// check the dependencies229checkResult(label, expect, result.keySet());230// check profile information231checkResult(label, profiles, result.values());232for (int i=0; i < expect.length; i++) {233String profile = result.get(expect[i]);234if (!profile.equals(profiles[i]))235error("Unexpected profile: '" + profile + "', expected: '" + profiles[i] + "'");236}237}238239boolean isEqual(List<String> expected, Collection<String> found) {240if (expected.size() != found.size())241return false;242243List<String> list = new ArrayList<>(found);244list.removeAll(expected);245return list.isEmpty();246}247248void error(String msg) {249System.err.println("Error: " + msg);250errors++;251}252253int errors;254}255256257