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 * 32 * <p>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 34 * tearDown will be called. 35 */ 36 public class TearDownPassThroughPreparer extends BaseTargetPreparer 37 implements IConfigurationReceiver, ITargetCleaner { 38 private IConfiguration mConfiguration; 39 40 @Option(name = "preparer", description = "names of preparers to tearDown") 41 private Collection<String> mPreparers = new ArrayList<String>(); 42 43 /** 44 * {@inheritDoc} 45 */ 46 @Override 47 public void setConfiguration(IConfiguration configuration) { 48 mConfiguration = configuration; 49 } 50 51 /** 52 * {@inheritDoc} 53 */ 54 @Override 55 public void setUp(ITestDevice device, IBuildInfo buildInfo) { 56 //setUp is taken care of elsewhere 57 } 58 59 /** 60 * {@inheritDoc} 61 */ 62 @Override 63 public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e) 64 throws DeviceNotAvailableException { 65 for (String preparer : mPreparers) { 66 Object configObject = mConfiguration.getConfigurationObject(preparer); 67 if (configObject instanceof ITargetCleaner) { 68 ITargetCleaner cleaner = (ITargetCleaner)configObject; 69 cleaner.tearDown(device, buildInfo, e); 70 } 71 if (configObject instanceof IHostCleaner) { 72 IHostCleaner cleaner = (IHostCleaner)configObject; 73 cleaner.cleanUp(buildInfo, e); 74 } 75 } 76 } 77 } 78