summaryrefslogtreecommitdiff
path: root/build-system/gradle-core/src/test/java/com/android/build/gradle/PseudoApiChangesTest.java
blob: c84bb32d17e54c42888cf5bf05df234ea67c079a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.build.gradle;

import com.android.testutils.ApiTester;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Resources;
import com.google.common.reflect.ClassPath;
import java.io.IOException;
import java.net.URL;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.Test;

public class PseudoApiChangesTest {

    private static final URL API_LIST_URL =
            Resources.getResource(PseudoApiChangesTest.class, "pseudo-api.txt");

    private static final ImmutableSet<String> EXCLUDED_IMPL_CLASSES =
            ImmutableSet.of(
                    "AndroidSourceSetFactory",
                    "VariantOutputFactory",
                    "ApkInfoParser",
                    "BuilderConstants",
                    "DefaultManifestParser",
                    "DefaultApiVersion",
                    "LibraryRequest",
                    "ManifestAttributeSupplier",
                    "ModuleBooleanPropertyKeys",
                    "ModuleStringPropertyKeys",
                    "StandardOutErrMessageReceiver",
                    "ToolsRevisionUtils",
                    "ComponentType",
                    "ComponentTypeImpl");

    @Test
    public void stableImplementationClassesTest() throws IOException {
        getApiTester().checkApiElements();
    }

    static ApiTester getApiTester() throws IOException {

        ImmutableSet<ClassPath.ClassInfo> allClasses = getPseudoApiClasses();

        List<ClassPath.ClassInfo> classes =
                allClasses.stream()
                        .filter(
                                classInfo ->
                                        !classInfo.getSimpleName().endsWith("Test")
                                                && !classInfo.getSimpleName().endsWith("TestKt")
                                                && !classInfo
                                                        .getSimpleName()
                                                        .equals("StableApiUpdater")
                                                && !classInfo
                                                        .getSimpleName()
                                                        .contains("DecoratedApiChangesUpdater"))
                        .collect(Collectors.toList());

        return new ApiTester(
                "The Android Gradle Plugin internal implementation classes.",
                classes,
                ApiTester.Filter.ALL,
                "The internal implementation classes"
                        + " have changed, either revert the api change or run\n"
                        + "gradlew :base:builder-model:updateToolingModelApi\n"
                        + "\n"
                        + "To update all the API expectation files, run \n"
                        + "    gradlew updateApi\n"
                        + "\n"
                        + "PseudoApiUpdater will apply the following changes if run:\n",
                API_LIST_URL,
                ApiTester.Flag.OMIT_HASH);
    }

    private static ImmutableSet<ClassPath.ClassInfo> getPseudoApiClasses() throws IOException {
        ClassPath classPath = ClassPath.from(BaseExtension.class.getClassLoader());
        ImmutableSet.Builder<ClassPath.ClassInfo> builder = ImmutableSet.builder();
        builder.addAll(classPath.getTopLevelClasses("com.android.build.gradle"));
        builder.addAll(classPath.getTopLevelClassesRecursive("com.android.build.gradle.api"));
        builder.addAll(classPath.getTopLevelClasses("com.android.builder.signing").stream()
                .filter(classInfo -> classInfo.getSimpleName().equals("DefaultSigningConfig"))
                .collect(Collectors.toList()));

        Set<String> excludedImplClasses = new HashSet<>(EXCLUDED_IMPL_CLASSES);
        // Include the legacy DSL impl classes
        for (ClassPath.ClassInfo aClass :
                classPath.getTopLevelClasses("com.android.build.gradle.internal.dsl")) {
            if (aClass.getSimpleName().endsWith("Impl")
                    || excludedImplClasses.remove(aClass.getSimpleName())) {
                continue;
            }
            builder.add(aClass);
        }
        // And the legacy DSL base classes in builder.
        for (ClassPath.ClassInfo aClass : classPath.getTopLevelClasses("com.android.builder.core")) {
            if (excludedImplClasses.remove(aClass.getSimpleName())) {
                continue;
            }
            builder.add(aClass);
        }

        if (!excludedImplClasses.isEmpty()) {
            throw new IllegalStateException("Excluded classes not found: " + excludedImplClasses);
        }
        return builder.build();
    }
}