<commit_before>/*
 * Copyright 2014 the original author or authors.
 *
 * 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 grails.test.runtime

import grails.test.mixin.TestRuntimeAwareMixin
import grails.test.mixin.UseTestPlugin
import grails.test.mixin.support.MixinInstance
import groovy.transform.CompileStatic

import java.lang.reflect.Field
import java.lang.reflect.Modifier

import org.springframework.util.ReflectionUtils

/**
 * TestRuntimeFactory is a singleton that contains the TestPlugin registry and has methods 
 * for getting a TestRuntime instance with given features.
 * 
 * It will resolve the requested features and pick the minimum set of plugins 
 * that are required for covering the requested features.
 * 
 * @author <NAME>
 * @since 2.4.0
 */
@CompileStatic
class TestRuntimeFactory {
    static TestRuntimeFactory INSTANCE=new TestRuntimeFactory()
    Set<Class<? extends TestPlugin>> availablePluginClasses=[
        GrailsApplicationTestPlugin,
        CoreBeansTestPlugin,
        MetaClassCleanerTestPlugin,
        ControllerTestPlugin,
        FiltersTestPlugin,
        GroovyPageTestPlugin,
        WebFlowTestPlugin,
        DomainClassTestPlugin
        ] as Set

    private TestRuntimeFactory() {
    }
    
    static TestRuntime getRuntimeForTestClass(Class<?> testClass) {
        Set<TestRuntimeAwareMixin> allInstances = [] as Set
        Set<String> allFeatures = [] as Set
        Set<TestPluginUsage> allTestPluginUsages = [] as Set
        Class<?> currentClass = testClass
        while(currentClass != Object && currentClass != null) {
            currentClass.getDeclaredFields().each { Field field ->
                if(field.getAnnotation(MixinInstance.class) != null && Modifier.isStatic(field.getModifiers())) {
                    ReflectionUtils.makeAccessible(field)
                    def instance = field.get(null)
                    if(instance instanceof TestRuntimeAwareMixin) {
                        allInstances.add((TestRuntimeAwareMixin)instance)
                        instance.getFeatures() && allFeatures.addAll(instance.getFeatures() as List)
                    }
                    if(instance instanceof TestPluginRegistrar) {
                        instance.getTestPluginUsages() && allTestPluginUsages.addAll(instance.getTestPluginUsages() as List)
                    }
                }
            }
            currentClass = currentClass.getSuperclass()
        }
        TestRuntimeSettings runtimeSettings = new TestRuntimeSettings(requiredFeatures: allFeatures, pluginUsages: allTestPluginUsages as List)
        SharedRuntime sharedRuntimeAnnotation = findSharedRuntimeAnnotation(testClass)
        TestRuntime runtime
        if(sharedRuntimeAnnotation==null) {
            runtime = INSTANCE.findOrCreateRuntimeForTestClass(testClass, runtimeSettings)
        } else {
            runtime = INSTANCE.findOrCreateSharedRuntime(sharedRuntimeAnnotation.value(), runtimeSettings)
        }
        allInstances.each { testMixinInstance ->
            testMixinInstance.runtime = runtime
        }
        runtime
    }

    private static SharedRuntime findSharedRuntimeAnnotation(Class testClass) {<commit_msg>fix GGTS Groovy compiler problem<commit_after>/*
 * Copyright 2014 the original author or authors.
 *
 * 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 grails.test.runtime

import grails.test.mixin.TestRuntimeAwareMixin
import grails.test.mixin.UseTestPlugin
import grails.test.mixin.support.MixinInstance
import groovy.transform.CompileStatic

import java.lang.reflect.Field
import java.lang.reflect.Modifier

import org.springframework.util.ReflectionUtils

/**
 * TestRuntimeFactory is a singleton that contains the TestPlugin registry and has methods 
 * for getting a TestRuntime instance with given features.
 * 
 * It will resolve the requested features and pick the minimum set of plugins 
 * that are required for covering the requested features.
 * 
 * @author <NAME>
 * @since 2.4.0
 */
@CompileStatic
class TestRuntimeFactory {
    static TestRuntimeFactory INSTANCE=new TestRuntimeFactory()
    Set<Class<? extends TestPlugin>> availablePluginClasses=[
        GrailsApplicationTestPlugin,
        CoreBeansTestPlugin,
        MetaClassCleanerTestPlugin,
        ControllerTestPlugin,
        FiltersTestPlugin,
        GroovyPageTestPlugin,
        WebFlowTestPlugin,
        DomainClassTestPlugin
        ] as Set

    private TestRuntimeFactory() {
    }
    
    static TestRuntime getRuntimeForTestClass(Class<?> testClass) {
        Set<TestRuntimeAwareMixin> allInstances = [] as Set
        Set<String> allFeatures = [] as Set
        Set<TestPluginUsage> allTestPluginUsages = [] as Set
        Class<?> currentClass = testClass
        while(currentClass != Object && currentClass != null) {
            currentClass.getDeclaredFields().each { Field field ->
                if(field.getAnnotation(MixinInstance.class) != null && Modifier.isStatic(field.getModifiers())) {
                    ReflectionUtils.makeAccessible(field)
                    def instance = field.get(null)
                    if(instance instanceof TestRuntimeAwareMixin) {
                        allInstances.add((TestRuntimeAwareMixin)instance)
                        instance.getFeatures() && allFeatures.addAll(instance.getFeatures() as List)
                    }
                    if(instance instanceof TestPluginRegistrar) {
                        instance.getTestPluginUsages() && allTestPluginUsages.addAll(instance.getTestPluginUsages() as List)
                    }
                }
            }
            currentClass = currentClass.getSuperclass()
        }
        TestRuntimeSettings runtimeSettings = new TestRuntimeSettings(requiredFeatures: allFeatures, pluginUsages: allTestPluginUsages as List)
        SharedRuntime sharedRuntimeAnnotation = findSharedRuntimeAnnotation(testClass)
        TestRuntime runtime
        if(sharedRuntimeAnnotation==null) {
            runtime = INSTANCE.findOrCreateRuntimeForTestClass(testClass, runtimeSettings)
        } else {
            runtime = INSTANCE.findOrCreateSharedRuntime(sharedRuntimeAnnotation.value(), runtimeSettings)
        }
        allInstances.each { TestRuntimeAwareMixin testMixinInstance ->
            testMixinInstance.runtime = runtime
        }
        runtime
    }

    private static SharedRuntime findSharedRuntimeAnnotation(Class testClass) {
{'fraction_non_alphanumeric': 0.06981409656279257, 'fraction_numerical': 0.002942356560117694, 'mean_word_length': 2.931650893796004, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1216824', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1788, 'n_words': 509}