1 /* 2 * Copyright (C) 2017 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.config.OptionSetter; 20 import com.android.tradefed.util.CommandResult; 21 import com.android.tradefed.util.CommandStatus; 22 import com.android.tradefed.util.IRunUtil; 23 24 import org.easymock.EasyMock; 25 import org.junit.Before; 26 import org.junit.Test; 27 28 /** Unit test for {@link RunHostCommandTargetPreparer}. */ 29 public final class RunHostCommandTargetPreparerTest { 30 31 private RunHostCommandTargetPreparer mPreparer; 32 private IRunUtil mRunUtil; 33 34 @Before 35 public void setUp() { 36 mRunUtil = EasyMock.createMock(IRunUtil.class); 37 mPreparer = 38 new RunHostCommandTargetPreparer() { 39 @Override 40 protected IRunUtil getRunUtil() { 41 return mRunUtil; 42 } 43 }; 44 } 45 46 @Test 47 public void testSetUp() throws Exception { 48 final String command = "command \t\t\t \t argument"; 49 final String[] commandArray = new String[] {"command", "argument"}; 50 51 final OptionSetter optionSetter = new OptionSetter(mPreparer); 52 optionSetter.setOptionValue("host-setup-command", command); 53 optionSetter.setOptionValue("host-cmd-timeout", "10"); 54 55 CommandResult result = new CommandResult(CommandStatus.SUCCESS); 56 result.setStdout(""); 57 result.setStderr(""); 58 59 EasyMock.expect(mRunUtil.runTimedCmd(10L, commandArray)).andReturn(result); 60 61 EasyMock.replay(mRunUtil); 62 mPreparer.setUp(null, null); 63 EasyMock.verify(mRunUtil); 64 } 65 66 @Test 67 public void testTearDown() throws Exception { 68 final String command = "command \t\t\t \t argument"; 69 final String[] commandArray = new String[] {"command", "argument"}; 70 71 final OptionSetter optionSetter = new OptionSetter(mPreparer); 72 optionSetter.setOptionValue("host-teardown-command", command); 73 optionSetter.setOptionValue("host-cmd-timeout", "10"); 74 75 CommandResult result = new CommandResult(CommandStatus.SUCCESS); 76 result.setStdout(""); 77 result.setStderr(""); 78 79 EasyMock.expect(mRunUtil.runTimedCmd(10L, commandArray)).andReturn(result); 80 81 EasyMock.replay(mRunUtil); 82 mPreparer.tearDown(null, null, null); 83 EasyMock.verify(mRunUtil); 84 } 85 } 86