<commit_before>/*
 * Copyright 2019 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 org.gradle.api.internal.artifacts

import org.gradle.api.attributes.Bundling
import org.gradle.api.attributes.CompatibilityCheckDetails
import org.gradle.api.attributes.CompileView
import org.gradle.api.attributes.MultipleCandidatesDetails
import org.gradle.api.attributes.Usage
import org.gradle.internal.component.model.DefaultMultipleCandidateResult
import org.gradle.util.TestUtil
import spock.lang.Issue
import spock.lang.Specification

import static org.gradle.api.attributes.Bundling.EMBEDDED
import static org.gradle.api.attributes.Bundling.EXTERNAL
import static org.gradle.api.attributes.Bundling.SHADOWED

class JavaEcosystemSupportTest extends Specification {
    def "check usage compatibility rules (consumer value=#consumer, producer value=#producer, compatible=#compatible)"() {
        given:
        JavaEcosystemSupport.UsageCompatibilityRules rules = new JavaEcosystemSupport.UsageCompatibilityRules()
        def details = Mock(CompatibilityCheckDetails)
        when:
        rules.execute(details)

        then:
        1 * details.getConsumerValue() >> usage(consumer)
        1 * details.getProducerValue() >> usage(producer)
        if (producer == consumer) {
            // implementations are NOT required to say "compatible" because
            // they should not even be called in this case
            0 * details._()
        } else if (compatible) {
            1 * details.compatible()
        } else {
            0 * details._()
        }

        where:
        consumer                     | producer                     | compatible
        null                         | Usage.JAVA_API               | true
        null                         | Usage.JAVA_RUNTIME           | true

        Usage.JAVA_API               | Usage.JAVA_API               | true
        Usage.JAVA_API               | Usage.JAVA_RUNTIME           | true

        Usage.JAVA_RUNTIME           | Usage.JAVA_API               | false
        Usage.JAVA_RUNTIME           | Usage.JAVA_RUNTIME           | true

        // Temporary compatibility
        Usage.JAVA_API               | Usage.JAVA_RUNTIME_JARS      | true
        Usage.JAVA_RUNTIME           | Usage.JAVA_RUNTIME_JARS      | true
    }

    @Issue("gradle/gradle#8700")
    def "check usage disambiguation rules (consumer=#consumer, candidates=#candidates, selected=#preferred)"() {
        given:
        JavaEcosystemSupport.UsageDisambiguationRules rules = new JavaEcosystemSupport.UsageDisambiguationRules(
            usage(Usage.JAVA_API),
            usage(JavaEcosystemSupport.DEPRECATED_JAVA_API_JARS),
            usage(Usage.JAVA_RUNTIME),
            usage(JavaEcosystemSupport.DEPRECATED_JAVA_RUNTIME_JARS)
        )
        MultipleCandidatesDetails details = new DefaultMultipleCandidateResult(usage(consumer), candidates.collect { usage(it)} as Set)

        when:
        rules.execute(details)

        then:
        details.hasResult()
        !details.matches.empty
        details.matches == [usage(preferred)] as Set

        details

        where: // not exhaustive, tests pathological cases
        consumer                | candidates                                     | preferred
        Usage.JAVA_API          | [Usage.JAVA_API, Usage.JAVA_RUNTIME]           | Usage.JAVA_API
        Usage.JAVA_RUNTIME      | [Usage.JAVA_RUNTIME, Usage.JAVA_API]           | Usage.JAVA_RUNTIME

        //Temporary compatibility
        Usage.JAVA_API          | [Usage.JAVA_API_JARS, Usage.JAVA_RUNTIME_JARS] | Usage.JAVA_API_JARS
        Usage.JAVA_RUNTIME      | [Usage.JAVA_API, Usage.JAVA_RUNTIME_JARS]      | Usage.JAVA_RUNTIME_JARS

        // while unlikely that a candidate would expose both JAVA_API_JARS and JAVA_API,
        // this confirms that JAVA_API_JARS takes precedence, per JavaEcosystemSupport
        Usage.JAVA_API          | [Usage.JAVA_API_JARS, Usage.JAVA_API, Usage.JAVA_RUNTIME_JARS] | Usage.JAVA_API_JARS
    }

    def "check compile-view compatibility rules consumer=#consumer producer=#producer compatible=#compatible"() {
        CompatibilityCheckDetails details = Mock(CompatibilityCheckDetails)

        when:
        new JavaEcosystemSupport.CompileViewCompatibilityRules().execute(details)

        then:
        1 * details.getConsumerValue() >> compileView(consumer)
        1 * details.getProducerValue() >> compileView(producer)

        if (compatible && !(consumer == producer)) {<commit_msg>Update references to migrated constants in tests<commit_after>/*
 * Copyright 2019 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 org.gradle.api.internal.artifacts

import org.gradle.api.attributes.Bundling
import org.gradle.api.attributes.CompatibilityCheckDetails
import org.gradle.api.attributes.CompileView
import org.gradle.api.attributes.MultipleCandidatesDetails
import org.gradle.api.attributes.Usage
import org.gradle.internal.component.model.DefaultMultipleCandidateResult
import org.gradle.util.TestUtil
import spock.lang.Issue
import spock.lang.Specification

import static org.gradle.api.attributes.Bundling.EMBEDDED
import static org.gradle.api.attributes.Bundling.EXTERNAL
import static org.gradle.api.attributes.Bundling.SHADOWED

class JavaEcosystemSupportTest extends Specification {
    def "check usage compatibility rules (consumer value=#consumer, producer value=#producer, compatible=#compatible)"() {
        given:
        JavaEcosystemSupport.UsageCompatibilityRules rules = new JavaEcosystemSupport.UsageCompatibilityRules()
        def details = Mock(CompatibilityCheckDetails)
        when:
        rules.execute(details)

        then:
        1 * details.getConsumerValue() >> usage(consumer)
        1 * details.getProducerValue() >> usage(producer)
        if (producer == consumer) {
            // implementations are NOT required to say "compatible" because
            // they should not even be called in this case
            0 * details._()
        } else if (compatible) {
            1 * details.compatible()
        } else {
            0 * details._()
        }

        where:
        consumer                     | producer                                             | compatible
        null                         | Usage.JAVA_API                                       | true
        null                         | Usage.JAVA_RUNTIME                                   | true

        Usage.JAVA_API               | Usage.JAVA_API                                       | true
        Usage.JAVA_API               | Usage.JAVA_RUNTIME                                   | true

        Usage.JAVA_RUNTIME           | Usage.JAVA_API                                       | false
        Usage.JAVA_RUNTIME           | Usage.JAVA_RUNTIME                                   | true

        // Temporary compatibility
        Usage.JAVA_API               | JavaEcosystemSupport.DEPRECATED_JAVA_RUNTIME_JARS      | true
        Usage.JAVA_RUNTIME           | JavaEcosystemSupport.DEPRECATED_JAVA_RUNTIME_JARS      | true
    }

    @Issue("gradle/gradle#8700")
    def "check usage disambiguation rules (consumer=#consumer, candidates=#candidates, selected=#preferred)"() {
        given:
        JavaEcosystemSupport.UsageDisambiguationRules rules = new JavaEcosystemSupport.UsageDisambiguationRules(
            usage(Usage.JAVA_API),
            usage(JavaEcosystemSupport.DEPRECATED_JAVA_API_JARS),
            usage(Usage.JAVA_RUNTIME),
            usage(JavaEcosystemSupport.DEPRECATED_JAVA_RUNTIME_JARS)
        )
        MultipleCandidatesDetails details = new DefaultMultipleCandidateResult(usage(consumer), candidates.collect { usage(it)} as Set)

        when:
        rules.execute(details)

        then:
        details.hasResult()
        !details.matches.empty
        details.matches == [usage(preferred)] as Set

        details

        where: // not exhaustive, tests pathological cases
        consumer                | candidates                                                                                                        | preferred
        Usage.JAVA_API          | [Usage.JAVA_API, Usage.JAVA_RUNTIME]                                                                              | Usage.JAVA_API
        Usage.JAVA_RUNTIME      | [Usage.JAVA_RUNTIME, Usage.JAVA_API]                                                                              | Usage.JAVA_RUNTIME

        //Temporary compatibility
        Usage.JAVA_API          | [JavaEcosystemSupport.DEPRECATED_JAVA_API_JARS, JavaEcosystemSupport.DEPRECATED_JAVA_RUNTIME_JARS]                | JavaEcosystemSupport.DEPRECATED_JAVA_API_JARS
        Usage.JAVA_RUNTIME      | [Usage.JAVA_API, JavaEcosystemSupport.DEPRECATED_JAVA_RUNTIME_JARS]                                               | JavaEcosystemSupport.DEPRECATED_JAVA_RUNTIME_JARS

        // while unlikely that a candidate would expose both JAVA_API_JARS and JAVA_API,
        // this confirms that JAVA_API_JARS takes precedence, per JavaEcosystemSupport
        Usage.JAVA_API          | [JavaEcosystemSupport.DEPRECATED_JAVA_API_JARS, Usage.JAVA_API, JavaEcosystemSupport.DEPRECATED_JAVA_RUNTIME_JARS] | JavaEcosystemSupport.DEPRECATED_JAVA_API_JARS
    }

    def "check compile-view compatibility rules consumer=#consumer producer=#producer compatible=#compatible"() {
        CompatibilityCheckDetails details = Mock(CompatibilityCheckDetails)

        when:
        new JavaEcosystemSupport.CompileViewCompatibilityRules().execute(details)

        then:
        1 * details.getConsumerValue() >> compileView(consumer)
        1 * details.getProducerValue() >> compileView(producer)

        if (compatible && !(consumer == producer)) {
{'fraction_non_alphanumeric': 0.07195436142804564, 'fraction_numerical': 0.0034965034965034965, 'mean_word_length': 2.2099822799763733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1469678', 'n_tokens_mistral': 3038, 'n_tokens_neox': 2715, 'n_words': 642}