/*
 * Copyright 2011 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.artifacts.ResolvedArtifact
import spock.lang.Specification

class DefaultResolvedDependencySpec extends Specification {
    final DefaultResolvedDependency dependency = new DefaultResolvedDependency("name", "group", "module", "version", "config")

    def "provides meta-data about the module"() {
        expect:
        dependency.module.id.group == "group"
        dependency.module.id.name == "module"
        dependency.module.id.version == "version"
    }

    def "artifacts are ordered by name then classifier then extension then type"() {
        ResolvedArtifact artifact1 = artifact("a", null, "jar", "jar")
        ResolvedArtifact artifact2 = artifact("b", null, "jar", "jar")
        ResolvedArtifact artifact3 = artifact("b", "a-classifier", "jar", "jar")
        ResolvedArtifact artifact4 = artifact("b", "b-classifier", "b-type", "a-ext")
        ResolvedArtifact artifact5 = artifact("b", "b-classifier", "a-type", "b-ext")
        ResolvedArtifact artifact6 = artifact("b", "b-classifier", "b-type", "b-ext")
        ResolvedArtifact artifact7 = artifact("c", "a-classifier", "jar", "jar")

        given:
        dependency.addModuleArtifact(artifact6)
        dependency.addModuleArtifact(artifact1)
        dependency.addModuleArtifact(artifact3)
        dependency.addModuleArtifact(artifact5)
        dependency.addModuleArtifact(artifact2)
        dependency.addModuleArtifact(artifact7)
        dependency.addModuleArtifact(artifact4)

        expect:
        dependency.moduleArtifacts as List == [artifact1, artifact2, artifact3, artifact4, artifact5, artifact6, artifact7]
    }

    def "does not discard artifacts with the same name and classifier and extension and type"() {
        ResolvedArtifact artifact1 = artifact("a", null, "jar", "jar")
        ResolvedArtifact artifact2 = artifact("a", null, "jar", "jar")

        given:
        dependency.addModuleArtifact(artifact1)
        dependency.addModuleArtifact(artifact2)

        expect:
        dependency.moduleArtifacts == [artifact1, artifact2] as Set
    }

    def "parent specific artifacts are ordered by name then classifier then extension then type"() {
        ResolvedArtifact artifact1 = artifact("a", null, "jar", "jar")
        ResolvedArtifact artifact2 = artifact("b", null, "jar", "jar")
        ResolvedArtifact artifact3 = artifact("b", "a-classifier", "jar", "jar")
        ResolvedArtifact artifact4 = artifact("b", "b-classifier", "b-type", "a-ext")
        ResolvedArtifact artifact5 = artifact("b", "b-classifier", "a-type", "b-ext")
        ResolvedArtifact artifact6 = artifact("b", "b-classifier", "b-type", "b-ext")
        ResolvedArtifact artifact7 = artifact("c", "a-classifier", "jar", "jar")
        DefaultResolvedDependency parent = Mock()

        given:
        dependency.parents.add(parent)
        dependency.addParentSpecificArtifacts(parent, [artifact6, artifact1, artifact7, artifact5, artifact2, artifact3, artifact4] as Set)

        expect:
        dependency.getParentArtifacts(parent) as List == [artifact1, artifact2, artifact3, artifact4, artifact5, artifact6, artifact7]
    }

    def artifact(String name, String classifier, String type, String extension) {
        ResolvedArtifact artifact = Mock()
        _ * artifact.toString() >> "$name-$classifier-$type.$extension"
        _ * artifact.name >> name
        _ * artifact.classifier >> classifier
        _ * artifact.type >> type
        _ * artifact.extension >> extension
        return artifact
    }
}
