TelephonyTestUtils.java revision aeb892fb006035636dd0959ad4e4f9cb106ac565
1/*
2 * Copyright (C) 2015 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
17package com.android.internal.telephony;
18
19import android.telephony.SmsCbMessage;
20
21import com.android.internal.telephony.cdma.SmsMessage;
22
23import java.lang.reflect.Constructor;
24import java.lang.reflect.InvocationTargetException;
25import java.lang.reflect.Method;
26import junit.framework.Assert;
27
28/**
29 * This class provides reflection for classes/methods that are not accessible from tests.
30 * Convention for helper function naming is: classNameFunctionName()
31 */
32public class TelephonyTestUtils {
33
34    /**
35     * This function calls constructor that takes in params.
36     * This function does not work if any of the parameter passed in is null because
37     * params[i].getClass() fails. In that case it needs to be handled by the caller (it knows the
38     * param types so getClass() is not needed)
39     */
40    public static Object createNewInstance(String className, Object... params) {
41        try {
42            Class clazz = Class.forName(className);
43            int numParam = params.length;
44            Class<?>[] paramType = new Class[numParam];
45            for (int i = 0; i < numParam; i++) {
46                paramType[i] = params[i].getClass();
47            }
48            Constructor constructor = clazz.getDeclaredConstructor(paramType);
49            constructor.setAccessible(true);
50
51            return constructor.newInstance(params);
52        } catch (Exception e) {
53            Assert.fail(e.toString());
54            return null;
55        }
56    }
57
58    /**
59     * This function does not work if any of the parameter passed in is null because
60     * params[i].getClass() fails. In that case it needs to be handled by the caller (it knows the
61     * param types so getClass() is not needed)
62     */
63    public static Object invokeStaticMethod(Class<?> clazz, String method, Object... params) {
64        try {
65            int numParam = params.length;
66            Class<?>[] paramType = new Class[numParam];
67            for (int i = 0; i < numParam; i++) {
68                paramType[i] = params[i].getClass();
69            }
70            Method methodReflection = clazz.getDeclaredMethod(method, paramType);
71            methodReflection.setAccessible(true);
72            return methodReflection.invoke(null, params);
73        } catch (Exception e) {
74            Assert.fail(e.toString());
75            return null;
76        }
77    }
78
79    /**
80     * This is needed when the test expects the method in source being called to throw an exception.
81     * Throwable will be an instanceof the expected exception.
82     * This function does not work if any of the parameter passed in is null because
83     * params[i].getClass() fails. In that case it needs to be handled by the caller (it knows the
84     * param types so getClass() is not needed)
85     */
86    public static Object invokeStaticMethodThrowsException(Class<?> clazz, String method,
87                                                           Object... params) throws Throwable {
88        try {
89            int numParam = params.length;
90            Class<?>[] paramType = new Class[numParam];
91            for (int i = 0; i < numParam; i++) {
92                paramType[i] = params[i].getClass();
93            }
94            Method methodReflection = clazz.getDeclaredMethod(method, paramType);
95            methodReflection.setAccessible(true);
96            return methodReflection.invoke(null, params);
97        } catch (InvocationTargetException e) {
98            throw e.getTargetException();
99        } catch (Exception e) {
100            Assert.fail(e.toString());
101            return null;
102        }
103    }
104
105    /**
106     * This function does not work if any of the parameter passed in is null because
107     * params[i].getClass() fails. In that case it needs to be handled by the caller (it knows the
108     * param types so getClass() is not needed)
109     */
110    public static Object invokeNonStaticMethod(Class<?> clazz, Object caller, String method,
111                                               Object... params) {
112        try {
113            int numParam = params.length;
114            Class<?>[] paramType = new Class[numParam];
115            for (int i = 0; i < numParam; i++) {
116                paramType[i] = params[i].getClass();
117            }
118            Method methodReflection = clazz.getDeclaredMethod(method, paramType);
119            methodReflection.setAccessible(true);
120            return methodReflection.invoke(caller, params);
121        } catch (Exception e) {
122            Assert.fail(e.toString());
123            return null;
124        }
125    }
126
127    /**
128     * This is needed when the test expects the method in source being called to throw an exception.
129     * Throwable will be an instanceof the expected exception.
130     * This function does not work if any of the parameter passed in is null because
131     * params[i].getClass() fails. In that case it needs to be handled by the caller (it knows the
132     * param types so getClass() is not needed)
133     */
134    public static Object invokeNonStaticMethodThrowsException(Class<?> clazz, Object caller,
135                                                              String method, Object... params)
136            throws Throwable {
137        try {
138            int numParam = params.length;
139            Class<?>[] paramType = new Class[numParam];
140            for (int i = 0; i < numParam; i++) {
141                paramType[i] = params[i].getClass();
142            }
143            Method methodReflection = clazz.getDeclaredMethod(method, paramType);
144            methodReflection.setAccessible(true);
145            return methodReflection.invoke(caller, params);
146        } catch (InvocationTargetException e) {
147            throw e.getTargetException();
148        } catch (Exception e) {
149            Assert.fail(e.toString());
150            return null;
151        }
152    }
153}
154