TestSuite.java revision 3c64686bc8fe2d681500c432ae1a6b4602c83f77
1/*
2 * Copyright (C) 2011 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 */
16package com.android.cts.tradefed.result;
17
18import com.android.tradefed.result.TestResult;
19
20import org.kxml2.io.KXmlSerializer;
21
22import java.io.IOException;
23import java.util.LinkedHashMap;
24import java.util.List;
25import java.util.Map;
26
27/**
28 * Data structure that represents a "TestSuite" XML element and its children.
29 */
30class TestSuite {
31
32    static final String TAG = "TestSuite";
33
34    private String mName;
35
36    // use linked hash map for predictable iteration order
37    Map<String, TestSuite> mChildSuiteMap = new LinkedHashMap<String, TestSuite>();
38    Map<String, TestCase> mChildTestCaseMap = new LinkedHashMap<String, TestCase>();
39
40    /**
41     * @param testSuite
42     */
43    public TestSuite(String suiteName) {
44        mName = suiteName;
45    }
46
47    public TestSuite() {
48    }
49
50    /**
51     * @return the name of this suite
52     */
53    public String getName() {
54        return mName;
55    }
56
57    /**
58     * Set the name of this suite
59     */
60    public void setName(String name) {
61        mName = name;
62    }
63
64    /**
65     * Insert the given test result into this suite.
66     *
67     * @param suiteNames list of remaining suite names for this test
68     * @param testClassName the test class name
69     * @param testName the test method name
70     * @param testResult the {@link TestResult}
71     */
72    public void insertTest(List<String> suiteNames, String testClassName, String testName,
73            TestResult testResult) {
74        if (suiteNames.size() <= 0) {
75            // no more package segments
76            TestCase testCase = getTestCase(testClassName);
77            testCase.insertTest(testName, testResult);
78        } else {
79            String rootName = suiteNames.remove(0);
80            TestSuite suite = getTestSuite(rootName);
81            suite.insertTest(suiteNames, testClassName, testName, testResult);
82        }
83    }
84
85    /**
86     * Get the child {@link TestSuite} with given name, creating if necessary.
87     *
88     * @param suiteName
89     * @return the {@link TestSuite}
90     */
91    private TestSuite getTestSuite(String suiteName) {
92        TestSuite testSuite = mChildSuiteMap.get(suiteName);
93        if (testSuite == null) {
94            testSuite = new TestSuite(suiteName);
95            mChildSuiteMap.put(suiteName, testSuite);
96        }
97        return testSuite;
98    }
99
100    /**
101     * Get the child {@link TestCase} with given name, creating if necessary.
102     * @param testCaseName
103     * @return
104     */
105    private TestCase getTestCase(String testCaseName) {
106        TestCase testCase = mChildTestCaseMap.get(testCaseName);
107        if (testCase == null) {
108            testCase = new TestCase(testCaseName);
109            mChildTestCaseMap.put(testCaseName, testCase);
110        }
111        return testCase;
112    }
113
114    /**
115     * Serialize this object and all its contents to XML.
116     *
117     * @param serializer
118     * @throws IOException
119     */
120    public void serialize(KXmlSerializer serializer) throws IOException {
121        if (mName != null) {
122            serializer.startTag(CtsXmlResultReporter.ns, TAG);
123            serializer.attribute(CtsXmlResultReporter.ns, "name", mName);
124        }
125        for (TestSuite childSuite : mChildSuiteMap.values()) {
126            childSuite.serialize(serializer);
127        }
128        for (TestCase childCase : mChildTestCaseMap.values()) {
129            childCase.serialize(serializer);
130        }
131        if (mName != null) {
132            serializer.endTag(CtsXmlResultReporter.ns, TAG);
133        }
134    }
135}
136