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