001package org.apache.maven.project;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.Iterator;
024
025import org.apache.maven.artifact.Artifact;
026import org.apache.maven.repository.RepositorySystem;
027import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader;
028import org.eclipse.aether.impl.ArtifactDescriptorReader;
029import org.eclipse.aether.impl.ArtifactResolver;
030
031public class ProjectClasspathTest
032    extends AbstractMavenProjectTestCase
033{
034    static final String dir = "projects/scope/";
035
036    public void setUp()
037        throws Exception
038    {
039        ArtifactResolver resolver = lookup( ArtifactResolver.class, "classpath" );
040        DefaultArtifactDescriptorReader pomReader = (DefaultArtifactDescriptorReader)lookup(ArtifactDescriptorReader.class);
041        pomReader.setArtifactResolver( resolver );
042
043        projectBuilder = lookup( ProjectBuilder.class, "classpath" );
044
045        // the metadata source looks up the default impl, so we have to trick it
046        getContainer().addComponent( projectBuilder, ProjectBuilder.class, "default" );
047
048        repositorySystem = lookup( RepositorySystem.class );
049    }
050
051    @Override
052    protected String getCustomConfigurationName()
053    {
054        return null;
055    }
056
057    public void testProjectClasspath()
058        throws Exception
059    {
060        File f = getFileForClasspathResource( dir + "project-with-scoped-dependencies.xml" );
061
062        MavenProject project = getProjectWithDependencies( f );
063
064        Artifact artifact;
065
066        assertNotNull( "Test project can't be null!", project );
067
068        checkArtifactIdScope( project, "provided", "provided" );
069        checkArtifactIdScope( project, "test", "test" );
070        checkArtifactIdScope( project, "compile", "compile" );
071        checkArtifactIdScope( project, "runtime", "runtime" );
072        checkArtifactIdScope( project, "default", "compile" );
073
074        // check all transitive deps of a test dependency are test, except test and provided which is skipped
075        artifact = getArtifact( project, "maven-test-test", "scope-provided" );
076        assertNull( "Check no provided dependencies are transitive", artifact );
077        artifact = getArtifact( project, "maven-test-test", "scope-test" );
078        assertNull( "Check no test dependencies are transitive", artifact );
079
080        artifact = getArtifact( project, "maven-test-test", "scope-compile" );
081        assertNotNull( artifact );
082
083        System.out.println( "a = " + artifact );
084        System.out.println( "b = " + artifact.getScope() );
085        assertEquals( "Check scope", "test", artifact.getScope() );
086        artifact = getArtifact( project, "maven-test-test", "scope-default" );
087        assertEquals( "Check scope", "test", artifact.getScope() );
088        artifact = getArtifact( project, "maven-test-test", "scope-runtime" );
089        assertEquals( "Check scope", "test", artifact.getScope() );
090
091        // check all transitive deps of a provided dependency are provided scope, except for test
092        checkGroupIdScope( project, "provided", "maven-test-provided" );
093        artifact = getArtifact( project, "maven-test-provided", "scope-runtime" );
094        assertEquals( "Check scope", "provided", artifact.getScope() );
095
096        // check all transitive deps of a runtime dependency are runtime scope, except for test
097        checkGroupIdScope( project, "runtime", "maven-test-runtime" );
098        artifact = getArtifact( project, "maven-test-runtime", "scope-runtime" );
099        assertEquals( "Check scope", "runtime", artifact.getScope() );
100
101        // check all transitive deps of a compile dependency are compile scope, except for runtime and test
102        checkGroupIdScope( project, "compile", "maven-test-compile" );
103        artifact = getArtifact( project, "maven-test-compile", "scope-runtime" );
104        assertEquals( "Check scope", "runtime", artifact.getScope() );
105
106        // check all transitive deps of a default dependency are compile scope, except for runtime and test
107        checkGroupIdScope( project, "compile", "maven-test-default" );
108        artifact = getArtifact( project, "maven-test-default", "scope-runtime" );
109        assertEquals( "Check scope", "runtime", artifact.getScope() );
110    }
111
112    private void checkGroupIdScope( MavenProject project, String scopeValue, String groupId )
113    {
114        Artifact artifact;
115        artifact = getArtifact( project, groupId, "scope-compile" );
116        assertEquals( "Check scope", scopeValue, artifact.getScope() );
117        artifact = getArtifact( project, groupId, "scope-test" );
118        assertNull( "Check test dependency is not transitive", artifact );
119        artifact = getArtifact( project, groupId, "scope-provided" );
120        assertNull( "Check provided dependency is not transitive", artifact );
121        artifact = getArtifact( project, groupId, "scope-default" );
122        assertEquals( "Check scope", scopeValue, artifact.getScope() );
123    }
124
125    private void checkArtifactIdScope( MavenProject project, String scope, String scopeValue )
126    {
127        String artifactId = "scope-" + scope;
128        Artifact artifact = getArtifact( project, "maven-test", artifactId );
129        assertNotNull( artifact );
130        assertEquals( "Check scope", scopeValue, artifact.getScope() );
131    }
132
133    private Artifact getArtifact( MavenProject project, String groupId, String artifactId )
134    {
135        System.out.println( "[ Looking for " + groupId + ":" + artifactId + " ]" );
136        for ( Artifact a : project.getArtifacts() )
137        {
138            System.out.println( a.toString() );
139            if ( artifactId.equals( a.getArtifactId() ) && a.getGroupId().equals( groupId ) )
140            {
141                System.out.println( "RETURN" );
142                return a;
143            }
144        }
145        System.out.println( "Return null" );
146        return null;
147    }
148
149}