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