1 /* 2 * Copyright (C) 2019 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.testtype.mobly; 18 19 /** Mobly yaml result handler factory which generates appropriate handler based on result type. */ 20 public class MoblyYamlResultHandlerFactory { 21 getHandler(String resultType)22 public IMoblyYamlResultHandler getHandler(String resultType) 23 throws InvalidResultTypeException, IllegalAccessException, InstantiationException { 24 IMoblyYamlResultHandler resultHandler; 25 switch (resultType) { 26 case "Record": 27 resultHandler = Type.RECORD.getHandlerInstance(); 28 break; 29 case "UserData": 30 resultHandler = Type.USER_DATA.getHandlerInstance(); 31 break; 32 case "TestNameList": 33 resultHandler = Type.TEST_NAME_LIST.getHandlerInstance(); 34 break; 35 case "ControllerInfo": 36 resultHandler = Type.CONTROLLER_INFO.getHandlerInstance(); 37 break; 38 case "Summary": 39 resultHandler = Type.SUMMARY.getHandlerInstance(); 40 break; 41 default: 42 throw new InvalidResultTypeException( 43 "Invalid mobly test result type: " + resultType); 44 } 45 return resultHandler; 46 } 47 48 public class InvalidResultTypeException extends Exception { InvalidResultTypeException(String errorMsg)49 public InvalidResultTypeException(String errorMsg) { 50 super(errorMsg); 51 } 52 } 53 54 public enum Type { 55 RECORD("Record", MoblyYamlResultRecordHandler.class), 56 USER_DATA("UserData", MoblyYamlResultUserDataHandler.class), 57 TEST_NAME_LIST("TestNameList", MoblyYamlResultTestNameListHandler.class), 58 CONTROLLER_INFO("ControllerInfo", MoblyYamlResultControllerInfoHandler.class), 59 SUMMARY("Summary", MoblyYamlResultSummaryHandler.class); 60 61 private String tag; 62 private Class handlerClass; 63 Type(String tag, Class handlerClass)64 Type(String tag, Class handlerClass) { 65 this.tag = tag; 66 this.handlerClass = handlerClass; 67 } 68 getTag()69 public String getTag() { 70 return tag; 71 } 72 getHandlerInstance()73 public <T> T getHandlerInstance() throws IllegalAccessException, InstantiationException { 74 return (T) handlerClass.newInstance(); 75 } 76 } 77 } 78