File: JdepsDependencyClosure.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 65,172 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (498 lines) | stat: -rw-r--r-- 22,049 bytes parent folder | download | duplicates (20)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @test
 * @bug 8080608
 * @summary Test that jdeps verbose output has a summary line when dependencies
 *          are found within the same archive. For each testcase, compare the
 *          result obtained from jdeps with the expected result.
 * @modules jdk.jdeps/com.sun.tools.jdeps
 *          java.base/sun.security.x509
 * @build use.indirect.DontUseJdkInternal2
 * @build use.indirect.UseJdkInternalIndirectly
 * @build use.indirect2.DontUseJdkInternal3
 * @build use.indirect2.UseJdkInternalIndirectly2
 * @build use.internal.DontUseJdkInternal
 * @build use.internal.UseClassWithJdkInternal
 * @build use.internal.UseJdkInternalClass
 * @build use.internal.UseJdkInternalClass2
 * @run main JdepsDependencyClosure --test:0
 * @run main JdepsDependencyClosure --test:1
 * @run main JdepsDependencyClosure --test:2
 * @run main JdepsDependencyClosure --test:3
 */
public class JdepsDependencyClosure {

    static boolean VERBOSE = false;
    static boolean COMPARE_TEXT = true;

    static final String JDEPS_SUMMARY_TEXT_FORMAT = "%s -> %s%n";
    static final String JDEPS_VERBOSE_TEXT_FORMAT = "   %-50s -> %-50s %s%n";

    /**
     * Helper class used to store arguments to pass to
     * {@code JdepsDependencyClosure.test} as well as expected
     * results.
     */
    static class TestCaseData {
        final Map<String, Set<String>> expectedDependencies;
        final String expectedText;
        final String[] args;
        final boolean closure;

        TestCaseData(Map<String, Set<String>> expectedDependencies,
                        String expectedText,
                        boolean closure,
                        String[] args) {
            this.expectedDependencies = expectedDependencies;
            this.expectedText = expectedText;
            this.closure = closure;
            this.args = args;
        }

        public void test() {
            if (expectedDependencies != null) {
                String format = closure
                        ? "Running (closure): jdeps %s %s %s %s"
                        : "Running: jdeps %s %s %s %s";
                System.out.println(String.format(format, (Object[])args));
            }
            JdepsDependencyClosure.test(args, expectedDependencies, expectedText, closure);
        }

        /**
         * Make a new test case data to invoke jdeps and test its output.
         * @param pattern The pattern that will passed through to jdeps -e
         *                This is expected to match only one class.
         * @param arcPath The archive to analyze. A jar or a class directory.
         * @param classes For each reported archive dependency couple, the
         *                expected list of classes in the source that will
         *                be reported as having a dependency on the class
         *                in the target that matches the given pattern.
         * @param dependencies For each archive dependency couple, a singleton list
         *                containing the name of the class in the target that
         *                matches the pattern. It is expected that the pattern
         *                will match only one class in the target.
         *                If the pattern matches several classes the
         *                expected text may no longer match the jdeps output.
         * @param archives A list of archive dependency couple in the form
         *               {{sourceName1, sourcePath1, targetDescription1, targetPath1}
         *                {sourceName2, sourcePath2, targetDescription2, targetPath2}
         *                ... }
         *               For a JDK module - e.g. java.base, the targetDescription
         *               is usually something like "JDK internal API (java.base)"
         *               and the targetPath is usually the module name "java.base".
         * @param closure Whether jdeps should be recursively invoked to build
         *                the closure.
         * @return An instance of TestCaseData containing all the information
         *         needed to perform the jdeps invokation and test its output.
         */
        public static TestCaseData make(String pattern, String arcPath, String[][] classes,
                String[][] dependencies, String[][] archives, boolean closure) {
            final String[] args = new String[] {
                "-e", pattern, "-v", arcPath
            };
            Map<String, Set<String>> expected = new HashMap<>();
            String expectedText = "";
            for (int i=0; i<classes.length; i++) {
                final int index = i;
                expectedText += Stream.of(classes[i])
                    .map((cn) -> String.format(JDEPS_VERBOSE_TEXT_FORMAT, cn,
                            dependencies[index][0], archives[index][2]))
                    .reduce(String.format(JDEPS_SUMMARY_TEXT_FORMAT, archives[i][0],
                            archives[index][3]), (s1,s2) -> s1.concat(s2));
                for (String cn : classes[index]) {
                    expected.putIfAbsent(cn, new HashSet<>());
                    expected.get(cn).add(dependencies[index][0]);
                }
            }
            return new TestCaseData(expected, expectedText, closure, args);
        }

