Home | History | Annotate | Download | only in result
      1 /*
      2  * Copyright (C) 2012 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 
     17 package com.android.tradefed.result;
     18 
     19 import com.android.tradefed.invoker.IInvocationContext;
     20 import com.android.tradefed.metrics.proto.MetricMeasurement.Metric;
     21 
     22 import java.util.HashMap;
     23 
     24 /**
     25  * A proxy listener to translate test method, class, and package names as results are reported.
     26  */
     27 public abstract class NameMangleListener implements ITestInvocationListener {
     28     private final ITestInvocationListener mListener;
     29 
     30     public NameMangleListener(ITestInvocationListener listener) {
     31         if (listener == null) throw new NullPointerException();
     32         mListener = listener;
     33     }
     34 
     35     /**
     36      * This method is run on all {@link TestDescription}s that are passed to the {@link
     37      * #testStarted(TestDescription)}, {@link #testFailed(TestDescription, String)}, and {@link
     38      * #testEnded(TestDescription, HashMap)} callbacks. The method should return a
     39      * possibly-different {@link TestDescription} that will be passed to the downstream {@link
     40      * ITestInvocationListener} that was specified during construction.
     41      *
     42      * <p>The implementation should be careful to not modify the original {@link TestDescription}.
     43      *
     44      * <p>The default implementation passes the incoming identifier through unmodified.
     45      */
     46     protected TestDescription mangleTestId(TestDescription test) {
     47         return test;
     48     }
     49 
     50     /**
     51      * This method is run on all test run names that are passed to the
     52      * {@link #testRunStarted(String, int)} callback.  The method should return a possibly-different
     53      * test run name that will be passed to the downstream {@link ITestInvocationListener} that was
     54      * specified during construction.
     55      * <p />
     56      * The implementation should be careful to not modify the original run name.
     57      * <p />
     58      * The default implementation passes the incoming test run name through unmodified.
     59      */
     60     protected String mangleTestRunName(String name) {
     61         return name;
     62     }
     63 
     64     // ITestLifeCycleReceiver methods
     65 
     66     /** {@inheritDoc} */
     67     @Override
     68     public void testEnded(TestDescription test, HashMap<String, Metric> testMetrics) {
     69         final TestDescription mangledTestId = mangleTestId(test);
     70         mListener.testEnded(mangledTestId, testMetrics);
     71     }
     72 
     73     /** {@inheritDoc} */
     74     @Override
     75     public void testFailed(TestDescription test, String trace) {
     76         final TestDescription mangledTestId = mangleTestId(test);
     77         mListener.testFailed(mangledTestId, trace);
     78     }
     79 
     80     /** {@inheritDoc} */
     81     @Override
     82     public void testAssumptionFailure(TestDescription test, String trace) {
     83         final TestDescription mangledTestId = mangleTestId(test);
     84         mListener.testAssumptionFailure(mangledTestId, trace);
     85     }
     86 
     87     /** {@inheritDoc} */
     88     @Override
     89     public void testIgnored(TestDescription test) {
     90         final TestDescription mangledTestId = mangleTestId(test);
     91         mListener.testIgnored(mangledTestId);
     92     }
     93 
     94     /** {@inheritDoc} */
     95     @Override
     96     public void testRunEnded(long elapsedTime, HashMap<String, Metric> runMetrics) {
     97         mListener.testRunEnded(elapsedTime, runMetrics);
     98     }
     99 
    100     /**
    101      * {@inheritDoc}
    102      */
    103     @Override
    104     public void testRunFailed(String errorMessage) {
    105         mListener.testRunFailed(errorMessage);
    106     }
    107 
    108     /**
    109      * {@inheritDoc}
    110      */
    111     @Override
    112     public void testRunStarted(String runName, int testCount) {
    113         final String mangledName = mangleTestRunName(runName);
    114         mListener.testRunStarted(mangledName, testCount);
    115     }
    116 
    117     /**
    118      * {@inheritDoc}
    119      */
    120     @Override
    121     public void testRunStopped(long elapsedTime) {
    122         mListener.testRunStopped(elapsedTime);
    123     }
    124 
    125     /** {@inheritDoc} */
    126     @Override
    127     public void testStarted(TestDescription test) {
    128         final TestDescription mangledTestId = mangleTestId(test);
    129         mListener.testStarted(mangledTestId);
    130     }
    131 
    132 
    133     // ITestInvocationListener methods
    134     /**
    135      * {@inheritDoc}
    136      */
    137     @Override
    138     public void invocationStarted(IInvocationContext context) {
    139         mListener.invocationStarted(context);
    140     }
    141 
    142     /**
    143      * {@inheritDoc}
    144      */
    145     @Override
    146     public void testLog(String dataName, LogDataType dataType, InputStreamSource dataStream) {
    147         mListener.testLog(dataName, dataType, dataStream);
    148     }
    149 
    150     /**
    151      * {@inheritDoc}
    152      */
    153     @Override
    154     public void invocationEnded(long elapsedTime) {
    155         mListener.invocationEnded(elapsedTime);
    156     }
    157 
    158     /**
    159      * {@inheritDoc}
    160      */
    161     @Override
    162     public void invocationFailed(Throwable cause) {
    163         mListener.invocationFailed(cause);
    164     }
    165 
    166     /**
    167      * {@inheritDoc}
    168      */
    169     @Override
    170     public TestSummary getSummary() {
    171         return mListener.getSummary();
    172     }
    173 }
    174 
    175