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     // MoblyYamlResultHandlerFactory won't be serialized so suppress serial warning.
49     @SuppressWarnings("serial")
50     public class InvalidResultTypeException extends Exception {
InvalidResultTypeException(String errorMsg)51         public InvalidResultTypeException(String errorMsg) {
52             super(errorMsg);
53         }
54     }
55 
56     public enum Type {
57         RECORD("Record", MoblyYamlResultRecordHandler.class),
58         USER_DATA("UserData", MoblyYamlResultUserDataHandler.class),
59         TEST_NAME_LIST("TestNameList", MoblyYamlResultTestNameListHandler.class),
60         CONTROLLER_INFO("ControllerInfo", MoblyYamlResultControllerInfoHandler.class),
61         SUMMARY("Summary", MoblyYamlResultSummaryHandler.class);
62 
63         private String tag;
64         private Class<?> handlerClass;
65 
Type(String tag, Class<?> handlerClass)66         Type(String tag, Class<?> handlerClass) {
67             this.tag = tag;
68             this.handlerClass = handlerClass;
69         }
70 
getTag()71         public String getTag() {
72             return tag;
73         }
74 
getHandlerInstance()75         public <T> T getHandlerInstance() throws IllegalAccessException, InstantiationException {
76             return (T) handlerClass.newInstance();
77         }
78     }
79 }
80