        public static TestCaseData valueOf(String[] args) {
            if (args.length == 1 && args[0].startsWith("--test:")) {
                // invoked from jtreg. build test case data for selected test.
                int index = Integer.parseInt(args[0].substring("--test:".length()));
                if (index >= dataSuppliers.size()) {
                    throw new RuntimeException("No such test case: " + index
                            + " - available testcases are [0.."
                            + (dataSuppliers.size()-1) + "]");
                }
                return dataSuppliers.get(index).get();
            } else {
                // invoked in standalone. just take the given argument
                // and perform no validation on the output (except that it
                // must start with a summary line)
                return new TestCaseData(null, null, true, args);
            }
        }

    }

    static TestCaseData makeTestCaseOne() {
        final String arcPath = System.getProperty("test.classes", "build/classes");
        final String arcName = Paths.get(arcPath).getFileName().toString();
        final String[][] classes = new String[][] {
            {"use.indirect2.UseJdkInternalIndirectly2", "use.internal.UseClassWithJdkInternal"},
        };
        final String[][] dependencies = new String[][] {
            {"use.internal.UseJdkInternalClass"},
        };
        final String[][] archives = new String[][] {
            {arcName, arcPath, arcName, arcPath},
        };
        return TestCaseData.make("use.internal.UseJdkInternalClass", arcPath, classes,
                dependencies, archives, false);
    }

    static TestCaseData makeTestCaseTwo() {
        String arcPath = System.getProperty("test.classes", "build/classes");
        String arcName = Paths.get(arcPath).getFileName().toString();
        String[][] classes = new String[][] {
            {"use.internal.UseJdkInternalClass", "use.internal.UseJdkInternalClass2"}
        };
        String[][] dependencies = new String[][] {
            {"sun.security.x509.X509CertInfo"}
        };
        String[][] archive = new String[][] {
            {arcName, arcPath, "JDK internal API (java.base)", "java.base"},
        };
        return TestCaseData.make("sun.security.x509.X509CertInfo", arcPath, classes,
                dependencies, archive, false);
    }

    static TestCaseData makeTestCaseThree() {
        final String arcPath = System.getProperty("test.classes", "build/classes");
        final String arcName = Paths.get(arcPath).getFileName().toString();
        final String[][] classes = new String[][] {
            {"use.indirect2.UseJdkInternalIndirectly2", "use.internal.UseClassWithJdkInternal"},
            {"use.indirect.UseJdkInternalIndirectly"}
        };
        final String[][] dependencies = new String[][] {
            {"use.internal.UseJdkInternalClass"},
            {"use.internal.UseClassWithJdkInternal"}
        };
        final String[][] archives = new String[][] {
            {arcName, arcPath, arcName, arcPath},
            {arcName, arcPath, arcName, arcPath}
        };
        return TestCaseData.make("use.internal.UseJdkInternalClass", arcPath, classes,
                dependencies, archives, true);
    }


    static TestCaseData makeTestCaseFour() {
        final String arcPath = System.getProperty("test.classes", "build/classes");
        final String arcName = Paths.get(arcPath).getFileName().toString();
        final String[][] classes = new String[][] {
            {"use.internal.UseJdkInternalClass", "use.internal.UseJdkInternalClass2"},
            {"use.indirect2.UseJdkInternalIndirectly2", "use.internal.UseClassWithJdkInternal"},
            {"use.indirect.UseJdkInternalIndirectly"}
        };
        final String[][] dependencies = new String[][] {
            {"sun.security.x509.X509CertInfo"},
            {"use.internal.UseJdkInternalClass"},
            {"use.internal.UseClassWithJdkInternal"}
        };
        final String[][] archives = new String[][] {
            {arcName, arcPath, "JDK internal API (java.base)", "java.base"},
            {arcName, arcPath, arcName, arcPath},
            {arcName, arcPath, arcName, arcPath}
        };
        return TestCaseData.make("sun.security.x509.X509CertInfo", arcPath, classes, dependencies,
                archives, true);
    }

