1 /* 2 * Copyright (C) 2015 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.compatibility.common.util; 17 18 import java.util.ArrayList; 19 import java.util.Collections; 20 import java.util.HashMap; 21 import java.util.List; 22 import java.util.Map; 23 24 /** 25 * Data structure for a Compatibility test case result. 26 */ 27 public class CaseResult implements ICaseResult { 28 29 private String mName; 30 31 private Map<String, ITestResult> mResults = new HashMap<>(); 32 33 /** 34 * Creates a {@link CaseResult} for the given name, eg <package-name>.<class-name> 35 */ 36 public CaseResult(String name) { 37 mName = name; 38 } 39 40 /** 41 * {@inheritDoc} 42 */ 43 @Override 44 public String getName() { 45 return mName; 46 } 47 48 /** 49 * {@inheritDoc} 50 */ 51 @Override 52 public ITestResult getOrCreateResult(String testName) { 53 ITestResult result = mResults.get(testName); 54 if (result == null) { 55 result = new TestResult(this, testName); 56 mResults.put(testName, result); 57 } 58 return result; 59 } 60 61 /** 62 * {@inheritDoc} 63 */ 64 @Override 65 public ITestResult getResult(String testName) { 66 return mResults.get(testName); 67 } 68 69 /** 70 * {@inheritDoc} 71 */ 72 @Override 73 public List<ITestResult> getResults(TestStatus status) { 74 List<ITestResult> results = new ArrayList<>(); 75 for (ITestResult result : mResults.values()) { 76 if (result.getResultStatus() == status) { 77 results.add(result); 78 } 79 } 80 return results; 81 } 82 83 /** 84 * {@inheritDoc} 85 */ 86 @Override 87 public List<ITestResult> getResults() { 88 ArrayList<ITestResult> results = new ArrayList<>(mResults.values()); 89 Collections.sort(results); 90 return results; 91 } 92 93 /** 94 * {@inheritDoc} 95 */ 96 @Override 97 public int countResults(TestStatus status) { 98 int total = 0; 99 for (ITestResult result : mResults.values()) { 100 if (result.getResultStatus() == status) { 101 total++; 102 } 103 } 104 return total; 105 } 106 107 /** 108 * {@inheritDoc} 109 */ 110 @Override 111 public int compareTo(ICaseResult another) { 112 return getName().compareTo(another.getName()); 113 } 114 115 /** 116 * {@inheritDoc} 117 */ 118 @Override 119 public void mergeFrom(ICaseResult otherCaseResult) { 120 if (!otherCaseResult.getName().equals(getName())) { 121 throw new IllegalArgumentException(String.format( 122 "Cannot merge case result with mismatched name. Expected %s, Found %s", 123 otherCaseResult.getName(), getName())); 124 } 125 126 for (ITestResult otherTestResult : otherCaseResult.getResults()) { 127 mResults.put(otherTestResult.getName(), otherTestResult); 128 } 129 } 130 131 } 132