1 /*
2  * Copyright (C) 2017 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 
17 package com.android.cts.apicoverage;
18 
19 import com.android.cts.apicoverage.TestSuiteProto.*;
20 
21 import org.xml.sax.Attributes;
22 import org.xml.sax.SAXException;
23 import org.xml.sax.helpers.DefaultHandler;
24 
25 /**
26  * {@link DefaultHandler} that builds an empty {@link ApiCoverage} object from scanning
27  * TestModule.xml.
28  */
29 class TestModuleConfigHandler extends DefaultHandler {
30     private static final String CONFIGURATION_TAG = "configuration";
31     private static final String DESCRIPTION_TAG = "description";
32     private static final String OPTION_TAG = "option";
33     private static final String TARGET_PREPARER_TAG = "target_preparer";
34     private static final String TEST_TAG = "test";
35     private static final String CLASS_TAG = "class";
36     private static final String NAME_TAG = "name";
37     private static final String KEY_TAG = "key";
38     private static final String VALUE_TAG = "value";
39     private static final String MODULE_NAME_TAG = "module-name";
40     private static final String GTEST_CLASS_TAG = "com.android.tradefed.testtype.GTest";
41 
42     private FileMetadata.Builder mFileMetadata;
43     private ConfigMetadata.Builder mConfigMetadata;
44     private ConfigMetadata.TestClass.Builder mTestCase;
45     private ConfigMetadata.TargetPreparer.Builder mTargetPreparer;
46     private String mModuleName = null;
47 
TestModuleConfigHandler(String configFileName)48     TestModuleConfigHandler(String configFileName) {
49         mFileMetadata = FileMetadata.newBuilder();
50         mConfigMetadata = ConfigMetadata.newBuilder();
51         mTestCase = null;
52         mTargetPreparer = null;
53         // Default Module Name is the Config File Name
54         mModuleName = configFileName.replaceAll(".config$", "");
55     }
56 
57     @Override
startElement(String uri, String localName, String name, Attributes attributes)58     public void startElement(String uri, String localName, String name, Attributes attributes)
59             throws SAXException {
60         super.startElement(uri, localName, name, attributes);
61 
62         if (CONFIGURATION_TAG.equalsIgnoreCase(localName)) {
63             if (null != attributes.getValue(DESCRIPTION_TAG)) {
64                 mFileMetadata.setDescription(attributes.getValue(DESCRIPTION_TAG));
65             } else {
66                 mFileMetadata.setDescription("WARNING: no description.");
67             }
68         } else if (TEST_TAG.equalsIgnoreCase(localName)) {
69             mTestCase = ConfigMetadata.TestClass.newBuilder();
70             mTestCase.setTestClass(attributes.getValue(CLASS_TAG));
71         } else if (TARGET_PREPARER_TAG.equalsIgnoreCase(localName)) {
72             mTargetPreparer = ConfigMetadata.TargetPreparer.newBuilder();
73             mTargetPreparer.setTestClass(attributes.getValue(CLASS_TAG));
74         } else if (OPTION_TAG.equalsIgnoreCase(localName)) {
75             Option.Builder option = Option.newBuilder();
76             option.setName(attributes.getValue(NAME_TAG));
77             option.setValue(attributes.getValue(VALUE_TAG));
78             String keyStr = attributes.getValue(KEY_TAG);
79             if (null != keyStr) {
80                 option.setKey(keyStr);
81             }
82             if (null != mTestCase) {
83                 mTestCase.addOptions(option);
84                 if (MODULE_NAME_TAG.equalsIgnoreCase(option.getName())) {
85                     mModuleName = option.getValue();
86                 }
87             } else if (null != mTargetPreparer) {
88                 mTargetPreparer.addOptions(option);
89             }
90         }
91     }
92 
93     @Override
endElement(String uri, String localName, String name)94     public void endElement(String uri, String localName, String name) throws SAXException {
95         super.endElement(uri, localName, name);
96         if (TEST_TAG.equalsIgnoreCase(localName)) {
97             mConfigMetadata.addTestClasses(mTestCase);
98             mTestCase = null;
99         } else if (TARGET_PREPARER_TAG.equalsIgnoreCase(localName)) {
100             mConfigMetadata.addTargetPreparers(mTargetPreparer);
101             mTargetPreparer = null;
102         } else if (CONFIGURATION_TAG.equalsIgnoreCase(localName)) {
103             mFileMetadata.setConfigMetadata(mConfigMetadata);
104         }
105     }
106 
getModuleName()107     public String getModuleName() {
108         return mModuleName;
109     }
110 
getTestClassName()111     public String getTestClassName() {
112         //return the 1st Test Class
113         return mFileMetadata.getConfigMetadata().getTestClassesList().get(0).getTestClass();
114     }
115 
getFileMetadata()116     public FileMetadata getFileMetadata() {
117         return mFileMetadata.build();
118     }
119 }
120