• 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 
44     /* (non-Javadoc)
45      * @see com.android.ddmlib.testrunner.ITestRunListener#testEnded(com.android.ddmlib.testrunner.TestIdentifier)
46      */
testEnded(TestIdentifier test)47     public void testEnded(TestIdentifier test) {
48         // ignore
49     }
50 
51     /* (non-Javadoc)
52      * @see com.android.ddmlib.testrunner.ITestRunListener#testFailed(com.android.ddmlib.testrunner.ITestRunListener.TestFailure, com.android.ddmlib.testrunner.TestIdentifier, java.lang.String)
53      */
testFailed(TestFailure status, TestIdentifier test, String trace)54     public void testFailed(TestFailure status, TestIdentifier test, String trace) {
55         // ignore - should be impossible since this is only collecting test information
56     }
57 
58     /* (non-Javadoc)
59      * @see com.android.ddmlib.testrunner.ITestRunListener#testRunEnded(long)
60      */
testRunEnded(long elapsedTime)61     public void testRunEnded(long elapsedTime) {
62         // ignore
63     }
64 
65     /* (non-Javadoc)
66      * @see com.android.ddmlib.testrunner.ITestRunListener#testRunFailed(java.lang.String)
67      */
testRunFailed(String errorMessage)68     public void testRunFailed(String errorMessage) {
69         mErrorMessage = errorMessage;
70     }
71 
72     /* (non-Javadoc)
73      * @see com.android.ddmlib.testrunner.ITestRunListener#testRunStarted(int)
74      */
testRunStarted(int testCount)75     public void testRunStarted(int testCount) {
76         mTotalTestCount = testCount;
77     }
78 
79     /* (non-Javadoc)
80      * @see com.android.ddmlib.testrunner.ITestRunListener#testRunStopped(long)
81      */
testRunStopped(long elapsedTime)82     public void testRunStopped(long elapsedTime) {
83         // ignore
84     }
85 
86     /* (non-Javadoc)
87      * @see com.android.ddmlib.testrunner.ITestRunListener#testStarted(com.android.ddmlib.testrunner.TestIdentifier)
88      */
testStarted(TestIdentifier test)89     public void testStarted(TestIdentifier test) {
90         TestSuiteReference suiteRef = mTestTree.get(test.getClassName());
91         if (suiteRef == null) {
92             // this test suite has not been seen before, create it
93             suiteRef = new TestSuiteReference(test.getClassName());
94             mTestTree.put(test.getClassName(), suiteRef);
95         }
96         suiteRef.addTest(new TestCaseReference(test));
97     }
98 
99     /**
100      * Returns the total test count in the test run.
101      */
getTestCaseCount()102     public int getTestCaseCount() {
103         return mTotalTestCount;
104     }
105 
106     /**
107      * Sends info about the test tree to be executed (ie the suites and their enclosed tests)
108      *
109      * @param notified the {@link IVisitsTestTrees} to send test data to
110      */
sendTrees(IVisitsTestTrees notified)111     public void sendTrees(IVisitsTestTrees notified) {
112         for (ITestReference ref : mTestTree.values()) {
113             ref.sendTree(notified);
114         }
115     }
116 
117     /**
118      * Returns the error message that was reported when collecting test info.
119      * Returns <code>null</code> if no error occurred.
120      */
getErrorMessage()121     public String getErrorMessage() {
122         return mErrorMessage;
123     }
124 }
125