"Fossies" - the Fresh Open Source Software Archive

Member "gradle-8.1.1/subprojects/platform-base/src/test/groovy/org/gradle/platform/base/internal/registry/AbstractAnnotationModelRuleExtractorTest.groovy" (20 Apr 2023, 5839 Bytes) of package /linux/misc/gradle-8.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * Copyright 2014 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  *      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 org.gradle.platform.base.internal.registry
   18 
   19 import org.gradle.internal.Factory
   20 import org.gradle.internal.reflect.JavaReflectionUtil
   21 import org.gradle.internal.reflect.MethodDescription
   22 import org.gradle.model.InvalidModelRuleDeclarationException
   23 import org.gradle.model.internal.core.ModelAction
   24 import org.gradle.model.internal.core.ModelActionRole
   25 import org.gradle.model.internal.core.ModelPath
   26 import org.gradle.model.internal.core.ModelView
   27 import org.gradle.model.internal.core.MutableModelNode
   28 import org.gradle.model.internal.core.rule.describe.ModelRuleDescriptor
   29 import org.gradle.model.internal.fixture.ProjectRegistrySpec
   30 import org.gradle.model.internal.inspect.DefaultMethodModelRuleExtractionContext
   31 import org.gradle.model.internal.inspect.DefaultMethodRuleDefinition
   32 import org.gradle.model.internal.inspect.DefaultModelRuleInvoker
   33 import org.gradle.model.internal.inspect.ExtractedModelRule
   34 import org.gradle.model.internal.inspect.FormattingValidationProblemCollector
   35 import org.gradle.model.internal.inspect.MethodModelRuleApplicationContext
   36 import org.gradle.model.internal.inspect.MethodRuleAction
   37 import org.gradle.model.internal.inspect.MethodRuleDefinition
   38 import org.gradle.model.internal.method.WeaklyTypeReferencingMethod
   39 import org.gradle.model.internal.registry.ModelRegistry
   40 import org.gradle.model.internal.type.ModelType
   41 
   42 import java.lang.annotation.Annotation
   43 import java.lang.reflect.Method
   44 
   45 public abstract class AbstractAnnotationModelRuleExtractorTest extends ProjectRegistrySpec {
   46     def ruleDefinition = Mock(MethodRuleDefinition)
   47 
   48     protected abstract AbstractAnnotationDrivenComponentModelRuleExtractor getRuleHandler();
   49 
   50     abstract Class<? extends Annotation> getAnnotation();
   51 
   52     abstract Class<?> getRuleClass();
   53 
   54     def "handles methods annotated with @#annotationName"() {
   55         when:
   56         1 * ruleDefinition.isAnnotationPresent(annotation) >> false
   57 
   58         then:
   59         !ruleHandler.isSatisfiedBy(ruleDefinition)
   60 
   61         when:
   62         1 * ruleDefinition.isAnnotationPresent(annotation) >> true
   63 
   64         then:
   65         ruleHandler.isSatisfiedBy(ruleDefinition)
   66 
   67         where:
   68         annotationName << [annotation.getSimpleName()]
   69     }
   70 
   71     void apply(ExtractedModelRule rule, ModelRegistry registry) {
   72         def context = Stub(MethodModelRuleApplicationContext) {
   73             getRegistry() >> registry
   74             contextualize(_) >> { MethodRuleAction action ->
   75                 Stub(ModelAction) {
   76                     getSubject() >> action.subject
   77                     getInputs() >> action.inputs
   78                     execute(_, _) >> { MutableModelNode node, List<ModelView<?>> inputs ->
   79                         action.execute(new DefaultModelRuleInvoker(rule.ruleDefinition.method, { JavaReflectionUtil.newInstance(rule.ruleDefinition.method.method.declaringClass) } as Factory), node, inputs) }
   80                 }
   81             }
   82             getScope() >> ModelPath.ROOT
   83         }
   84         def node = Stub(MutableModelNode)
   85         rule.apply(context, node)
   86     }
   87 
   88     void apply(MethodRuleDefinition<?, ?> definition) {
   89         def rule = extract(definition)
   90         def registryNode = Stub(MutableModelNode) {
   91             isAtLeast(_) >> true
   92             asMutable(_, _) >> { ModelType type, ModelRuleDescriptor ruleDescriptor ->
   93                 return Stub(ModelView) {
   94                     getInstance() >> { Stub(type.concreteClass) }
   95                 }
   96             }
   97             asImmutable(_, _) >> { ModelType type, ModelRuleDescriptor ruleDescriptor ->
   98                 return Stub(ModelView) {
   99                     getInstance() >> { Stub(type.concreteClass) }
  100                 }
  101             }
  102         }
  103         def registry = Stub(ModelRegistry) {
  104             configure(_, _) >> { ModelActionRole role, ModelAction action ->
  105                 action.execute(registryNode, [])
  106             }
  107         }
  108         apply(rule, registry)
  109     }
  110 
  111     ExtractedModelRule extract(MethodRuleDefinition<?, ?> definition) {
  112         def problems = new FormattingValidationProblemCollector("rule source", ModelType.of(ruleClass))
  113         def context = new DefaultMethodModelRuleExtractionContext(null, problems)
  114         def registration = ruleHandler.registration(definition, context)
  115         if (context.hasProblems()) {
  116             throw new InvalidModelRuleDeclarationException(problems.format())
  117         }
  118         return registration
  119     }
  120 
  121     MethodRuleDefinition<?, ?> ruleDefinitionForMethod(String methodName) {
  122         for (Method candidate : ruleClass.getDeclaredMethods()) {
  123             if (candidate.getName().equals(methodName)) {
  124                 return DefaultMethodRuleDefinition.create(ruleClass, candidate)
  125             }
  126         }
  127         throw new IllegalArgumentException("Not a test method name")
  128     }
  129 
  130     String getStringDescription(MethodRuleDefinition ruleDefinition) {
  131         def builder = new StringBuilder()
  132         ruleDefinition.descriptor.describeTo(builder)
  133         builder.toString()
  134     }
  135 
  136     String getStringDescription(WeaklyTypeReferencingMethod<?, ?> method) {
  137         return MethodDescription.name(method.getName())
  138                 .takes(method.method.getGenericParameterTypes())
  139                 .toString();
  140     }
  141 }