• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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.ide.eclipse.adt.internal.launch.junit.runtime;
18 
19 import com.android.ddmlib.testrunner.ITestRunListener;
20 import com.android.ddmlib.testrunner.TestIdentifier;
21 
22 import org.eclipse.jdt.internal.junit.runner.ITestReference;
23 import org.eclipse.jdt.internal.junit.runner.IVisitsTestTrees;
24 
25 import java.util.HashMap;
26 import java.util.Map;
27 
28 /**
29  * Collects info about tests to be executed by listening to the results of an Android test run.
30  */
31 @SuppressWarnings("restriction")
32 class TestCollector implements ITestRunListener {
33 
34     private int mTotalTestCount;
35     /** test name to test suite reference map. */
36     private Map<String, TestSuiteReference> mTestTree;
37     private String mErrorMessage = null;
38 
TestCollector()39     TestCollector() {
40         mTotalTestCount = 0;
41         mTestTree = new HashMap<String, TestSuiteReference>();
42     }
43 
testEnded(TestIdentifier test, Map<String, String> testMetrics)44     public void testEnded(TestIdentifier test, Map<String, String> testMetrics) {
45         // ignore
46     }
47 
48     /* (non-Javadoc)
49      * @see com.android.ddmlib.testrunner.ITestRunListener#testFailed(com.android.ddmlib.testrunner.ITestRunListener.TestFailure, com.android.ddmlib.testrunner.TestIdentifier, java.lang.String)
50      */
testFailed(TestFailure status, TestIdentifier test, String trace)51     public void testFailed(TestFailure status, TestIdentifier test, String trace) {
52         // ignore - should be impossible since this is only collecting test information
53     }
54 
55     /* (non-Javadoc)
56      * @see com.android.ddmlib.testrunner.ITestRunListener#testRunEnded(long, Map<String, String>)
57      */
testRunEnded(long elapsedTime, Map<String, String> runMetrics)58     public void testRunEnded(long elapsedTime, Map<String, String> runMetrics) {
59         // ignore
60     }
61 
62     /* (non-Javadoc)
63      * @see com.android.ddmlib.testrunner.ITestRunListener#testRunFailed(java.lang.String)
64      */
testRunFailed(String errorMessage)65     public void testRunFailed(String errorMessage) {
66         mErrorMessage = errorMessage;
67     }
68 
69     /* (non-Javadoc)
70      * @see com.android.ddmlib.testrunner.ITestRunListener#testRunStarted(int)
71      */
testRunStarted(String ignoredRunName, int testCount)72     public void testRunStarted(String ignoredRunName, int testCount) {
73         mTotalTestCount = testCount;
74     }
75 
76     /* (non-Javadoc)
77      * @see com.android.ddmlib.testrunner.ITestRunListener#testRunStopped(long)
78      */
testRunStopped(long elapsedTime)79     public void testRunStopped(long elapsedTime) {
80         // ignore
81     }
82 
83     /* (non-Javadoc)
84      * @see com.android.ddmlib.testrunner.ITestRunListener#testStarted(com.android.ddmlib.testrunner.TestIdentifier)
85      */
testStarted(TestIdentifier test)86     public void testStarted(TestIdentifier test) {
87         TestSuiteReference suiteRef = mTestTree.get(test.getClassName());
88         if (suiteRef == null) {
89             // this test suite has not been seen before, create it
90             suiteRef = new TestSuiteReference(test.getClassName());
91             mTestTree.put(test.getClassName(), suiteRef);
92         }
93         suiteRef.addTest(new TestCaseReference(test));
94     }
95 
96     /**
97      * Returns the total test count in the test run.
98      */
getTestCaseCount()99     public int getTestCaseCount() {
100         return mTotalTestCount;
101     }
102 
103     /**
104      * Sends info about the test tree to be executed (ie the suites and their enclosed tests)
105      *
106      * @param notified the {@link IVisitsTestTrees} to send test data to
107      */
sendTrees(IVisitsTestTrees notified)108     public void sendTrees(IVisitsTestTrees notified) {
109         for (ITestReference ref : mTestTree.values()) {
110             ref.sendTree(notified);
111         }
112     }
113 
114     /**
115      * Returns the error message that was reported when collecting test info.
116      * Returns <code>null</code> if no error occurred.
117      */
getErrorMessage()118     public String getErrorMessage() {
119         return mErrorMessage;
120     }
121 }
122