1 /*
2  * Copyright (C) 2010 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  */
16 package com.android.tradefed.testtype.testdefs;
17 
18 import com.android.tradefed.util.xml.AbstractXmlParser;
19 
20 import org.xml.sax.Attributes;
21 import org.xml.sax.SAXException;
22 import org.xml.sax.helpers.DefaultHandler;
23 
24 import java.util.Collection;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27 
28 /**
29  * Parses a test_defs.xml file.
30  * <p/>
31  * See development/testrunner/test_defs.xsd for expected format
32  */
33 class XmlDefsParser extends AbstractXmlParser {
34 
35     private Map<String, InstrumentationTestDef> mTestDefsMap;
36 
37     /**
38      * SAX callback object. Handles parsing data from the xml tags.
39      */
40     private class DefsHandler extends DefaultHandler {
41 
42         private static final String TEST_TAG = "test";
43 
44         @Override
startElement(String uri, String localName, String name, Attributes attributes)45         public void startElement(String uri, String localName, String name, Attributes attributes)
46                 throws SAXException {
47             if (TEST_TAG.equals(localName)) {
48                 final String defName = attributes.getValue("name");
49                 InstrumentationTestDef def = new InstrumentationTestDef(defName,
50                         attributes.getValue("package"));
51                 def.setClassName(attributes.getValue("class"));
52                 def.setRunner(attributes.getValue("runner"));
53                 def.setContinuous("true".equals(attributes.getValue("continuous")));
54                 def.setCoverageTarget(attributes.getValue("coverage_target"));
55                 mTestDefsMap.put(defName, def);
56             }
57         }
58     }
59 
XmlDefsParser()60     XmlDefsParser() {
61         // Uses a LinkedHashmap to have predictable iteration order
62         mTestDefsMap = new LinkedHashMap<String, InstrumentationTestDef>();
63     }
64 
65     /**
66      * Gets the list of parsed test definitions. The element order should be consistent with the
67      * order of elements in the parsed input.
68      */
getTestDefs()69     public Collection<InstrumentationTestDef> getTestDefs() {
70         return mTestDefsMap.values();
71     }
72 
73     /**
74      * {@inheritDoc}
75      */
76     @Override
createXmlHandler()77     protected DefaultHandler createXmlHandler() {
78         return new DefsHandler();
79     }
80 }
81