Mon, 14 Jan 2013 19:52:36 +0100
7193719: Support repeating annotations in javax.lang.model
Reviewed-by: jjg
mchung@1472 | 1 | /* |
mchung@1472 | 2 | * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
mchung@1472 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mchung@1472 | 4 | * |
mchung@1472 | 5 | * This code is free software; you can redistribute it and/or modify it |
mchung@1472 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mchung@1472 | 7 | * published by the Free Software Foundation. |
mchung@1472 | 8 | * |
mchung@1472 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mchung@1472 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mchung@1472 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mchung@1472 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mchung@1472 | 13 | * accompanied this code). |
mchung@1472 | 14 | * |
mchung@1472 | 15 | * You should have received a copy of the GNU General Public License version |
mchung@1472 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mchung@1472 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mchung@1472 | 18 | * |
mchung@1472 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mchung@1472 | 20 | * or visit www.oracle.com if you need additional information or have any |
mchung@1472 | 21 | * questions. |
mchung@1472 | 22 | */ |
mchung@1472 | 23 | |
mchung@1472 | 24 | /* |
mchung@1472 | 25 | * @test |
mchung@1472 | 26 | * @bug 8003562 |
mchung@1472 | 27 | * @summary Basic tests for jdeps tool |
mchung@1472 | 28 | * @build Test p.Foo |
mchung@1472 | 29 | * @run main Basic |
mchung@1472 | 30 | */ |
mchung@1472 | 31 | |
mchung@1472 | 32 | import java.io.File; |
mchung@1472 | 33 | import java.io.IOException; |
mchung@1472 | 34 | import java.io.PrintWriter; |
mchung@1472 | 35 | import java.io.StringWriter; |
mchung@1472 | 36 | import java.util.*; |
mchung@1472 | 37 | import java.util.regex.*; |
mchung@1472 | 38 | |
mchung@1472 | 39 | public class Basic { |
mchung@1472 | 40 | public static void main(String... args) throws Exception { |
mchung@1472 | 41 | int errors = 0; |
mchung@1472 | 42 | |
mchung@1472 | 43 | errors += new Basic().run(); |
mchung@1472 | 44 | if (errors > 0) |
mchung@1472 | 45 | throw new Exception(errors + " errors found"); |
mchung@1472 | 46 | } |
mchung@1472 | 47 | |
mchung@1472 | 48 | int run() throws IOException { |
mchung@1472 | 49 | File testDir = new File(System.getProperty("test.classes", ".")); |
mchung@1472 | 50 | // test a .class file |
mchung@1472 | 51 | test(new File(testDir, "Test.class"), |
mchung@1472 | 52 | new String[] {"java.lang", "p"}); |
mchung@1472 | 53 | // test a directory |
mchung@1472 | 54 | test(new File(testDir, "p"), |
mchung@1472 | 55 | new String[] {"java.lang", "java.util"}); |
mchung@1472 | 56 | // test class-level dependency output |
mchung@1472 | 57 | test(new File(testDir, "Test.class"), |
mchung@1472 | 58 | new String[] {"java.lang.Object", "p.Foo"}, |
mchung@1472 | 59 | new String[] {"-V", "class"}); |
mchung@1472 | 60 | // test -p option |
mchung@1472 | 61 | test(new File(testDir, "Test.class"), |
mchung@1472 | 62 | new String[] {"p.Foo"}, |
mchung@1472 | 63 | new String[] {"--verbose-level=class", "-p", "p"}); |
mchung@1472 | 64 | // test -e option |
mchung@1472 | 65 | test(new File(testDir, "Test.class"), |
mchung@1472 | 66 | new String[] {"p.Foo"}, |
mchung@1472 | 67 | new String[] {"-V", "class", "-e", "p\\..*"}); |
mchung@1472 | 68 | test(new File(testDir, "Test.class"), |
mchung@1472 | 69 | new String[] {"java.lang"}, |
mchung@1472 | 70 | new String[] {"-V", "package", "-e", "java\\.lang\\..*"}); |
mchung@1472 | 71 | // test -classpath and -all options |
mchung@1472 | 72 | test(null, |
mchung@1472 | 73 | new String[] {"com.sun.tools.jdeps", "java.lang", "java.util", |
mchung@1472 | 74 | "java.util.regex", "java.io", "p"}, |
mchung@1472 | 75 | new String[] {"--classpath", testDir.getPath(), "*"}); |
mchung@1472 | 76 | return errors; |
mchung@1472 | 77 | } |
mchung@1472 | 78 | |
mchung@1472 | 79 | void test(File file, String[] expect) { |
mchung@1472 | 80 | test(file, expect, new String[0]); |
mchung@1472 | 81 | } |
mchung@1472 | 82 | |
mchung@1472 | 83 | void test(File file, String[] expect, String[] options) { |
mchung@1472 | 84 | String[] args; |
mchung@1472 | 85 | if (file != null) { |
mchung@1472 | 86 | args = Arrays.copyOf(options, options.length+1); |
mchung@1472 | 87 | args[options.length] = file.getPath(); |
mchung@1472 | 88 | } else { |
mchung@1472 | 89 | args = options; |
mchung@1472 | 90 | } |
mchung@1472 | 91 | String[] deps = jdeps(args); |
mchung@1472 | 92 | checkEqual("dependencies", expect, deps); |
mchung@1472 | 93 | } |
mchung@1472 | 94 | |
mchung@1472 | 95 | String[] jdeps(String... args) { |
mchung@1472 | 96 | StringWriter sw = new StringWriter(); |
mchung@1472 | 97 | PrintWriter pw = new PrintWriter(sw); |
mchung@1472 | 98 | System.err.println("jdeps " + Arrays.toString(args)); |
mchung@1472 | 99 | int rc = com.sun.tools.jdeps.Main.run(args, pw); |
mchung@1472 | 100 | pw.close(); |
mchung@1472 | 101 | String out = sw.toString(); |
mchung@1472 | 102 | if (!out.isEmpty()) |
mchung@1472 | 103 | System.err.println(out); |
mchung@1472 | 104 | if (rc != 0) |
mchung@1472 | 105 | throw new Error("jdeps failed: rc=" + rc); |
mchung@1472 | 106 | return findDeps(out); |
mchung@1472 | 107 | } |
mchung@1472 | 108 | |
mchung@1472 | 109 | // Pattern used to parse lines |
mchung@1472 | 110 | private static Pattern linePattern = Pattern.compile(".*\r?\n"); |
mchung@1472 | 111 | private static Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +.*"); |
mchung@1472 | 112 | |
mchung@1472 | 113 | // Use the linePattern to break the given String into lines, applying |
mchung@1472 | 114 | // the pattern to each line to see if we have a match |
mchung@1472 | 115 | private static String[] findDeps(String out) { |
mchung@1472 | 116 | List<String> result = new ArrayList<>(); |
mchung@1472 | 117 | Matcher lm = linePattern.matcher(out); // Line matcher |
mchung@1472 | 118 | Matcher pm = null; // Pattern matcher |
mchung@1472 | 119 | int lines = 0; |
mchung@1472 | 120 | while (lm.find()) { |
mchung@1472 | 121 | lines++; |
mchung@1472 | 122 | CharSequence cs = lm.group(); // The current line |
mchung@1472 | 123 | if (pm == null) |
mchung@1472 | 124 | pm = pattern.matcher(cs); |
mchung@1472 | 125 | else |
mchung@1472 | 126 | pm.reset(cs); |
mchung@1472 | 127 | if (pm.find()) |
mchung@1472 | 128 | result.add(pm.group(1)); |
mchung@1472 | 129 | if (lm.end() == out.length()) |
mchung@1472 | 130 | break; |
mchung@1472 | 131 | } |
mchung@1472 | 132 | return result.toArray(new String[0]); |
mchung@1472 | 133 | } |
mchung@1472 | 134 | |
mchung@1472 | 135 | void checkEqual(String label, String[] expect, String[] found) { |
mchung@1472 | 136 | Set<String> s1 = new HashSet<>(Arrays.asList(expect)); |
mchung@1472 | 137 | Set<String> s2 = new HashSet<>(Arrays.asList(found)); |
mchung@1472 | 138 | |
mchung@1472 | 139 | if (!s1.equals(s2)) |
mchung@1472 | 140 | error("Unexpected " + label + " found: '" + s2 + "', expected: '" + s1 + "'"); |
mchung@1472 | 141 | } |
mchung@1472 | 142 | |
mchung@1472 | 143 | void error(String msg) { |
mchung@1472 | 144 | System.err.println("Error: " + msg); |
mchung@1472 | 145 | errors++; |
mchung@1472 | 146 | } |
mchung@1472 | 147 | |
mchung@1472 | 148 | int errors; |
mchung@1472 | 149 | } |