/*
 * Copyright 2009 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.ivyservice;

import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.DependencySet;
import org.gradle.api.artifacts.ResolvedConfiguration;
import org.gradle.api.internal.artifacts.ArtifactDependencyResolver;
import org.gradle.api.internal.artifacts.configurations.ConfigurationInternal;
import org.gradle.api.specs.Specs;
import org.jmock.Expectations;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.gradle.util.Matchers.isEmpty;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;

@RunWith(JMock.class)
public class ShortcircuitEmptyConfigsArtifactDependencyResolverTest {
    private final JUnit4Mockery context = new JUnit4Mockery();
    private final ArtifactDependencyResolver delegate = context.mock(ArtifactDependencyResolver.class);
    private final ConfigurationInternal configuration = context.mock(ConfigurationInternal.class);
    private final DependencySet dependencies = context.mock(DependencySet.class);
    private final ShortcircuitEmptyConfigsArtifactDependencyResolver dependencyResolver = new ShortcircuitEmptyConfigsArtifactDependencyResolver(delegate);

    @Test
    public void resolveReturnsEmptyResolvedConfigWhenConfigHasNoDependencies() {
        context.checking(new Expectations(){{
            allowing(configuration).getAllDependencies();
            will(returnValue(dependencies));

            allowing(dependencies).isEmpty();
            will(returnValue(true));
        }});

        ResolvedConfiguration resolvedConfig = dependencyResolver.resolve(configuration);

        assertFalse(resolvedConfig.hasError());
        resolvedConfig.rethrowFailure();
        assertThat(resolvedConfig.getFiles(Specs.<Dependency>satisfyAll()), isEmpty());
        assertThat(resolvedConfig.getFirstLevelModuleDependencies(), isEmpty());
        assertThat(resolvedConfig.getResolvedArtifacts(), isEmpty());
    }

    @Test
    public void resolveDelegatesToBackingServiceWhenConfigHasDependencies() {
        final ResolvedConfiguration resolvedConfigDummy = context.mock(ResolvedConfiguration.class);

        context.checking(new Expectations() {{
            allowing(configuration).getAllDependencies();
            will(returnValue(dependencies));

            allowing(dependencies).isEmpty();
            will(returnValue(false));

            one(delegate).resolve(configuration);
            will(returnValue(resolvedConfigDummy));
        }});

        assertThat(dependencyResolver.resolve(configuration), sameInstance(resolvedConfigDummy));
    }
}
