|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 the original author or authors. |
| 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 | + * https://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 org.springframework.boot.gradle; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | +import java.util.regex.Pattern; |
| 23 | + |
| 24 | +import com.tngtech.archunit.base.DescribedPredicate; |
| 25 | +import com.tngtech.archunit.base.Predicate; |
| 26 | +import com.tngtech.archunit.core.domain.JavaClasses; |
| 27 | +import com.tngtech.archunit.core.domain.JavaMethodCall; |
| 28 | +import com.tngtech.archunit.core.domain.JavaType; |
| 29 | +import com.tngtech.archunit.core.importer.ImportOption; |
| 30 | +import com.tngtech.archunit.core.importer.Location; |
| 31 | +import com.tngtech.archunit.junit.AnalyzeClasses; |
| 32 | +import com.tngtech.archunit.junit.ArchTest; |
| 33 | +import com.tngtech.archunit.lang.syntax.ArchRuleDefinition; |
| 34 | +import org.gradle.api.Action; |
| 35 | +import org.gradle.api.tasks.TaskCollection; |
| 36 | +import org.gradle.api.tasks.TaskContainer; |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests that verify the plugin's compliance with task configuration avoidance. |
| 40 | + * |
| 41 | + * @author Andy Wilkinson |
| 42 | + */ |
| 43 | +@AnalyzeClasses(packages = "org.springframework.boot.gradle", |
| 44 | + importOptions = TaskConfigurationAvoidanceTests.DoNotIncludeTests.class) |
| 45 | +class TaskConfigurationAvoidanceTests { |
| 46 | + |
| 47 | + @ArchTest |
| 48 | + void noApisThatCauseEagerTaskConfigurationShouldBeCalled(JavaClasses classes) { |
| 49 | + ProhibitedMethods prohibited = new ProhibitedMethods(); |
| 50 | + prohibited.on(TaskContainer.class).methodsNamed("create", "findByPath, getByPath").method("withType", |
| 51 | + Class.class, Action.class); |
| 52 | + prohibited.on(TaskCollection.class).methodsNamed("findByName", "getByName"); |
| 53 | + ArchRuleDefinition.noClasses().should() |
| 54 | + .callMethodWhere(DescribedPredicate.describe("it would cause eager task configuration", prohibited)) |
| 55 | + .check(classes); |
| 56 | + } |
| 57 | + |
| 58 | + static class DoNotIncludeTests implements ImportOption { |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean includes(Location location) { |
| 62 | + return !location.matches(Pattern.compile(".*Tests\\.class")); |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + private static final class ProhibitedMethods implements Predicate<JavaMethodCall> { |
| 68 | + |
| 69 | + private final List<Predicate<JavaMethodCall>> prohibited = new ArrayList<>(); |
| 70 | + |
| 71 | + private ProhibitedConfigurer on(Class<?> type) { |
| 72 | + return new ProhibitedConfigurer(type); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean apply(JavaMethodCall methodCall) { |
| 77 | + for (Predicate<JavaMethodCall> spec : this.prohibited) { |
| 78 | + if (spec.apply(methodCall)) { |
| 79 | + return true; |
| 80 | + } |
| 81 | + } |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + private final class ProhibitedConfigurer { |
| 86 | + |
| 87 | + private final Class<?> type; |
| 88 | + |
| 89 | + private ProhibitedConfigurer(Class<?> type) { |
| 90 | + this.type = type; |
| 91 | + } |
| 92 | + |
| 93 | + private ProhibitedConfigurer methodsNamed(String... names) { |
| 94 | + for (String name : names) { |
| 95 | + ProhibitedMethods.this.prohibited.add(new ProhibitMethodsNamed(this.type, name)); |
| 96 | + } |
| 97 | + return this; |
| 98 | + } |
| 99 | + |
| 100 | + private ProhibitedConfigurer method(String name, Class<?>... parameterTypes) { |
| 101 | + ProhibitedMethods.this.prohibited |
| 102 | + .add(new ProhibitMethod(this.type, name, Arrays.asList(parameterTypes))); |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + static class ProhibitMethodsNamed implements Predicate<JavaMethodCall> { |
| 109 | + |
| 110 | + private final Class<?> owner; |
| 111 | + |
| 112 | + private final String name; |
| 113 | + |
| 114 | + ProhibitMethodsNamed(Class<?> owner, String name) { |
| 115 | + this.owner = owner; |
| 116 | + this.name = name; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public boolean apply(JavaMethodCall methodCall) { |
| 121 | + return methodCall.getTargetOwner().isEquivalentTo(this.owner) && methodCall.getName().equals(this.name); |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + private static final class ProhibitMethod extends ProhibitMethodsNamed { |
| 127 | + |
| 128 | + private final List<Class<?>> parameterTypes; |
| 129 | + |
| 130 | + private ProhibitMethod(Class<?> owner, String name, List<Class<?>> parameterTypes) { |
| 131 | + super(owner, name); |
| 132 | + this.parameterTypes = parameterTypes; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public boolean apply(JavaMethodCall methodCall) { |
| 137 | + return super.apply(methodCall) && match(methodCall.getTarget().getParameterTypes()); |
| 138 | + } |
| 139 | + |
| 140 | + private boolean match(List<JavaType> callParameterTypes) { |
| 141 | + if (this.parameterTypes.size() != callParameterTypes.size()) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + for (int i = 0; i < this.parameterTypes.size(); i++) { |
| 145 | + if (!callParameterTypes.get(i).toErasure().isEquivalentTo(this.parameterTypes.get(i))) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + } |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments