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