TestCase.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.Map;
25
26/**
27 * Data structure that represents a "TestCase" XML element and its children.
28 */
29class TestCase {
30
31    static final String TAG = "TestCase";
32
33    private String mName;
34
35    Map<String, Test> mChildTestMap = new LinkedHashMap<String, Test>();
36
37    /**
38     * Create a {@link TestCase}
39     * @param testCaseName
40     */
41    public TestCase(String testCaseName) {
42        setName(testCaseName);
43    }
44
45    public TestCase() {
46    }
47
48    public void setName(String name) {
49        mName = name;
50    }
51
52    public String getName() {
53        return mName;
54    }
55
56    /**
57     * Inserts given test result
58     *
59     * @param testName
60     * @param testResult
61     */
62    public void insertTest(String testName, TestResult testResult) {
63        Test t = new Test(testName, testResult);
64        insertTest(t);
65    }
66
67    /**
68     * Inserts given test result
69     *
70     * @param testName
71     * @param testResult
72     */
73    private void insertTest(Test test) {
74        mChildTestMap.put(test.getName(), test);
75    }
76
77    /**
78     * Serialize this object and all its contents to XML.
79     *
80     * @param serializer
81     * @throws IOException
82     */
83    public void serialize(KXmlSerializer serializer) throws IOException {
84        serializer.startTag(CtsXmlResultReporter.ns, TAG);
85        serializer.attribute(CtsXmlResultReporter.ns, "name", getName());
86        // unused
87        serializer.attribute(CtsXmlResultReporter.ns, "priority", "");
88        for (Test t : mChildTestMap.values()) {
89            t.serialize(serializer);
90        }
91       serializer.endTag(CtsXmlResultReporter.ns, TAG);
92    }
93}
94