Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.tradefed.result; |
| 17 | |
Brett Chabot | 5ec5292 | 2010-09-07 18:08:30 -0700 | [diff] [blame] | 18 | import com.android.ddmlib.testrunner.TestIdentifier; |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 19 | import com.android.tradefed.result.TestResult.TestStatus; |
Brett Chabot | 5ec5292 | 2010-09-07 18:08:30 -0700 | [diff] [blame] | 20 | import com.android.tradefed.targetsetup.IBuildInfo; |
| 21 | |
Brett Chabot | feeb214 | 2010-04-16 16:22:52 -0700 | [diff] [blame] | 22 | import java.io.InputStream; |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 23 | import java.util.Collection; |
Brett Chabot | fccfc61 | 2010-03-29 16:08:49 -0700 | [diff] [blame] | 24 | import java.util.Collections; |
| 25 | import java.util.LinkedHashMap; |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 26 | import java.util.Map; |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 29 | * A {@link ITestInvocationListener} that will collect all test results. |
| 30 | * <p/> |
| 31 | * Although the data structures used in this object are thread-safe, the |
| 32 | * {@link ITestInvocationListener} callbacks must be called in the correct order. |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 33 | */ |
Brett Chabot | feeb214 | 2010-04-16 16:22:52 -0700 | [diff] [blame] | 34 | public class CollectingTestListener implements ITestInvocationListener { |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 35 | |
Brett Chabot | fccfc61 | 2010-03-29 16:08:49 -0700 | [diff] [blame] | 36 | // Stores the test results |
| 37 | // Uses a synchronized map to make thread safe. |
| 38 | // Uses a LinkedHashmap to have predictable iteration order |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 39 | private Map<String, TestRunResult> mRunResultsMap = |
| 40 | Collections.synchronizedMap(new LinkedHashMap<String, TestRunResult>()); |
| 41 | private TestRunResult mCurrentResults = null; |
| 42 | |
| 43 | // cached test constants |
| 44 | private Integer mNumTotalTests = null; |
| 45 | private Integer mNumPassedTests = null; |
| 46 | private Integer mNumFailedTests = null; |
| 47 | private Integer mNumErrorTests = null; |
| 48 | |
| 49 | /** |
| 50 | * {@inheritDoc} |
| 51 | */ |
| 52 | public void invocationStarted(IBuildInfo buildInfo) { |
| 53 | // ignore |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * {@inheritDoc} |
| 58 | */ |
| 59 | public void testRunStarted(String name, int numTests) { |
| 60 | if (mRunResultsMap.containsKey(name)) { |
| 61 | // rerun of previous run. Add test results to it |
| 62 | mCurrentResults = mRunResultsMap.get(name); |
| 63 | } else { |
| 64 | // new run |
| 65 | mCurrentResults = new TestRunResult(name); |
| 66 | mRunResultsMap.put(name, mCurrentResults); |
| 67 | } |
| 68 | mCurrentResults.setRunComplete(false); |
| 69 | mCurrentResults.setRunFailed(false); |
| 70 | } |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * {@inheritDoc} |
| 74 | */ |
| 75 | public void testStarted(TestIdentifier test) { |
| 76 | // ignore |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * {@inheritDoc} |
| 81 | */ |
Omari Stephens | 81e6ce0 | 2010-08-05 21:58:14 -0700 | [diff] [blame] | 82 | public void testEnded(TestIdentifier test, Map<String, String> testMetrics) { |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 83 | if (mCurrentResults == null) { |
| 84 | throw new IllegalStateException("testEnded called before testRunStarted"); |
| 85 | } |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 86 | // only record test pass if failure not already recorded |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 87 | if (!mCurrentResults.getTestResults().containsKey(test)) { |
| 88 | mCurrentResults.getTestResults().put(test, new TestResult(TestStatus.PASSED)); |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * {@inheritDoc} |
| 94 | */ |
| 95 | public void testFailed(TestFailure status, TestIdentifier test, String trace) { |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 96 | if (mCurrentResults == null) { |
| 97 | throw new IllegalStateException("testFailed called before testRunStarted"); |
| 98 | } |
Brett Chabot | fccfc61 | 2010-03-29 16:08:49 -0700 | [diff] [blame] | 99 | if (status.equals(TestFailure.ERROR)) { |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 100 | mCurrentResults.getTestResults().put(test, new TestResult(TestStatus.ERROR, trace)); |
Brett Chabot | fccfc61 | 2010-03-29 16:08:49 -0700 | [diff] [blame] | 101 | } else { |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 102 | mCurrentResults.getTestResults().put(test, new TestResult(TestStatus.FAILURE, trace)); |
Brett Chabot | fccfc61 | 2010-03-29 16:08:49 -0700 | [diff] [blame] | 103 | } |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * {@inheritDoc} |
| 108 | */ |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 109 | public void testRunEnded(long elapsedTime, Map<String, String> runMetrics) { |
| 110 | if (mCurrentResults == null) { |
| 111 | throw new IllegalStateException("testRunEnded called before testRunStarted"); |
| 112 | } |
| 113 | mCurrentResults.setRunComplete(true); |
| 114 | mCurrentResults.setMetrics(runMetrics); |
| 115 | mCurrentResults.addElapsedTime(elapsedTime); |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
| 119 | * {@inheritDoc} |
| 120 | */ |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 121 | public void testRunFailed(String errorMessage) { |
| 122 | if (mCurrentResults == null) { |
| 123 | throw new IllegalStateException("testRunFailed called before testRunStarted"); |
| 124 | } |
| 125 | mCurrentResults.setRunComplete(true); |
| 126 | mCurrentResults.setRunFailed(true); |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * {@inheritDoc} |
| 131 | */ |
| 132 | public void testRunStopped(long elapsedTime) { |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 133 | if (mCurrentResults == null) { |
| 134 | throw new IllegalStateException("testRunStopped called before testRunStarted"); |
| 135 | } |
| 136 | mCurrentResults.setRunComplete(true); |
| 137 | mCurrentResults.addElapsedTime(elapsedTime); |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 141 | * Gets the results for the current test run. |
Brett Chabot | 4d081a9 | 2010-09-29 16:44:19 -0700 | [diff] [blame^] | 142 | * <p/> |
| 143 | * Its intended to be called once test run is complete (ie {@link #testRunEnded(long, Map)} has |
| 144 | * been called). Calling this method before the run has even started |
| 145 | * (ie before {@link #testRunStarted(String, int)} has been called) is invalid, and will |
| 146 | * produce a {@link IllegalStateException}. |
| 147 | * |
| 148 | * @return the {@link TestRunResult} representing data collected during last test run |
| 149 | * @throws IllegalStateException if no test run data has been collected. This can occur if this |
| 150 | * method is called before {@link #testRunStarted(String, int))} has been called. |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 151 | */ |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 152 | public TestRunResult getCurrentRunResults() { |
Brett Chabot | 4d081a9 | 2010-09-29 16:44:19 -0700 | [diff] [blame^] | 153 | if (mCurrentResults == null) { |
| 154 | throw new IllegalStateException("no current results"); |
| 155 | } |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 156 | return mCurrentResults; |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 160 | * Gets the results for all test runs. |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 161 | */ |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 162 | public Collection<TestRunResult> getRunResults() { |
| 163 | return mRunResultsMap.values(); |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 167 | * Gets the total number of tests for all runs. |
Guang Zhu | 42fb282 | 2010-06-17 17:55:28 -0700 | [diff] [blame] | 168 | */ |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 169 | public int getNumTotalTests() { |
| 170 | if (!areTestCountsCalculated()) { |
| 171 | calculateTestCounts(); |
| 172 | } |
| 173 | return mNumTotalTests; |
Guang Zhu | 42fb282 | 2010-06-17 17:55:28 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 177 | * Gets the total number of failed tests for all runs. |
Brett Chabot | fccfc61 | 2010-03-29 16:08:49 -0700 | [diff] [blame] | 178 | */ |
| 179 | public int getNumFailedTests() { |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 180 | if (!areTestCountsCalculated()) { |
| 181 | calculateTestCounts(); |
| 182 | } |
| 183 | return mNumFailedTests; |
Brett Chabot | fccfc61 | 2010-03-29 16:08:49 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 187 | * Gets the total number of error tests for all runs. |
Brett Chabot | fccfc61 | 2010-03-29 16:08:49 -0700 | [diff] [blame] | 188 | */ |
| 189 | public int getNumErrorTests() { |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 190 | if (!areTestCountsCalculated()) { |
| 191 | calculateTestCounts(); |
| 192 | } |
| 193 | return mNumErrorTests; |
Brett Chabot | fccfc61 | 2010-03-29 16:08:49 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 196 | /** |
| 197 | * Gets the total number of passed tests for all runs. |
| 198 | */ |
| 199 | public int getNumPassedTests() { |
| 200 | if (!areTestCountsCalculated()) { |
| 201 | calculateTestCounts(); |
| 202 | } |
| 203 | return mNumPassedTests; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @returns true if invocation had any failed or error tests. |
| 208 | */ |
Brett Chabot | 057e16e | 2010-06-27 21:53:48 -0700 | [diff] [blame] | 209 | public boolean hasFailedTests() { |
| 210 | return getNumErrorTests() > 0 || getNumFailedTests() > 0; |
| 211 | } |
| 212 | |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 213 | private synchronized boolean areTestCountsCalculated() { |
| 214 | return mNumTotalTests != null; |
| 215 | } |
| 216 | |
| 217 | private synchronized void calculateTestCounts() { |
| 218 | mNumTotalTests = 0; |
| 219 | mNumPassedTests = 0; |
| 220 | mNumFailedTests = 0; |
| 221 | mNumErrorTests = 0; |
| 222 | for (TestRunResult runResult : getRunResults()) { |
| 223 | mNumTotalTests += runResult.getNumTests(); |
| 224 | mNumPassedTests += runResult.getNumPassedTests(); |
| 225 | mNumFailedTests += runResult.getNumFailedTests(); |
| 226 | mNumErrorTests += runResult.getNumErrorTests(); |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 227 | } |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 228 | } |
Brett Chabot | feeb214 | 2010-04-16 16:22:52 -0700 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * {@inheritDoc} |
| 232 | */ |
Brett Chabot | 3759c85 | 2010-07-13 17:36:31 -0700 | [diff] [blame] | 233 | public void invocationEnded(long elapsedTime) { |
Brett Chabot | feeb214 | 2010-04-16 16:22:52 -0700 | [diff] [blame] | 234 | // ignore |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * {@inheritDoc} |
| 239 | */ |
Omari Stephens | 203296c | 2010-09-02 18:20:45 -0700 | [diff] [blame] | 240 | public void invocationFailed(Throwable cause) { |
Brett Chabot | feeb214 | 2010-04-16 16:22:52 -0700 | [diff] [blame] | 241 | // ignore |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * {@inheritDoc} |
| 246 | */ |
Omari Stephens | a01abfe | 2010-09-07 15:48:44 -0700 | [diff] [blame] | 247 | public TestSummary getSummary() { |
Brett Chabot | fe88a15 | 2010-08-02 17:52:59 -0700 | [diff] [blame] | 248 | // ignore |
Omari Stephens | 203296c | 2010-09-02 18:20:45 -0700 | [diff] [blame] | 249 | return null; |
Brett Chabot | fe88a15 | 2010-08-02 17:52:59 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
| 253 | * {@inheritDoc} |
| 254 | */ |
Brett Chabot | 057e16e | 2010-06-27 21:53:48 -0700 | [diff] [blame] | 255 | public void testLog(String dataName, LogDataType dataType, InputStream dataStream) { |
Brett Chabot | feeb214 | 2010-04-16 16:22:52 -0700 | [diff] [blame] | 256 | // ignore |
| 257 | } |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 258 | } |