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 
17 package com.android.server.wifi;
18 
19 import androidx.test.filters.SmallTest;
20 
21 import java.lang.annotation.Annotation;
22 import java.lang.reflect.Method;
23 
24 /**
25  * Generial Utilities for Wifi tests
26  */
27 @SmallTest
28 public class WifiTestUtil {
29 
30     /**
31      * Walk up the stack and find the first method annotated with @Test
32      * Note: this will evaluate all overloads with the method name for the @Test annotation
33      */
getTestMethod()34     public static String getTestMethod() {
35         StackTraceElement[] stack = Thread.currentThread().getStackTrace();
36         for (StackTraceElement e : stack) {
37             if (e.isNativeMethod()) {
38                 continue;
39             }
40             Class clazz;
41             try {
42                 clazz = Class.forName(e.getClassName());
43             } catch (ClassNotFoundException ex) {
44                 throw new RuntimeException("Could not find class from stack", ex);
45             }
46             Method[] methods = clazz.getDeclaredMethods();
47             for (Method method : methods) {
48                 if (method.getName().equals(e.getMethodName())) {
49                     Annotation[] annotations = method.getDeclaredAnnotations();
50                     for (Annotation annotation : annotations) {
51                         if (annotation.annotationType().equals(org.junit.Test.class)) {
52                             return e.getClassName() + "#" + e.getMethodName();
53                         }
54                     }
55                 }
56             }
57         }
58         throw new RuntimeException("Could not find a test method in the stack");
59     }
60 }
61