1 /*
   2  * Copyright (c) 2015, 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 
  24 package compiler.compilercontrol.share.processors;
  25 
  26 import compiler.compilercontrol.share.scenario.CompileCommand;
  27 import jdk.test.lib.Asserts;
  28 import jdk.test.lib.process.OutputAnalyzer;
  29 
  30 import java.util.Iterator;
  31 import java.util.List;
  32 import java.util.function.Consumer;
  33 
  34 /**
  35  * Checks that output contains a string with commands and full method pattern
  36  */
  37 public class CommandProcessor implements Consumer<OutputAnalyzer> {
  38     private static final String INVALID_COMMAND_MSG = "CompileCommand: "
  39             + "\\b(unrecognized command|Bad pattern|"
  40             + "An error occurred during parsing)\\b";
  41     private final Iterator<CompileCommand> nonQuietedIterator;
  42     private final Iterator<CompileCommand> quietedIterator;
  43 
  44     public CommandProcessor(List<CompileCommand> nonQuieted,
  45                             List<CompileCommand> quieted) {
  46         this.nonQuietedIterator = nonQuieted.iterator();
  47         this.quietedIterator = quieted.iterator();
  48     }
  49 
  50     @Override
  51     public void accept(OutputAnalyzer outputAnalyzer) {
  52         try {
  53             outputAnalyzer.asLines().stream()
  54                     .filter(s -> s.startsWith("CompileCommand:"))
  55                     .forEachOrdered(this::check);
  56         } catch (Exception e) {
  57             System.err.println(outputAnalyzer.getOutput());
  58             throw e;
  59         }
  60     }
  61 
  62     private void check(String input) {
  63         if (nonQuietedIterator.hasNext()) {
  64             CompileCommand command = nonQuietedIterator.next();
  65             if (command.isValid()) {
  66                 Asserts.assertTrue(input.contains(getOutputString(command)),
  67                         getOutputString(command) + "missing in output");
  68             } else {
  69                 Asserts.assertTrue(input.matches(INVALID_COMMAND_MSG),
  70                         "Error message missing for: " + getOutputString(
  71                                 command));
  72             }
  73         } else if (quietedIterator.hasNext()) {
  74             CompileCommand command = quietedIterator.next();
  75             if (command.isValid()) {
  76                 Asserts.assertFalse(input.contains(getOutputString(command)));
  77             } else {
  78                 Asserts.assertTrue(input.matches(INVALID_COMMAND_MSG),
  79                         "Error message missing for: " + getOutputString(
  80                                 command));
  81             }
  82         }
  83     }
  84 
  85     private String getOutputString(CompileCommand command) {
  86         return "CompileCommand: "
  87                 + command.command.name + " "
  88                 + command.methodDescriptor.getCanonicalString();
  89     }
  90 }