Home | History | Annotate | Download | only in util
      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.compatibility.common.util;
     18 
     19 import com.android.ddmlib.Log;
     20 import com.android.tradefed.build.IBuildInfo;
     21 import com.android.tradefed.device.ITestDevice;
     22 import com.android.tradefed.log.LogUtil;
     23 
     24 import java.lang.reflect.Method;
     25 import java.util.Arrays;
     26 import java.util.List;
     27 
     28 /**
     29  * Execute business logic methods for host side test cases
     30  */
     31 public class BusinessLogicHostExecutor extends BusinessLogicExecutor {
     32 
     33     private ITestDevice mDevice;
     34     private IBuildInfo mBuild;
     35     private Object mTestObj;
     36 
     37     public BusinessLogicHostExecutor(ITestDevice device, IBuildInfo build, Object testObj) {
     38         mDevice = device;
     39         mBuild = build;
     40         mTestObj = testObj;
     41     }
     42 
     43     /**
     44      * {@inheritDoc}
     45      */
     46     @Override
     47     protected Object getTestObject() {
     48         return mTestObj;
     49     }
     50 
     51     /**
     52      * {@inheritDoc}
     53      */
     54     @Override
     55     public void logInfo(String format, Object... args) {
     56         LogUtil.printLog(Log.LogLevel.INFO, LOG_TAG, String.format(format, args));
     57     }
     58 
     59     /**
     60      * {@inheritDoc}
     61      */
     62     @Override
     63     protected ResolvedMethod getResolvedMethod(Class cls, String methodName, String... args)
     64             throws ClassNotFoundException {
     65         List<Method> nameMatches = getMethodsWithName(cls, methodName);
     66         for (Method m : nameMatches) {
     67             ResolvedMethod rm = new ResolvedMethod(m);
     68             int paramTypesMatched = 0;
     69             int argsUsed = 0;
     70             Class[] paramTypes = m.getParameterTypes();
     71             for (Class paramType : paramTypes) {
     72                 if (argsUsed == args.length && paramType.equals(String.class)) {
     73                     // We've used up all supplied string args, so this method will not match.
     74                     // If paramType is the ITestDevice or IBuildInfo class, we can match a
     75                     // paramType without needing more string args. similarly, paramType "String[]"
     76                     // can be matched with zero string args. If we add support for more paramTypes,
     77                     // this logic may require adjustment.
     78                     break;
     79                 }
     80                 if (paramType.equals(String.class)) {
     81                     // Type "String" -- supply the next available arg
     82                     rm.addArg(args[argsUsed++]);
     83                 } else if (ITestDevice.class.isAssignableFrom(paramType)) {
     84                     rm.addArg(mDevice);
     85                 } else if (IBuildInfo.class.isAssignableFrom(paramType)) {
     86                     rm.addArg(mBuild);
     87                 } else if (paramType.equals(Class.forName(STRING_ARRAY_CLASS))) {
     88                     // Type "String[]" (or "String...") -- supply all remaining args
     89                     rm.addArg(Arrays.copyOfRange(args, argsUsed, args.length));
     90                     argsUsed += (args.length - argsUsed);
     91                 } else {
     92                     break; // Param type is unrecognized, this method will not match.
     93                 }
     94                 paramTypesMatched++; // A param type has been matched when reaching this point.
     95             }
     96             if (paramTypesMatched == paramTypes.length && argsUsed == args.length) {
     97                 return rm; // Args match, methods match, so return the first method-args pairing.
     98             }
     99             // Not a match, try args for next method that matches by name.
    100         }
    101         throw new RuntimeException(String.format(
    102                 "BusinessLogic: Failed to invoke action method %s with args: %s", methodName,
    103                 Arrays.toString(args)));
    104     }
    105 }
    106