Home | History | Annotate | Download | only in result
      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 
     18 import com.android.tradefed.invoker.IInvocationContext;
     19 import com.android.tradefed.log.LogUtil.CLog;
     20 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
     21 
     22 import java.util.Arrays;
     23 import java.util.Collections;
     24 import java.util.HashMap;
     25 import java.util.List;
     26 
     27 /**
     28  * A {@link ITestInvocationListener} that forwards invocation results to a list of other listeners.
     29  */
     30 public class ResultForwarder implements ITestInvocationListener {
     31 
     32     private List<ITestInvocationListener> mListeners;
     33 
     34     /**
     35      * Create a {@link ResultForwarder} with deferred listener setting.  Intended only for use by
     36      * subclasses.
     37      */
     38     protected ResultForwarder() {
     39         mListeners = Collections.emptyList();
     40     }
     41 
     42     /**
     43      * Create a {@link ResultForwarder}.
     44      *
     45      * @param listeners the real {@link ITestInvocationListener}s to forward results to
     46      */
     47     public ResultForwarder(List<ITestInvocationListener> listeners) {
     48         mListeners = listeners;
     49     }
     50 
     51     /**
     52      * Alternate variable arg constructor for {@link ResultForwarder}.
     53      *
     54      * @param listeners the real {@link ITestInvocationListener}s to forward results to
     55      */
     56     public ResultForwarder(ITestInvocationListener... listeners) {
     57         mListeners = Arrays.asList(listeners);
     58     }
     59 
     60     /**
     61      * Set the listeners after construction.  Intended only for use by subclasses.
     62      *
     63      * @param listeners the real {@link ITestInvocationListener}s to forward results to
     64      */
     65     protected void setListeners(List<ITestInvocationListener> listeners) {
     66         mListeners = listeners;
     67     }
     68 
     69     /**
     70      * Set the listeners after construction.  Intended only for use by subclasses.
     71      *
     72      * @param listeners the real {@link ITestInvocationListener}s to forward results to
     73      */
     74     protected void setListeners(ITestInvocationListener... listeners) {
     75         mListeners = Arrays.asList(listeners);
     76     }
     77 
     78     /**
     79      * Get the list of listeners.  Intended only for use by subclasses.
     80      *
     81      * @return The list of {@link ITestInvocationListener}s.
     82      */
     83     protected List<ITestInvocationListener> getListeners() {
     84         return mListeners;
     85     }
     86 
     87     /**
     88      * {@inheritDoc}
     89      */
     90     @Override
     91     public void invocationStarted(IInvocationContext context) {
     92         for (ITestInvocationListener listener : mListeners) {
     93             try {
     94                 listener.invocationStarted(context);
     95             } catch (RuntimeException e) {
     96                 CLog.e(
     97                         "Exception while invoking %s#invocationStarted",
     98                         listener.getClass().getName());
     99                 CLog.e(e);
    100             }
    101         }
    102     }
    103 
    104     /**
    105      * {@inheritDoc}
    106      */
    107     @Override
    108     public void invocationFailed(Throwable cause) {
    109         for (ITestInvocationListener listener : mListeners) {
    110             try {
    111                 listener.invocationFailed(cause);
    112             } catch (RuntimeException e) {
    113                 CLog.e(
    114                         "Exception while invoking %s#invocationFailed",
    115                         listener.getClass().getName());
    116                 CLog.e(e);
    117             }
    118         }
    119     }
    120 
    121     /**
    122      * {@inheritDoc}
    123      */
    124     @Override
    125     public void invocationEnded(long elapsedTime) {
    126         InvocationSummaryHelper.reportInvocationEnded(mListeners, elapsedTime);
    127     }
    128 
    129     /**
    130      * {@inheritDoc}
    131      */
    132     @Override
    133     public TestSummary getSummary() {
    134         // should never be called
    135         return null;
    136     }
    137 
    138     /**
    139      * {@inheritDoc}
    140      */
    141     @Override
    142     public void testLog(String dataName, LogDataType dataType, InputStreamSource dataStream) {
    143         for (ITestInvocationListener listener : mListeners) {
    144             try {
    145                 listener.testLog(dataName, dataType, dataStream);
    146             } catch (RuntimeException e) {
    147                 CLog.e("Exception while invoking %s#testLog", listener.getClass().getName());
    148                 CLog.e(e);
    149             }
    150         }
    151     }
    152 
    153     /**
    154      * {@inheritDoc}
    155      */
    156     @Override
    157     public void testRunStarted(String runName, int testCount) {
    158         for (ITestInvocationListener listener : mListeners) {
    159             try {
    160                 listener.testRunStarted(runName, testCount);
    161             } catch (RuntimeException e) {
    162                 CLog.e("Exception while invoking %s#testRunStarted", listener.getClass().getName());
    163                 CLog.e(e);
    164             }
    165         }
    166     }
    167 
    168     /**
    169      * {@inheritDoc}
    170      */
    171     @Override
    172     public void testRunFailed(String errorMessage) {
    173         for (ITestInvocationListener listener : mListeners) {
    174             try {
    175                 listener.testRunFailed(errorMessage);
    176             } catch (RuntimeException e) {
    177                 CLog.e("Exception while invoking %s#testRunFailed", listener.getClass().getName());
    178                 CLog.e(e);
    179             }
    180         }
    181     }
    182 
    183     /**
    184      * {@inheritDoc}
    185      */
    186     @Override
    187     public void testRunStopped(long elapsedTime) {
    188         for (ITestInvocationListener listener : mListeners) {
    189             try {
    190                 listener.testRunStopped(elapsedTime);
    191             } catch (RuntimeException e) {
    192                 CLog.e("Exception while invoking %s#testRunStopped", listener.getClass().getName());
    193                 CLog.e(e);
    194             }
    195         }
    196     }
    197 
    198     /** {@inheritDoc} */
    199     @Override
    200     public void testRunEnded(long elapsedTime, HashMap<String, Metric> runMetrics) {
    201         for (ITestInvocationListener listener : mListeners) {
    202             try {
    203                 listener.testRunEnded(elapsedTime, runMetrics);
    204             } catch (RuntimeException e) {
    205                 CLog.e("Exception while invoking %s#testRunEnded", listener.getClass().getName());
    206                 CLog.e(e);
    207             }
    208         }
    209     }
    210 
    211     /** {@inheritDoc} */
    212     @Override
    213     public void testStarted(TestDescription test) {
    214         testStarted(test, System.currentTimeMillis());
    215     }
    216 
    217     /** {@inheritDoc} */
    218     @Override
    219     public void testStarted(TestDescription test, long startTime) {
    220         for (ITestInvocationListener listener : mListeners) {
    221             try {
    222                 listener.testStarted(test, startTime);
    223             } catch (RuntimeException e) {
    224                 CLog.e("Exception while invoking %s#testStarted", listener.getClass().getName());
    225                 CLog.e(e);
    226             }
    227         }
    228     }
    229 
    230     /** {@inheritDoc} */
    231     @Override
    232     public void testFailed(TestDescription test, String trace) {
    233         for (ITestInvocationListener listener : mListeners) {
    234             try {
    235                 listener.testFailed(test, trace);
    236             } catch (RuntimeException e) {
    237                 CLog.e("Exception while invoking %s#testFailed", listener.getClass().getName());
    238                 CLog.e(e);
    239             }
    240         }
    241     }
    242 
    243     /** {@inheritDoc} */
    244     @Override
    245     public void testEnded(TestDescription test, HashMap<String, Metric> testMetrics) {
    246         testEnded(test, System.currentTimeMillis(), testMetrics);
    247     }
    248 
    249     /** {@inheritDoc} */
    250     @Override
    251     public void testEnded(TestDescription test, long endTime, HashMap<String, Metric> testMetrics) {
    252         for (ITestInvocationListener listener : mListeners) {
    253             try {
    254                 listener.testEnded(test, endTime, testMetrics);
    255             } catch (RuntimeException e) {
    256                 CLog.e("Exception while invoking %s#testEnded", listener.getClass().getName());
    257                 CLog.e(e);
    258             }
    259         }
    260     }
    261 
    262     @Override
    263     public void testAssumptionFailure(TestDescription test, String trace) {
    264         for (ITestInvocationListener listener : mListeners) {
    265             try {
    266                 listener.testAssumptionFailure(test, trace);
    267             } catch (RuntimeException e) {
    268                 CLog.e(
    269                         "Exception while invoking %s#testAssumptionFailure",
    270                         listener.getClass().getName());
    271                 CLog.e(e);
    272             }
    273         }
    274     }
    275 
    276     @Override
    277     public void testIgnored(TestDescription test) {
    278         for (ITestInvocationListener listener : mListeners) {
    279             try {
    280                 listener.testIgnored(test);
    281             } catch (RuntimeException e) {
    282                 CLog.e("Exception while invoking %s#testIgnored", listener.getClass().getName());
    283                 CLog.e(e);
    284             }
    285         }
    286     }
    287 
    288     @Override
    289     public void testModuleStarted(IInvocationContext moduleContext) {
    290         for (ITestInvocationListener listener : mListeners) {
    291             try {
    292                 listener.testModuleStarted(moduleContext);
    293             } catch (RuntimeException e) {
    294                 CLog.e("Exception while invoking testModuleStarted");
    295                 CLog.e(e);
    296             }
    297         }
    298     }
    299 
    300     @Override
    301     public void testModuleEnded() {
    302         for (ITestInvocationListener listener : mListeners) {
    303             try {
    304                 listener.testModuleEnded();
    305             } catch (RuntimeException e) {
    306                 CLog.e("Exception while invoking testModuleEnded");
    307                 CLog.e(e);
    308             }
    309         }
    310     }
    311 }
    312