Home | History | Annotate | Download | only in apicoverage
      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 
     34     public static void printXmlReport(List<File> testApks, ApiCoverage apiCoverage,
     35             String 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 (pkg.getName().startsWith(packageFilter)
     61                    && 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                             out.println("<constructor name=\"" + constructor.getName()
     88                                     + "\" deprecated=\"" + constructor.isDeprecated()
     89                                     + "\" covered=\"" + constructor.isCovered() + "\">");
     90                             if (constructor.isDeprecated()) {
     91                                 if (constructor.isCovered()) {
     92                                     totalCoveredMethods -= 1;
     93                                 }
     94                                 totalMethods -= 1;
     95                             }
     96                             for (String parameterType : constructor.getParameterTypes()) {
     97                                 out.println("<parameter type=\"" + parameterType + "\" />");
     98                             }
     99 
    100                             out.println("</constructor>");
    101                         }
    102 
    103                         for (ApiMethod method : apiClass.getMethods()) {
    104                             out.println("<method name=\"" + method.getName()
    105                                     + "\" returnType=\"" + method.getReturnType()
    106                                     + "\" deprecated=\"" + method.isDeprecated()
    107                                     + "\" covered=\"" + method.isCovered() + "\">");
    108                             if (method.isDeprecated()) {
    109                                 if (method.isCovered()) {
    110                                     totalCoveredMethods -= 1;
    111                                 }
    112                                 totalMethods -= 1;
    113                             }
    114                             for (String parameterType : method.getParameterTypes()) {
    115                                 out.println("<parameter type=\"" + parameterType + "\" />");
    116                             }
    117 
    118                             out.println("</method>");
    119                         }
    120                         out.println("</class>");
    121                     }
    122                 }
    123                 out.println("</package>");
    124             }
    125         }
    126 
    127         out.println("</api>");
    128         out.println("<total numCovered=\"" + totalCoveredMethods + "\" "
    129                 + "numTotal=\"" + totalMethods + "\" "
    130                 + "coveragePercentage=\""
    131                 + Math.round((float)totalCoveredMethods / totalMethods * 100.0f) + "\" />");
    132         out.println("</api-coverage>");
    133     }
    134 }
    135