1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.cts; 18 19 import junit.framework.TestFailure; 20 import junit.framework.TestResult; 21 22 import java.util.Enumeration; 23 import java.util.HashMap; 24 25 26 /** 27 * Store the result of a specific test. 28 */ 29 public class CtsTestResult { 30 private int mResultCode; 31 private String mFailedMessage; 32 private String mStackTrace; 33 34 public static final int CODE_INIT = -1; 35 public static final int CODE_NOT_EXECUTED = 0; 36 public static final int CODE_PASS = 1; 37 public static final int CODE_FAIL = 2; 38 public static final int CODE_ERROR = 3; 39 public static final int CODE_TIMEOUT = 4; 40 public static final int CODE_FIRST = CODE_INIT; 41 public static final int CODE_LAST = CODE_TIMEOUT; 42 43 public static final String STR_ERROR = "error"; 44 public static final String STR_TIMEOUT = "timeout"; 45 public static final String STR_NOT_EXECUTED = "notExecuted"; 46 public static final String STR_FAIL = "fail"; 47 public static final String STR_PASS = "pass"; 48 49 private static HashMap<Integer, String> sCodeToResultMap; 50 private static HashMap<String, Integer> sResultToCodeMap; 51 static { 52 sCodeToResultMap = new HashMap<Integer, String>(); 53 sCodeToResultMap.put(CODE_NOT_EXECUTED, STR_NOT_EXECUTED); 54 sCodeToResultMap.put(CODE_PASS, STR_PASS); 55 sCodeToResultMap.put(CODE_FAIL, STR_FAIL); 56 sCodeToResultMap.put(CODE_ERROR, STR_ERROR); 57 sCodeToResultMap.put(CODE_TIMEOUT, STR_TIMEOUT); 58 sResultToCodeMap = new HashMap<String, Integer>(); 59 for (int code : sCodeToResultMap.keySet()) { 60 sResultToCodeMap.put(sCodeToResultMap.get(code), code); 61 } 62 } 63 64 public CtsTestResult(int resCode) { 65 mResultCode = resCode; 66 } 67 68 public CtsTestResult(int resCode, final String failedMessage, final String stackTrace) { 69 mResultCode = resCode; 70 mFailedMessage = failedMessage; 71 mStackTrace = stackTrace; 72 } 73 74 public CtsTestResult(final String result, final String failedMessage, 75 final String stackTrace) throws InvalidTestResultStringException { 76 if (!sResultToCodeMap.containsKey(result)) { 77 throw new InvalidTestResultStringException(result); 78 } 79 80 mResultCode = sResultToCodeMap.get(result); 81 mFailedMessage = failedMessage; 82 mStackTrace = stackTrace; 83 } 84 85 /** 86 * Check if the result indicates failure. 87 * 88 * @return If failed, return true; else, return false. 89 */ 90 public boolean isFail() { 91 return mResultCode == CODE_FAIL; 92 } 93 94 /** 95 * Check if the result indicates pass. 96 * 97 * @return If pass, return true; else, return false. 98 */ 99 public boolean isPass() { 100 return mResultCode == CODE_PASS; 101 } 102 103 /** 104 * Check if the result indicates not executed. 105 * 106 * @return If not executed, return true; else, return false. 107 */ 108 public boolean isNotExecuted() { 109 return mResultCode == CODE_NOT_EXECUTED; 110 } 111 112 /** 113 * Get result code of the test. 114 * 115 * @return The result code of the test. 116 * The following is the possible result codes: 117 * <ul> 118 * <li> notExecuted 119 * <li> pass 120 * <li> fail 121 * <li> error 122 * <li> timeout 123 * </ul> 124 */ 125 public int getResultCode() { 126 return mResultCode; 127 } 128 129 /** 130 * Get the failed message. 131 * 132 * @return The failed message. 133 */ 134 public String getFailedMessage() { 135 return mFailedMessage; 136 } 137 138 /** 139 * Get the stack trace. 140 * 141 * @return The stack trace. 142 */ 143 public String getStackTrace() { 144 return mStackTrace; 145 } 146 147 /** 148 * Set the result. 149 * 150 * @param testResult The result in the form of JUnit test result. 151 */ 152 @SuppressWarnings("unchecked") 153 public void setResult(TestResult testResult) { 154 int resCode = CODE_PASS; 155 String failedMessage = null; 156 String stackTrace = null; 157 if ((testResult != null) && (testResult.failureCount() > 0 || testResult.errorCount() > 0)) { 158 resCode = CODE_FAIL; 159 Enumeration<TestFailure> failures = testResult.failures(); 160 while (failures.hasMoreElements()) { 161 TestFailure failure = failures.nextElement(); 162 failedMessage += failure.exceptionMessage(); 163 stackTrace += failure.trace(); 164 } 165 Enumeration<TestFailure> errors = testResult.errors(); 166 while (errors.hasMoreElements()) { 167 TestFailure failure = errors.nextElement(); 168 failedMessage += failure.exceptionMessage(); 169 stackTrace += failure.trace(); 170 } 171 } 172 mResultCode = resCode; 173 mFailedMessage = failedMessage; 174 mStackTrace = stackTrace; 175 } 176 177 /** 178 * Reverse the result code. 179 */ 180 public void reverse() { 181 if (isPass()) { 182 mResultCode = CtsTestResult.CODE_FAIL; 183 } else if (isFail()){ 184 mResultCode = CtsTestResult.CODE_PASS; 185 } 186 } 187 188 /** 189 * Get the test result as string. 190 * 191 * @return The readable result string. 192 */ 193 public String getResultString() { 194 return sCodeToResultMap.get(mResultCode); 195 } 196 197 /** 198 * Check if the given resultType is a valid result type defined.. 199 * 200 * @param resultType The result type to be checked. 201 * @return If valid, return true; else, return false. 202 */ 203 static public boolean isValidResultType(final String resultType) { 204 return sResultToCodeMap.containsKey(resultType); 205 } 206 } 207