1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.devicehealthchecks; 17 18 import java.util.ArrayList; 19 import java.util.HashMap; 20 import java.util.List; 21 import java.util.Map; 22 import java.util.regex.Matcher; 23 import java.util.regex.Pattern; 24 25 /** 26 * A class to manage and match known failures 27 */ 28 public class KnownFailures { 29 30 private Map<String, List<KnownFailureItem>> mKnownFailures = new HashMap<>(); 31 32 /** 33 * Convenience method to add a known failure to list 34 * @param dropboxLabel 35 * @param pattern 36 * @param bugNumber 37 */ addKnownFailure(String dropboxLabel, Pattern pattern, String bugNumber)38 public void addKnownFailure(String dropboxLabel, Pattern pattern, String bugNumber) { 39 // Example: 40 // addKnownFailure( 41 // "system_app_anr", // dropbox label 42 // Pattern.compile("^.*Process:.*com\\.google\\.android\\.googlequicksearchbox" 43 // + ":search.*$", Pattern.MULTILINE), // regex pattern 44 // "73254069"); // bug number 45 // It's recommended to use multiline matching and a snippet will be matched if it's found 46 // to contain the pattern described by regex 47 List<KnownFailureItem> knownFailures = mKnownFailures.get(dropboxLabel); 48 if (knownFailures == null) { 49 knownFailures = new ArrayList<>(); 50 mKnownFailures.put(dropboxLabel, knownFailures); 51 } 52 knownFailures.add(new KnownFailureItem(pattern, bugNumber)); 53 } 54 55 /** 56 * Check if a dropbox snippet of the specificed label type contains a known failure 57 * @param dropboxLabel 58 * @param dropboxSnippet 59 * @return the first known failure pattern matched, or <code>null</code> if none matched 60 */ findMatchedKnownFailure(String dropboxLabel, String dropboxSnippet)61 public KnownFailureItem findMatchedKnownFailure(String dropboxLabel, String dropboxSnippet) { 62 List<KnownFailureItem> knownFailureItems = mKnownFailures.get(dropboxLabel); 63 if (knownFailureItems != null) { 64 for (KnownFailureItem k : knownFailureItems) { 65 Matcher m = k.failurePattern.matcher(dropboxSnippet); 66 if (m.find()) { 67 return k; 68 } 69 } 70 } 71 return null; 72 } 73 } 74