• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 util.build;
18 
19 import java.lang.reflect.Method;
20 import java.util.ArrayList;
21 import java.util.LinkedHashMap;
22 import java.util.List;
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestResult;
26 import junit.textui.TestRunner;
27 
28 public class JUnitTestCollector {
29 
30     public final int testClassCnt;
31     public final int testMethodsCnt;
32 
33     /**
34      * Map collection all found tests.
35      *
36      * using a linked hashmap to keep the insertion order for iterators.
37      * the junit suite/tests adding order is used to generate the order of the
38      * report.
39      * a map. key: fully qualified class name, value: a list of test methods for
40      * the given class
41      */
42     public final LinkedHashMap<String, List<String>> map =
43             new LinkedHashMap<String, List<String>>();
44 
JUnitTestCollector(ClassLoader loader)45     public JUnitTestCollector(ClassLoader loader) {
46         Test test;
47         try {
48             Class<?> allTestsClass = loader.loadClass("dot.junit.AllTests");
49             Method suiteMethod = allTestsClass.getDeclaredMethod("suite");
50             test = (Test)suiteMethod.invoke(null);
51         } catch (Exception e) {
52             throw new RuntimeException(e);
53         }
54 
55         final Counters counters = new Counters();
56         new TestRunner() {
57             @Override
58             protected TestResult createTestResult() {
59                 return new TestResult() {
60                     @Override
61                     protected void run(TestCase test) {
62                         String packageName = test.getClass().getPackage().getName();
63                         packageName = packageName.substring(packageName.lastIndexOf('.'));
64 
65 
66                         String method = test.getName(); // e.g. testVFE2
67                         String fqcn = test.getClass().getName(); // e.g.
68                         // dxc.junit.opcodes.iload_3.Test_iload_3
69 
70                         counters.a++;
71                         List<String> li = map.get(fqcn);
72                         if (li == null) {
73                             counters.b++;
74                             li = new ArrayList<String>();
75                             map.put(fqcn, li);
76                         }
77                         li.add(method);
78                     }
79 
80                 };
81             }
82         }.doRun(test);
83         testMethodsCnt = counters.a;
84         testClassCnt = counters.b;
85     }
86 
87     private static class Counters {
88         int a = 0;
89         int b = 0;
90     }
91 }
92