    static final List<Supplier<TestCaseData>> dataSuppliers = Arrays.asList(
        JdepsDependencyClosure::makeTestCaseOne,
        JdepsDependencyClosure::makeTestCaseTwo,
        JdepsDependencyClosure::makeTestCaseThree,
        JdepsDependencyClosure::makeTestCaseFour
    );



    /**
     * The OutputStreamParser is used to parse the format of jdeps.
     * It is thus dependent on that format.
     */
    static class OutputStreamParser extends OutputStream {
        // OutputStreamParser will populate this map:
        //
        // For each archive, a list of class in where dependencies where
        //     found...
        final Map<String, Set<String>> deps;
        final StringBuilder text = new StringBuilder();

        StringBuilder[] lines = { new StringBuilder(), new StringBuilder() };
        int line = 0;
        int sepi = 0;
        char[] sep;

        public OutputStreamParser(Map<String, Set<String>> deps) {
            this.deps = deps;
            this.sep = System.getProperty("line.separator").toCharArray();
        }

        @Override
        public void write(int b) throws IOException {
            lines[line].append((char)b);
            if (b == sep[sepi]) {
                if (++sepi == sep.length) {
                    text.append(lines[line]);
                    if (lines[0].toString().startsWith("  ")) {
                        throw new RuntimeException("Bad formatting: "
                                + "summary line missing for\n"+lines[0]);
                    }
                    // Usually the output looks like that:
                    // <archive-1> -> java.base
                    //   <class-1>      -> <dependency> <dependency description>
                    //   <class-2>      -> <dependency> <dependency description>
                    //   ...
                    // <archive-2> -> java.base
                    //   <class-3>      -> <dependency> <dependency description>
                    //   <class-4>      -> <dependency> <dependency description>
                    //   ...
                    //
                    // We want to keep the <archive> line in lines[0]
                    // and have the ith <class-i> line in lines[1]
                    if (line == 1) {
                        // we have either a <class> line or an <archive> line.
                        String line1 = lines[0].toString();
                        String line2 = lines[1].toString();
                        if (line2.startsWith("  ")) {
                            // we have a class line, record it.
                            parse(line1, line2);
                            // prepare for next <class> line.
                            lines[1] = new StringBuilder();
                        } else {
                            // We have an archive line: We are switching to the next archive.
                            // put the new <archive> line in lines[0], and prepare
                            // for reading the next <class> line
                            lines[0] = lines[1];
                            lines[1] = new StringBuilder();
                         }
                    } else {
                        // we just read the first <archive> line.
                        // prepare to read <class> lines.
                        line = 1;
                    }
                    sepi = 0;
                }
            } else {
                sepi = 0;
            }
        }

        // Takes a couple of lines, where line1 is an <archive> line and
        // line 2 is a <class> line. Parses the line to extract the archive
        // name and dependent class name, and record them in the map...
        void parse(String line1, String line2) {
            String archive = line1.substring(0, line1.indexOf(" -> "));
            int l2ArrowIndex = line2.indexOf(" -> ");
            String className = line2.substring(2, l2ArrowIndex).replace(" ", "");
            String depdescr = line2.substring(l2ArrowIndex + 4);
            String depclass = depdescr.substring(0, depdescr.indexOf(" "));
            deps.computeIfAbsent(archive, (k) -> new HashSet<>());
            deps.get(archive).add(className);
            if (VERBOSE) {
                System.out.println(archive+": "+className+" depends on "+depclass);
            }
        }

    }

    /**
     * The main method.
     *
     * Can be run in two modes:
     * <ul>
     * <li>From jtreg: expects 1 argument in the form {@code --test:<test-nb>}</li>
     * <li>From command line: expected syntax is {@code -e <pattern> -v jar [jars..]}</li>
     * </ul>
     * <p>When called from the command line this method will call jdeps recursively
     * to build a closure of the dependencies on {@code <pattern>} and print a summary.
     * <p>When called from jtreg - it will call jdeps either once only or
     * recursively depending on the pattern.
     * @param args either {@code --test:<test-nb>} or {@code -e <pattern> -v jar [jars..]}.
     */
    public static void main(String[] args) {
        runWithLocale(Locale.ENGLISH, TestCaseData.valueOf(args)::test);
    }

    private static void runWithLocale(Locale loc, Runnable run) {
        final Locale defaultLocale = Locale.getDefault();
        Locale.setDefault(loc);
        try {
            run.run();
        } finally {
            Locale.setDefault(defaultLocale);
        }
    }


