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.CtsReportProto.*;
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 CtsReportHandler extends DefaultHandler {
30     private static final String MODULE_TAG = "Module";
31     private static final String TEST_CASE_TAG = "TestCase";
32     private static final String TEST_TAG = "Test";
33     private static final String NAME_TAG = "name";
34     private static final String RESULT_TAG = "result";
35     private static final String ABI_TAG = "abi";
36     private static final String DONE_TAG = "done";
37     private static final String PASS_TAG = "pass";
38     private static final String FAILED_TAG = "failed";
39     private static final String RUNTIME_TAG = "runtime";
40 
41     private CtsReport.Builder mCtsReportBld;
42     private CtsReport.TestPackage.Builder mTestPackageBld;
43     private CtsReport.TestPackage.TestSuite.Builder mTestSuiteBld;
44     private CtsReport.TestPackage.TestSuite.TestCase.Builder mTestCaseBld;
45 
CtsReportHandler(String configFileName)46     CtsReportHandler(String configFileName) {
47         mCtsReportBld = CtsReport.newBuilder();
48     }
49 
50     @Override
startElement(String uri, String localName, String name, Attributes attributes)51     public void startElement(String uri, String localName, String name, Attributes attributes)
52             throws SAXException {
53         super.startElement(uri, localName, name, attributes);
54 
55         try {
56             if (MODULE_TAG.equalsIgnoreCase(localName)) {
57                 mTestPackageBld = CtsReport.TestPackage.newBuilder();
58                 mTestSuiteBld = CtsReport.TestPackage.TestSuite.newBuilder();
59                 mTestSuiteBld.setName(attributes.getValue(NAME_TAG));
60                 mTestPackageBld.setName(attributes.getValue(NAME_TAG));
61                 // ABI is option fro a Report
62                 if (null != attributes.getValue(NAME_TAG)) {
63                     mTestPackageBld.setAbi(attributes.getValue(ABI_TAG));
64                 }
65             } else if (TEST_CASE_TAG.equalsIgnoreCase(localName)) {
66                 mTestCaseBld = CtsReport.TestPackage.TestSuite.TestCase.newBuilder();
67                 mTestCaseBld.setName(attributes.getValue(NAME_TAG));
68             } else if (TEST_TAG.equalsIgnoreCase(localName)) {
69                 CtsReport.TestPackage.TestSuite.TestCase.Test.Builder testBld;
70                 testBld = CtsReport.TestPackage.TestSuite.TestCase.Test.newBuilder();
71                 testBld.setName(attributes.getValue(NAME_TAG));
72                 testBld.setResult(attributes.getValue(RESULT_TAG));
73                 mTestCaseBld.addTest(testBld);
74             }
75         } catch (Exception ex) {
76             System.err.println(localName + " " + name);
77         }
78     }
79 
80     @Override
endElement(String uri, String localName, String name)81     public void endElement(String uri, String localName, String name) throws SAXException {
82         super.endElement(uri, localName, name);
83 
84         if (MODULE_TAG.equalsIgnoreCase(localName)) {
85             mTestPackageBld.addTestSuite(mTestSuiteBld);
86             mCtsReportBld.addTestPackage(mTestPackageBld);
87             mTestPackageBld = null;
88             mTestSuiteBld = null;
89         } else if (TEST_CASE_TAG.equalsIgnoreCase(localName)) {
90             mTestSuiteBld.addTestCase(mTestCaseBld);
91             mTestCaseBld = null;
92         }
93     }
94 
getCtsReport()95     public CtsReport getCtsReport() {
96         return mCtsReportBld.build();
97     }
98 }
99