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 org.eclipse.jdt.internal.junit.runner.IVisitsTestTrees;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 /**
25  * Reference for an Android test suite aka class.
26  */
27 @SuppressWarnings("restriction")
28 class TestSuiteReference extends AndroidTestReference {
29 
30     private final String mClassName;
31     private List<AndroidTestReference> mTests;
32 
33     /**
34      * Creates a TestSuiteReference
35      *
36      * @param className the fully qualified name of the test class
37      */
TestSuiteReference(String className)38     TestSuiteReference(String className) {
39          mClassName = className;
40          mTests = new ArrayList<AndroidTestReference>();
41     }
42 
43     /**
44      * Returns a count of the number of test cases included in this suite.
45      */
46     @Override
countTestCases()47     public int countTestCases() {
48         return mTests.size();
49     }
50 
51     /**
52      * Sends test identifier and test count information for this test class, and all its included
53      * test methods.
54      *
55      * @param notified the {@link IVisitsTestTrees} to send test info too
56      */
57     @Override
sendTree(IVisitsTestTrees notified)58     public void sendTree(IVisitsTestTrees notified) {
59         notified.visitTreeEntry(getIdentifier(), true, countTestCases());
60         for (AndroidTestReference ref: mTests) {
61             ref.sendTree(notified);
62         }
63     }
64 
65     /**
66      * Return the name of this test class.
67      */
68     @Override
getName()69     public String getName() {
70         return mClassName;
71     }
72 
73     /**
74      * Adds a test method to this suite.
75      *
76      * @param testRef the {@link TestCaseReference} to add
77      */
addTest(AndroidTestReference testRef)78     void addTest(AndroidTestReference testRef) {
79         mTests.add(testRef);
80     }
81 
82     /** Returns the test suite of given name, null if no such test suite exists */
getTestSuite(String name)83     public TestSuiteReference getTestSuite(String name) {
84         for (AndroidTestReference ref: mTests) {
85             if (ref instanceof TestSuiteReference && ref.getName().equals(name)) {
86                 return (TestSuiteReference) ref;
87             }
88         }
89 
90         return null;
91     }
92 }
93