Home | History | Annotate | Download | only in cts
      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_OMITTED = 5;
     41     public static final int CODE_FIRST = CODE_INIT;
     42     public static final int CODE_LAST = CODE_OMITTED;
     43 
     44     public static final String STR_ERROR = "error";
     45     public static final String STR_TIMEOUT = "timeout";
     46     public static final String STR_NOT_EXECUTED = "notExecuted";
     47     public static final String STR_OMITTED = "omitted";
     48     public static final String STR_FAIL = "fail";
     49     public static final String STR_PASS = "pass";
     50 
     51     private static HashMap<Integer, String> sCodeToResultMap;
     52     private static HashMap<String, Integer> sResultToCodeMap;
     53     static {
     54         sCodeToResultMap = new HashMap<Integer, String>();
     55         sCodeToResultMap.put(CODE_NOT_EXECUTED, STR_NOT_EXECUTED);
     56         sCodeToResultMap.put(CODE_PASS, STR_PASS);
     57         sCodeToResultMap.put(CODE_FAIL, STR_FAIL);
     58         sCodeToResultMap.put(CODE_ERROR, STR_ERROR);
     59         sCodeToResultMap.put(CODE_TIMEOUT, STR_TIMEOUT);
     60         sCodeToResultMap.put(CODE_OMITTED, STR_OMITTED);
     61         sResultToCodeMap = new HashMap<String, Integer>();
     62         for (int code : sCodeToResultMap.keySet()) {
     63             sResultToCodeMap.put(sCodeToResultMap.get(code), code);
     64         }
     65     }
     66 
     67     public CtsTestResult(int resCode) {
     68         mResultCode = resCode;
     69     }
     70 
     71     public CtsTestResult(int resCode, final String failedMessage, final String stackTrace) {
     72         mResultCode = resCode;
     73         mFailedMessage = failedMessage;
     74         mStackTrace = stackTrace;
     75     }
     76 
     77     public CtsTestResult(final String result, final String failedMessage,
     78             final String stackTrace) throws InvalidTestResultStringException {
     79         if (!sResultToCodeMap.containsKey(result)) {
     80             throw new InvalidTestResultStringException(result);
     81         }
     82 
     83         mResultCode = sResultToCodeMap.get(result);
     84         mFailedMessage = failedMessage;
     85         mStackTrace = stackTrace;
     86     }
     87 
     88     /**
     89      * Check if the result indicates failure.
     90      *
     91      * @return If failed, return true; else, return false.
     92      */
     93     public boolean isFail() {
     94         return mResultCode == CODE_FAIL;
     95     }
     96 
     97     /**
     98      * Check if the result indicates pass.
     99      *
    100      * @return If pass, return true; else, return false.
    101      */
    102     public boolean isPass() {
    103         return mResultCode == CODE_PASS;
    104     }
    105 
    106     /**
    107      * Check if the result indicates not executed.
    108      *
    109      * @return If not executed, return true; else, return false.
    110      */
    111     public boolean isNotExecuted() {
    112         return mResultCode == CODE_NOT_EXECUTED;
    113     }
    114 
    115     /**
    116      * Get result code of the test.
    117      *
    118      * @return The result code of the test.
    119      *         The following is the possible result codes:
    120      * <ul>
    121      *    <li> notExecuted
    122      *    <li> pass
    123      *    <li> fail
    124      *    <li> error
    125      *    <li> timeout
    126      * </ul>
    127      */
    128     public int getResultCode() {
    129         return mResultCode;
    130     }
    131 
    132     /**
    133      * Get the failed message.
    134      *
    135      * @return The failed message.
    136      */
    137     public String getFailedMessage() {
    138         return mFailedMessage;
    139     }
    140 
    141     /**
    142      * Get the stack trace.
    143      *
    144      * @return The stack trace.
    145      */
    146     public String getStackTrace() {
    147         return mStackTrace;
    148     }
    149 
    150     /**
    151      * Set the result.
    152      *
    153      * @param testResult The result in the form of JUnit test result.
    154      */
    155     @SuppressWarnings("unchecked")
    156     public void setResult(TestResult testResult) {
    157         int resCode = CODE_PASS;
    158         String failedMessage = null;
    159         String stackTrace = null;
    160         if ((testResult != null) && (testResult.failureCount() > 0 || testResult.errorCount() > 0)) {
    161             resCode = CODE_FAIL;
    162             Enumeration<TestFailure> failures = testResult.failures();
    163             while (failures.hasMoreElements()) {
    164                 TestFailure failure = failures.nextElement();
    165                 failedMessage += failure.exceptionMessage();
    166                 stackTrace += failure.trace();
    167             }
    168             Enumeration<TestFailure> errors = testResult.errors();
    169             while (errors.hasMoreElements()) {
    170                 TestFailure failure = errors.nextElement();
    171                 failedMessage += failure.exceptionMessage();
    172                 stackTrace += failure.trace();
    173             }
    174         }
    175         mResultCode = resCode;
    176         mFailedMessage = failedMessage;
    177         mStackTrace = stackTrace;
    178     }
    179 
    180     /**
    181      * Reverse the result code.
    182      */
    183     public void reverse() {
    184         if (isPass()) {
    185             mResultCode = CtsTestResult.CODE_FAIL;
    186         } else if (isFail()){
    187             mResultCode = CtsTestResult.CODE_PASS;
    188         }
    189     }
    190 
    191     /**
    192      * Get the test result as string.
    193      *
    194      * @return The readable result string.
    195      */
    196     public String getResultString() {
    197         return sCodeToResultMap.get(mResultCode);
    198     }
    199 
    200     /**
    201      * Check if the given resultType is a valid result type defined..
    202      *
    203      * @param resultType The result type to be checked.
    204      * @return If valid, return true; else, return false.
    205      */
    206     static public boolean isValidResultType(final String resultType) {
    207         return sResultToCodeMap.containsKey(resultType);
    208     }
    209 }
    210