Home | History | Annotate | Download | only in targetprep
      1 /*
      2  * Copyright (C) 2014 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.targetprep;
     18 
     19 import com.android.tradefed.build.IBuildInfo;
     20 import com.android.tradefed.config.IConfiguration;
     21 import com.android.tradefed.config.IConfigurationReceiver;
     22 import com.android.tradefed.config.Option;
     23 import com.android.tradefed.device.DeviceNotAvailableException;
     24 import com.android.tradefed.device.ITestDevice;
     25 
     26 import java.util.ArrayList;
     27 import java.util.Collection;
     28 
     29 /**
     30  * Allows for running tearDown on preparers that are included in a config as an object.
     31  * <p/>
     32  * When a preparer is included as an object in a config rather than an {@link ITargetPreparer},
     33  * it's tearDown will not be called. Referencing it from this preparer will make sure that it's tearDown will be called.
     34  */
     35 public class TearDownPassThroughPreparer implements IConfigurationReceiver, ITargetCleaner {
     36     private IConfiguration mConfiguration;
     37 
     38     @Option(name = "preparer", description = "names of preparers to tearDown")
     39     private Collection<String> mPreparers = new ArrayList<String>();
     40 
     41     /**
     42      * {@inheritDoc}
     43      */
     44     @Override
     45     public void setConfiguration(IConfiguration configuration) {
     46         mConfiguration = configuration;
     47     }
     48 
     49     /**
     50      * {@inheritDoc}
     51      */
     52     @Override
     53     public void setUp(ITestDevice device, IBuildInfo buildInfo) {
     54         //setUp is taken care of elsewhere
     55     }
     56 
     57     /**
     58      * {@inheritDoc}
     59      */
     60     @Override
     61     public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)
     62             throws DeviceNotAvailableException {
     63         for (String preparer : mPreparers) {
     64             Object configObject = mConfiguration.getConfigurationObject(preparer);
     65             if (configObject instanceof ITargetCleaner) {
     66                 ITargetCleaner cleaner = (ITargetCleaner)configObject;
     67                 cleaner.tearDown(device, buildInfo, e);
     68             }
     69             if (configObject instanceof IHostCleaner) {
     70                 IHostCleaner cleaner = (IHostCleaner)configObject;
     71                 cleaner.cleanUp(buildInfo, e);
     72             }
     73         }
     74     }
     75 }
     76