• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.cts.apicoverage;
18 
19 import java.io.File;
20 import java.io.OutputStream;
21 import java.io.PrintStream;
22 import java.text.SimpleDateFormat;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.List;
27 import java.util.stream.Collectors;
28 
29 /**
30  * Class that outputs an XML report of the {@link ApiCoverage} collected. It can be viewed in
31  * a browser when used with the api-coverage.css and api-coverage.xsl files.
32  */
33 class XmlReport {
34 
printXmlReport(List<File> testApks, ApiCoverage apiCoverage, CddCoverage cddCoverage, PackageFilter packageFilter, String reportTitle, OutputStream outputStream)35     public static void printXmlReport(List<File> testApks, ApiCoverage apiCoverage,
36             CddCoverage cddCoverage, PackageFilter packageFilter, String reportTitle,
37             OutputStream outputStream) {
38         PrintStream out = new PrintStream(outputStream);
39         out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
40         out.println("<?xml-stylesheet type=\"text/xsl\"  href=\"api-coverage.xsl\"?>");
41 
42         SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yyyy h:mm a z");
43         String date = format.format(new Date(System.currentTimeMillis()));
44         out.println("<api-coverage generatedTime=\"" + date + "\" title=\"" + reportTitle +"\">");
45 
46         out.println("<debug>");
47         out.println("<sources>");
48         for (File testApk : testApks) {
49             out.println("<apk path=\"" + testApk.getPath() + "\" />");
50         }
51         out.println("</sources>");
52         out.println("</debug>");
53 
54         out.println("<api>");
55 
56         CoverageComparator comparator = new CoverageComparator();
57         List<ApiPackage> packages = new ArrayList<ApiPackage>(apiCoverage.getPackages());
58         Collections.sort(packages, comparator);
59         int totalMethods = 0;
60         int totalCoveredMethods = 0;
61         for (ApiPackage pkg : packages) {
62             if (packageFilter.accept(pkg.getName()) && pkg.getTotalMethods() > 0) {
63                 int pkgTotal = pkg.getTotalMethods();
64                 totalMethods += pkgTotal;
65                 int pkgTotalCovered = pkg.getNumCoveredMethods();
66                 totalCoveredMethods += pkgTotalCovered;
67                 out.println("<package name=\"" + pkg.getName()
68                         + "\" numCovered=\"" + pkgTotalCovered
69                         + "\" numTotal=\"" + pkgTotal
70                         + "\" coveragePercentage=\""
71                         + Math.round(pkg.getCoveragePercentage())
72                         + "\">");
73 
74                 List<ApiClass> classes = new ArrayList<ApiClass>(pkg.getClasses());
75                 Collections.sort(classes, comparator);
76 
77                 for (ApiClass apiClass : classes) {
78                     if (apiClass.getTotalMethods() > 0) {
79                         out.println("<class name=\"" + apiClass.getName()
80                                 + "\" numCovered=\"" + apiClass.getNumCoveredMethods()
81                                 + "\" numTotal=\"" + apiClass.getTotalMethods()
82                                 + "\" deprecated=\"" + apiClass.isDeprecated()
83                                 + "\" coveragePercentage=\""
84                                     + Math.round(apiClass.getCoveragePercentage())
85                                 + "\">");
86 
87                         for (ApiConstructor constructor : apiClass.getConstructors()) {
88                             String coveredWithList =
89                                     constructor.getCoveredWith().stream().collect(Collectors.joining(","));
90                             out.println("<constructor name=\"" + constructor.getName()
91                                     + "\" deprecated=\"" + constructor.isDeprecated()
92                                     + "\" covered=\"" + constructor.isCovered()
93                                     + "\" with=\"" + coveredWithList
94                                     + "\">");
95                             if (constructor.isDeprecated()) {
96                                 if (constructor.isCovered()) {
97                                     totalCoveredMethods -= 1;
98                                 }
99                                 totalMethods -= 1;
100                             }
101                             for (String parameterType : constructor.getParameterTypes()) {
102                                 out.println("<parameter type=\"" + parameterType + "\" />");
103                             }
104 
105                             out.println("</constructor>");
106                         }
107 
108                         for (ApiMethod method : apiClass.getMethods()) {
109                             String coveredWithList =
110                                     method.getCoveredWith().stream().collect(Collectors.joining(","));
111                             out.println("<method name=\"" + method.getName()
112                                     + "\" returnType=\"" + method.getReturnType()
113                                     + "\" deprecated=\"" + method.isDeprecated()
114                                     + "\" static=\"" + method.isStaticMethod()
115                                     + "\" final=\"" + method.isFinalMethod()
116                                     + "\" visibility=\"" + method.getVisibility()
117                                     + "\" abstract=\"" + method.isAbstractMethod()
118                                     + "\" covered=\"" + method.isCovered()
119                                     + "\" with=\"" + coveredWithList
120                                     + "\">");
121                             if (method.isDeprecated()) {
122                                 if (method.isCovered()) {
123                                     totalCoveredMethods -= 1;
124                                 }
125                                 totalMethods -= 1;
126                             }
127                             for (String parameterType : method.getParameterTypes()) {
128                                 out.println("<parameter type=\"" + parameterType + "\" />");
129                             }
130 
131                             out.println("</method>");
132                         }
133                         out.println("</class>");
134                     }
135                 }
136                 out.println("</package>");
137             }
138         }
139 
140         out.println("</api>");
141         out.println("<cdd>");
142         for (CddCoverage.CddRequirement requirement : cddCoverage.getCddRequirements()) {
143             out.println("<requirement id=\"" + requirement.getRequirementId() + "\">");
144             for (CddCoverage.TestMethod method : requirement.getTestMethods()) {
145                 out.print("<test module=\"" + method.getTestModule()
146                         + "\" class=\"" + method.getTestClass() + "\" ");
147                 if (method.getTestMethod() != null) {
148                     out.print("method=\"" + method.getTestMethod() + "\"");
149                 }
150                 out.println("/>" );
151             }
152             out.println("</requirement>");
153         }
154         out.println("</cdd>");
155         out.println("<total numCovered=\"" + totalCoveredMethods + "\" "
156                 + "numTotal=\"" + totalMethods + "\" "
157                 + "coveragePercentage=\""
158                 + Math.round((float)totalCoveredMethods / totalMethods * 100.0f) + "\" />");
159         out.println("</api-coverage>");
160     }
161 }
162