• 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, PackageFilter packageFilter, String reportTitle, OutputStream outputStream)35     public static void printXmlReport(List<File> testApks, ApiCoverage apiCoverage,
36             PackageFilter packageFilter, String reportTitle, OutputStream outputStream) {
37         PrintStream out = new PrintStream(outputStream);
38         out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
39         out.println("<?xml-stylesheet type=\"text/xsl\"  href=\"api-coverage.xsl\"?>");
40 
41         SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yyyy h:mm a z");
42         String date = format.format(new Date(System.currentTimeMillis()));
43         out.println("<api-coverage generatedTime=\"" + date + "\" title=\"" + reportTitle +"\">");
44 
45         out.println("<debug>");
46         out.println("<sources>");
47         for (File testApk : testApks) {
48             out.println("<apk path=\"" + testApk.getPath() + "\" />");
49         }
50         out.println("</sources>");
51         out.println("</debug>");
52 
53         out.println("<api>");
54 
55         CoverageComparator comparator = new CoverageComparator();
56         List<ApiPackage> packages = new ArrayList<ApiPackage>(apiCoverage.getPackages());
57         Collections.sort(packages, comparator);
58         int totalMethods = 0;
59         int totalCoveredMethods = 0;
60         for (ApiPackage pkg : packages) {
61             if (packageFilter.accept(pkg.getName()) && pkg.getTotalMethods() > 0) {
62                 int pkgTotal = pkg.getTotalMethods();
63                 totalMethods += pkgTotal;
64                 int pkgTotalCovered = pkg.getNumCoveredMethods();
65                 totalCoveredMethods += pkgTotalCovered;
66                 out.println("<package name=\"" + pkg.getName()
67                         + "\" numCovered=\"" + pkgTotalCovered
68                         + "\" numTotal=\"" + pkgTotal
69                         + "\" coveragePercentage=\""
70                         + Math.round(pkg.getCoveragePercentage())
71                         + "\">");
72 
73                 List<ApiClass> classes = new ArrayList<ApiClass>(pkg.getClasses());
74                 Collections.sort(classes, comparator);
75 
76                 for (ApiClass apiClass : classes) {
77                     if (apiClass.getTotalMethods() > 0) {
78                         out.println("<class name=\"" + apiClass.getName()
79                                 + "\" numCovered=\"" + apiClass.getNumCoveredMethods()
80                                 + "\" numTotal=\"" + apiClass.getTotalMethods()
81                                 + "\" deprecated=\"" + apiClass.isDeprecated()
82                                 + "\" coveragePercentage=\""
83                                     + Math.round(apiClass.getCoveragePercentage())
84                                 + "\">");
85 
86                         for (ApiConstructor constructor : apiClass.getConstructors()) {
87                             String coveredWithList =
88                                     constructor.getCoveredWith().stream().collect(Collectors.joining(","));
89                             out.println("<constructor name=\"" + constructor.getName()
90                                     + "\" deprecated=\"" + constructor.isDeprecated()
91                                     + "\" covered=\"" + constructor.isCovered()
92                                     + "\" with=\"" + coveredWithList
93                                     + "\">");
94                             if (constructor.isDeprecated()) {
95                                 if (constructor.isCovered()) {
96                                     totalCoveredMethods -= 1;
97                                 }
98                                 totalMethods -= 1;
99                             }
100                             for (String parameterType : constructor.getParameterTypes()) {
101                                 out.println("<parameter type=\"" + parameterType + "\" />");
102                             }
103 
104                             out.println("</constructor>");
105                         }
106 
107                         for (ApiMethod method : apiClass.getMethods()) {
108                             String coveredWithList =
109                                     method.getCoveredWith().stream().collect(Collectors.joining(","));
110                             out.println("<method name=\"" + method.getName()
111                                     + "\" returnType=\"" + method.getReturnType()
112                                     + "\" deprecated=\"" + method.isDeprecated()
113                                     + "\" static=\"" + method.isStaticMethod()
114                                     + "\" final=\"" + method.isFinalMethod()
115                                     + "\" visibility=\"" + method.getVisibility()
116                                     + "\" abstract=\"" + method.isAbstractMethod()
117                                     + "\" covered=\"" + method.isCovered()
118                                     + "\" with=\"" + coveredWithList
119                                     + "\">");
120                             if (method.isDeprecated()) {
121                                 if (method.isCovered()) {
122                                     totalCoveredMethods -= 1;
123                                 }
124                                 totalMethods -= 1;
125                             }
126                             for (String parameterType : method.getParameterTypes()) {
127                                 out.println("<parameter type=\"" + parameterType + "\" />");
128                             }
129 
130                             out.println("</method>");
131                         }
132                         out.println("</class>");
133                     }
134                 }
135                 out.println("</package>");
136             }
137         }
138 
139         out.println("</api>");
140         out.println("<total numCovered=\"" + totalCoveredMethods + "\" "
141                 + "numTotal=\"" + totalMethods + "\" "
142                 + "coveragePercentage=\""
143                 + Math.round((float)totalCoveredMethods / totalMethods * 100.0f) + "\" />");
144         out.println("</api-coverage>");
145     }
146 }
147