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.method.MethodDescriptor;
  27 import compiler.compilercontrol.share.method.MethodGenerator;
  28 import compiler.compilercontrol.share.scenario.State;
  29 import jdk.test.lib.Asserts;
  30 import jdk.test.lib.OutputAnalyzer;
  31 import jdk.test.lib.Pair;
  32 import pool.PoolHelper;
  33 
  34 import java.io.File;
  35 import java.io.FileNotFoundException;
  36 import java.lang.reflect.Executable;
  37 import java.util.List;
  38 import java.util.Map;
  39 import java.util.Scanner;
  40 import java.util.function.Consumer;
  41 import java.util.regex.Matcher;
  42 import java.util.regex.Pattern;
  43 import java.util.stream.Collectors;
  44 
  45 /**
  46  * Log compilation file processor
  47  */
  48 public class LogProcessor implements Consumer<OutputAnalyzer> {
  49     public static final String LOG_FILE = "compilation.log";
  50     private static final String TASK_ELEMENT = "<task [^>]*>";
  51     private static final String TASK_DONE_ELEMENT = "<task_done [^>]*>";
  52     private static final String TASK_END_ELEMENT = "</task>";
  53     private static final String ANY_ELEMENT = "<[^>]*>";
  54     private static final Pattern METHOD_PATTERN = Pattern.compile(
  55             "method='([^']+)'");
  56     private final List<String> loggedMethods;
  57     private final List<String> testMethods;
  58     private Scanner scanner = null;
  59 
  60     public LogProcessor(Map<Executable, State> states) {
  61         loggedMethods = states.keySet().stream()
  62                 .filter(x -> states.get(x).isLog())
  63                 .map(MethodGenerator::logDescriptor)
  64                 .map(MethodDescriptor::getString)
  65                 .collect(Collectors.toList());
  66         testMethods = new PoolHelper().getAllMethods()
  67                 .stream()
  68                 .map(pair -> pair.first)
  69                 .map(MethodGenerator::logDescriptor)
  70                 .map(MethodDescriptor::getString)
  71                 .collect(Collectors.toList());
  72     }
  73 
  74     @Override
  75     public void accept(OutputAnalyzer outputAnalyzer) {
  76         if (loggedMethods.isEmpty()) {
  77             return;
  78         }
  79         getScanner();
  80         matchTasks();
  81     }
  82 
  83     /*
  84      * Gets scanner for log file of the test case
  85      */
  86     private Scanner getScanner() {
  87         File logFile = new File(LOG_FILE);
  88         try {
  89             scanner = new Scanner(logFile);
  90         } catch (FileNotFoundException e) {
  91             throw new Error("TESTBUG: file not found: " + logFile, e);
  92         }
  93         return scanner;
  94     }
  95 
  96     /*
  97      * Parses for &lt;task method='java.lang.String indexOf (I)I' &gt;
  98      * and finds if there is a compilation log for this task
  99      */
 100     private void matchTasks() {
 101         String task = scanner.findWithinHorizon(TASK_ELEMENT, 0);
 102         while (task != null) {
 103             String element = scanner.findWithinHorizon(ANY_ELEMENT, 0);
 104             if (Pattern.matches(TASK_DONE_ELEMENT, element)
 105                     || Pattern.matches(TASK_END_ELEMENT, element)) {
 106                 /* If there is nothing between <task> and </task>
 107                    except <task done /> then compilation log is empty */
 108                 Asserts.assertTrue(matchMethod(task), "Compilation log "
 109                         + "expected. Met: " + element);
 110             }
 111             task = scanner.findWithinHorizon(TASK_ELEMENT, 0);
 112         }
 113     }
 114 
 115     // Matches given string to regular expression
 116     private boolean matchMethod(String input) {
 117         Matcher matcher = METHOD_PATTERN.matcher(input);
 118         Asserts.assertTrue(matcher.find(), "Wrong matcher or input");
 119         // Get method and normalize it
 120         String method = normalize(matcher.group(1));
 121         // Check that this method matches regexp
 122         return loggedMethods.contains(method) || !testMethods.contains(method);
 123     }
 124 
 125     // Normalize given signature to conform regular expression used in tests
 126     private String normalize(String method) {
 127         return method.replaceAll("\\.", "/") // replace dots in a class string
 128                 .replaceFirst(" ", ".")      // replace space between class and method
 129                 .replaceFirst(" ", "")       // remove space between method and signature
 130                 .replace("&lt;", "<")
 131                 .replace("&gt;", ">");
 132     }
 133 }