TestCase.java revision 65cd6eff9303eebf0a57c68b42e8a9e44b8771ae
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;
21import org.xmlpull.v1.XmlPullParser;
22import org.xmlpull.v1.XmlPullParserException;
23
24import java.io.IOException;
25import java.util.Collection;
26import java.util.LinkedHashMap;
27import java.util.Map;
28
29/**
30 * Data structure that represents a "TestCase" XML element and its children.
31 */
32class TestCase extends AbstractXmlPullParser {
33
34    static final String TAG = "TestCase";
35
36    private String mName;
37
38    Map<String, Test> mChildTestMap = new LinkedHashMap<String, Test>();
39
40    /**
41     * Create a {@link TestCase}
42     * @param testCaseName
43     */
44    public TestCase(String testCaseName) {
45        setName(testCaseName);
46    }
47
48    public TestCase() {
49    }
50
51    public void setName(String name) {
52        mName = name;
53    }
54
55    public String getName() {
56        return mName;
57    }
58
59    /**
60     * Gets the child tests
61     */
62    public Collection<Test> getTests() {
63        return mChildTestMap.values();
64    }
65
66    /**
67     * Inserts given test result
68     *
69     * @param testName
70     * @param testResult
71     */
72    public void insertTest(String testName, TestResult testResult) {
73        Test t = new Test(testName, testResult);
74        insertTest(t);
75    }
76
77    /**
78     * Inserts given test result
79     *
80     * @param testName
81     * @param testResult
82     */
83    private void insertTest(Test test) {
84        mChildTestMap.put(test.getName(), test);
85    }
86
87    /**
88     * Serialize this object and all its contents to XML.
89     *
90     * @param serializer
91     * @throws IOException
92     */
93    public void serialize(KXmlSerializer serializer) throws IOException {
94        serializer.startTag(CtsXmlResultReporter.ns, TAG);
95        serializer.attribute(CtsXmlResultReporter.ns, "name", getName());
96        // unused
97        serializer.attribute(CtsXmlResultReporter.ns, "priority", "");
98        for (Test t : mChildTestMap.values()) {
99            t.serialize(serializer);
100        }
101       serializer.endTag(CtsXmlResultReporter.ns, TAG);
102    }
103
104    /**
105     * Populates this class with test case result data parsed from XML.
106     *
107     * @param parser the {@link XmlPullParser}. Expected to be pointing at start
108     *            of a TestCase tag
109     */
110    @Override
111    void parse(XmlPullParser parser) throws XmlPullParserException, IOException {
112        if (!parser.getName().equals(TAG)) {
113            throw new XmlPullParserException(String.format(
114                    "invalid XML: Expected %s tag but received %s", TAG, parser.getName()));
115        }
116        setName(getAttribute(parser, "name"));
117        int eventType = parser.next();
118        while (eventType != XmlPullParser.END_DOCUMENT) {
119            if (eventType == XmlPullParser.START_TAG && parser.getName().equals(Test.TAG)) {
120                Test test = new Test();
121                test.parse(parser);
122                insertTest(test);
123            } else if (eventType == XmlPullParser.END_TAG && parser.getName().equals(TAG)) {
124                return;
125            }
126            eventType = parser.next();
127        }
128    }
129}
130