    public static void test(String[] args, Map<String, Set<String>> expected,
            String expectedText, boolean closure) {
        try {
            doTest(args, expected, expectedText, closure);
        } catch (Throwable t) {
            try {
                printDiagnostic(args, expectedText, t, closure);
            } catch(Throwable tt) {
                throw t;
            }
            throw t;
        }
    }

    static class TextFormatException extends RuntimeException {
        final String expected;
        final String actual;
        TextFormatException(String message, String expected, String actual) {
            super(message);
            this.expected = expected;
            this.actual = actual;
        }
    }

    public static void printDiagnostic(String[] args, String expectedText,
            Throwable t, boolean closure) {
        if (expectedText != null || t instanceof TextFormatException) {
            System.err.println("=====   TEST FAILED   =======");
            System.err.println("command: " + Stream.of(args)
                    .reduce("jdeps", (s1,s2) -> s1.concat(" ").concat(s2)));
            System.err.println("===== Expected Output =======");
            System.err.append(expectedText);
            System.err.println("===== Command  Output =======");
            if (t instanceof TextFormatException) {
                System.err.print(((TextFormatException)t).actual);
            } else {
                com.sun.tools.jdeps.Main.run(args, new PrintWriter(System.err));
                if (closure) System.err.println("... (closure not available) ...");
            }
            System.err.println("=============================");
        }
    }

    public static void doTest(String[] args, Map<String, Set<String>> expected,
            String expectedText, boolean closure) {
        if (args.length < 3 || !"-e".equals(args[0]) || !"-v".equals(args[2])) {
            System.err.println("Syntax: -e <classname> -v [list of jars or directories]");
            return;
        }
        Map<String, Map<String, Set<String>>> alldeps = new HashMap<>();
        String depName = args[1];
        List<String> search = new ArrayList<>();
        search.add(depName);
        Set<String> searched = new LinkedHashSet<>();
        StringBuilder text = new StringBuilder();
        while(!search.isEmpty()) {
            args[1] = search.remove(0);
            if (VERBOSE) {
                System.out.println("Looking for " + args[1]);
            }
            searched.add(args[1]);
            Map<String, Set<String>> deps =
                    alldeps.computeIfAbsent(args[1], (k) -> new HashMap<>());
            OutputStreamParser parser = new OutputStreamParser(deps);
            PrintWriter writer = new PrintWriter(parser);
            com.sun.tools.jdeps.Main.run(args, writer);
            if (VERBOSE) {
                System.out.println("Found: " + deps.values().stream()
                        .flatMap(s -> s.stream()).collect(Collectors.toSet()));
            }
            if (expectedText != null) {
                text.append(parser.text.toString());
            }
            search.addAll(deps.values().stream()
                    .flatMap(s -> s.stream())
                    .filter(k -> !searched.contains(k))
                    .collect(Collectors.toSet()));
            if (!closure) break;
        }

        // Print summary...
        final Set<String> classes = alldeps.values().stream()
                .flatMap((m) -> m.values().stream())
                .flatMap(s -> s.stream()).collect(Collectors.toSet());
        Map<String, Set<String>> result = new HashMap<>();
        for (String c : classes) {
            Set<String> archives = new HashSet<>();
            Set<String> dependencies = new HashSet<>();
            for (String d : alldeps.keySet()) {
                Map<String, Set<String>> m = alldeps.get(d);
                for (String a : m.keySet()) {
                    Set<String> s = m.get(a);
                    if (s.contains(c)) {
                        archives.add(a);
                        dependencies.add(d);
                    }
                }
            }
            result.put(c, dependencies);
            System.out.println(c + " " + archives + " depends on " + dependencies);
        }

        // If we're in jtreg, then check result (expectedText != null)
        if (expectedText != null && COMPARE_TEXT) {
            //text.append(String.format("%n"));
            if (text.toString().equals(expectedText)) {
                System.out.println("SUCCESS - got expected text");
            } else {
                throw new TextFormatException("jdeps output is not as expected",
                                expectedText, text.toString());
            }
        }
        if (expected != null) {
            if (expected.equals(result)) {
                System.out.println("SUCCESS - found expected dependencies");
            } else if (expectedText == null) {
                throw new RuntimeException("Bad dependencies: Expected " + expected
                        + " but found " + result);
            } else {
                throw new TextFormatException("Bad dependencies: Expected "
                        + expected
                        + " but found " + result,
                        expectedText, text.toString());
            }
        }
    }
}