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 Collections.sort(testApks); 49 for (File testApk : testApks) { 50 out.println("<apk path=\"" + testApk.getPath() + "\" />"); 51 } 52 out.println("</sources>"); 53 out.println("</debug>"); 54 55 out.println("<api>"); 56 57 CoverageComparator comparator = new CoverageComparator(); 58 List<ApiPackage> packages = new ArrayList<ApiPackage>(apiCoverage.getPackages()); 59 Collections.sort(packages, comparator); 60 int totalMethods = 0; 61 int totalCoveredMethods = 0; 62 for (ApiPackage pkg : packages) { 63 if (packageFilter.accept(pkg.getName()) && pkg.getTotalMethods() > 0) { 64 int pkgTotal = pkg.getTotalMethods(); 65 totalMethods += pkgTotal; 66 int pkgTotalCovered = pkg.getNumCoveredMethods(); 67 totalCoveredMethods += pkgTotalCovered; 68 out.println("<package name=\"" + pkg.getName() 69 + "\" numCovered=\"" + pkgTotalCovered 70 + "\" numTotal=\"" + pkgTotal 71 + "\" coveragePercentage=\"" 72 + Math.round(pkg.getCoveragePercentage()) 73 + "\">"); 74 75 List<ApiClass> classes = new ArrayList<ApiClass>(pkg.getClasses()); 76 Collections.sort(classes, comparator); 77 78 for (ApiClass apiClass : classes) { 79 if (apiClass.getTotalMethods() > 0) { 80 out.println("<class name=\"" + apiClass.getName() 81 + "\" numCovered=\"" + apiClass.getNumCoveredMethods() 82 + "\" numTotal=\"" + apiClass.getTotalMethods() 83 + "\" deprecated=\"" + apiClass.isDeprecated() 84 + "\" coveragePercentage=\"" 85 + Math.round(apiClass.getCoveragePercentage()) 86 + "\">"); 87 88 for (ApiConstructor constructor : apiClass.getConstructors()) { 89 List<String> coveredWithList = new ArrayList<String>(constructor.getCoveredWith()); 90 Collections.sort(coveredWithList); 91 String coveredWith = coveredWithList.stream().collect(Collectors.joining(",")); 92 out.println("<constructor name=\"" + constructor.getName() 93 + "\" deprecated=\"" + constructor.isDeprecated() 94 + "\" covered=\"" + constructor.isCovered() 95 + "\" with=\"" + coveredWith 96 + "\">"); 97 if (constructor.isDeprecated()) { 98 if (constructor.isCovered()) { 99 totalCoveredMethods -= 1; 100 } 101 totalMethods -= 1; 102 } 103 for (String parameterType : constructor.getParameterTypes()) { 104 out.println("<parameter type=\"" + parameterType + "\" />"); 105 } 106 107 out.println("</constructor>"); 108 } 109 110 for (ApiMethod method : apiClass.getMethods()) { 111 List<String> coveredWithList = new ArrayList<String>(method.getCoveredWith()); 112 Collections.sort(coveredWithList); 113 String coveredWith = coveredWithList.stream().collect(Collectors.joining(",")); 114 out.println("<method name=\"" + method.getName() 115 + "\" returnType=\"" + method.getReturnType() 116 + "\" deprecated=\"" + method.isDeprecated() 117 + "\" static=\"" + method.isStaticMethod() 118 + "\" final=\"" + method.isFinalMethod() 119 + "\" visibility=\"" + method.getVisibility() 120 + "\" abstract=\"" + method.isAbstractMethod() 121 + "\" covered=\"" + method.isCovered() 122 + "\" with=\"" + coveredWith 123 + "\">"); 124 if (method.isDeprecated()) { 125 if (method.isCovered()) { 126 totalCoveredMethods -= 1; 127 } 128 totalMethods -= 1; 129 } 130 for (String parameterType : method.getParameterTypes()) { 131 out.println("<parameter type=\"" + parameterType + "\" />"); 132 } 133 134 out.println("</method>"); 135 } 136 out.println("</class>"); 137 } 138 } 139 out.println("</package>"); 140 } 141 } 142 143 out.println("</api>"); 144 out.println("<cdd>"); 145 for (CddCoverage.CddRequirement requirement : cddCoverage.getCddRequirements()) { 146 out.println("<requirement id=\"" + requirement.getRequirementId() + "\">"); 147 for (CddCoverage.TestMethod method : requirement.getTestMethods()) { 148 out.print("<test module=\"" + method.getTestModule() 149 + "\" class=\"" + method.getTestClass() + "\" "); 150 if (method.getTestMethod() != null) { 151 out.print("method=\"" + method.getTestMethod() + "\""); 152 } 153 out.println("/>" ); 154 } 155 out.println("</requirement>"); 156 } 157 out.println("</cdd>"); 158 out.println("<total numCovered=\"" + totalCoveredMethods + "\" " 159 + "numTotal=\"" + totalMethods + "\" " 160 + "coveragePercentage=\"" 161 + Math.round((float)totalCoveredMethods / totalMethods * 100.0f) + "\" />"); 162 out.println("</api-coverage>"); 163 } 164 } 165