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
17package com.android.cts.apicoverage;
18
19import java.io.OutputStream;
20import java.io.PrintStream;
21import java.util.ArrayList;
22import java.util.Collections;
23import java.util.List;
24
25/**
26 * Class that outputs a text report of {@link ApiCoverage}.
27 */
28class TextReport {
29
30    public static void printTextReport(ApiCoverage api, OutputStream outputStream) {
31        PrintStream out = new PrintStream(outputStream);
32
33        CoverageComparator comparator = new CoverageComparator();
34        List<ApiPackage> packages = new ArrayList<ApiPackage>(api.getPackages());
35        Collections.sort(packages, comparator);
36
37        for (ApiPackage apiPackage : packages) {
38            if (apiPackage.getName().startsWith("android")
39                    && apiPackage.getTotalMethods() > 0) {
40                printPackage(apiPackage, out);
41            }
42        }
43
44        out.println();
45        out.println();
46
47        for (ApiPackage apiPackage : packages) {
48            if (apiPackage.getName().startsWith("android")) {
49                printPackage(apiPackage, out);
50
51                List<ApiClass> classes = new ArrayList<ApiClass>(apiPackage.getClasses());
52                Collections.sort(classes, comparator);
53                for (ApiClass apiClass : classes) {
54                    if (apiClass.getTotalMethods() > 0) {
55                        printClass(apiClass, out);
56
57                        List<ApiConstructor> constructors =
58                                new ArrayList<ApiConstructor>(apiClass.getConstructors());
59                        Collections.sort(constructors);
60                        for (ApiConstructor constructor : constructors) {
61                            printConstructor(constructor, out);
62                        }
63
64                        List<ApiMethod> methods = new ArrayList<ApiMethod>(apiClass.getMethods());
65                        Collections.sort(methods);
66                        for (ApiMethod method : methods) {
67                            printMethod(method, out);
68                        }
69                    }
70                }
71            }
72        }
73    }
74
75    private static void printPackage(ApiPackage apiPackage, PrintStream out) {
76        out.println(apiPackage.getName() + " "
77                + Math.round(apiPackage.getCoveragePercentage()) + "% ("
78                + apiPackage.getNumCoveredMethods() + "/" + apiPackage.getTotalMethods() + ")");
79    }
80
81    private static void printClass(ApiClass apiClass, PrintStream out) {
82        out.println("  " + apiClass.getName() + " "
83                + Math.round(apiClass.getCoveragePercentage()) + "% ("
84                + apiClass.getNumCoveredMethods() + "/" + apiClass.getTotalMethods() + ") ");
85    }
86
87    private static void printConstructor(ApiConstructor constructor, PrintStream out) {
88        StringBuilder builder = new StringBuilder("    [")
89                .append(constructor.isCovered() ? "X" : " ")
90                .append("] ").append(constructor.getName()).append("(");
91
92        List<String> parameterTypes = constructor.getParameterTypes();
93        int numParameterTypes = parameterTypes.size();
94        for (int i = 0; i < numParameterTypes; i++) {
95            builder.append(parameterTypes.get(i));
96            if (i + 1 < numParameterTypes) {
97                builder.append(", ");
98            }
99        }
100        out.println(builder.append(")"));
101    }
102
103    private static void printMethod(ApiMethod method, PrintStream out) {
104        StringBuilder builder = new StringBuilder("    [")
105                .append(method.isCovered() ? "X" : " ")
106                .append("] ").append(method.getReturnType()).append(" ")
107                .append(method.getName()).append("(");
108        List<String> parameterTypes = method.getParameterTypes();
109        int numParameterTypes = parameterTypes.size();
110        for (int i = 0; i < numParameterTypes; i++) {
111            builder.append(parameterTypes.get(i));
112            if (i + 1 < numParameterTypes) {
113                builder.append(", ");
114            }
115        }
116        out.println(builder.append(")"));
117    }
118}
119