1/*
2 * Copyright (c) 2015, 2016, 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
24package compiler.compilercontrol.share.processors;
25
26import compiler.compilercontrol.share.method.MethodDescriptor;
27import compiler.compilercontrol.share.scenario.CompileCommand;
28import jdk.test.lib.Asserts;
29import jdk.test.lib.process.OutputAnalyzer;
30
31import java.util.ArrayList;
32import java.util.Iterator;
33import java.util.List;
34import java.util.function.Consumer;
35import java.util.regex.Matcher;
36import java.util.regex.Pattern;
37import java.util.stream.Collectors;
38
39public class PrintDirectivesProcessor
40        implements Consumer<List<OutputAnalyzer>> {
41    private final List<CompileCommand> commands;
42    private static final Pattern MATCH_PATTERN
43            = Pattern.compile(" matching: (.*)");
44
45    public PrintDirectivesProcessor(List<CompileCommand> commands) {
46        this.commands = commands;
47    }
48
49    @Override
50    public void accept(List<OutputAnalyzer> outputAnalyzers) {
51        List<String> directives = new ArrayList<>();
52        outputAnalyzers.forEach(outputAnalyzer ->
53                directives.addAll(getDirectives(outputAnalyzer)));
54        List<String> expectedDirectives = commands.stream()
55                .map(cc -> cc.methodDescriptor)
56                .map(MethodDescriptor::getCanonicalString)
57                .collect(Collectors.toList());
58
59        if (directives.size() != expectedDirectives.size()) {
60            printDirectives(directives, expectedDirectives);
61            throw new AssertionError(String.format("Different number of "
62                    + "directives. Expected: %d, actual: %d",
63                    expectedDirectives.size(), directives.size()));
64        }
65        for (int i = 0; i < directives.size(); i++) {
66            if (!directives.get(i).equals(expectedDirectives.get(i))) {
67                printDirectives(directives, expectedDirectives);
68                throw new AssertionError(
69                        String.format("Directives differ at %d, expected:%s%n",
70                                i, expectedDirectives.get(i)));
71            }
72        }
73    }
74
75    private List<String> getDirectives(OutputAnalyzer outputAnalyzer) {
76        List<String> directives = new ArrayList<>();
77        List<String> inputStrings = outputAnalyzer.asLines();
78        Iterator<String> iterator = inputStrings.iterator();
79        while (iterator.hasNext()) {
80            String input = iterator.next();
81            if (input.equals("Directive:")) {
82                Asserts.assertTrue(iterator.hasNext(), "inconsistent directive"
83                        + "printed into the output");
84                String matchString = iterator.next();
85                Matcher matcher = MATCH_PATTERN.matcher(matchString);
86                Asserts.assertTrue(matcher.matches(), "Incorrect matching "
87                        + "string in directive");
88                directives.add(matcher.group(1));
89            }
90        }
91        return directives;
92    }
93
94    private void printDirectives(List<String> directives,
95                                 List<String> expected) {
96        System.err.println("Actual directives: " + directives);
97        System.err.println("Expected directives: " + expected);
98    }
99}
100