1   package org.apache.maven.project;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  
24  import java.io.File;
25  import java.util.Iterator;
26  
27  /**
28   * @todo relocate to maven-artifact in entirety
29   */
30  public class ProjectClasspathTest
31      extends AbstractMavenProjectTestCase
32  {
33      private String dir = "projects/scope/";
34  
35      public void testProjectClasspath()
36          throws Exception
37      {
38          File f = getFileForClasspathResource( dir + "project-with-scoped-dependencies.xml" );
39  
40  //        assertEquals( TestArtifactResolver.class, getContainer().lookup( ArtifactResolver.ROLE ).getClass() );
41          TestProjectBuilder builder = (TestProjectBuilder) getContainer().lookup( MavenProjectBuilder.ROLE, "test" );
42          
43          TestArtifactResolver testArtifactResolver = (TestArtifactResolver) getContainer().lookup( TestArtifactResolver.class.getName() );
44          
45          builder.setArtifactResolver( testArtifactResolver );
46          builder.setArtifactMetadataSource( testArtifactResolver.source() );
47          
48          MavenProject project = getProjectWithDependencies( f );
49  
50          Artifact artifact;
51  
52          assertNotNull( "Test project can't be null!", project );
53  
54          checkArtifactIdScope( project, "provided", "provided" );
55          checkArtifactIdScope( project, "test", "test" );
56          checkArtifactIdScope( project, "compile", "compile" );
57          checkArtifactIdScope( project, "runtime", "runtime" );
58          checkArtifactIdScope( project, "default", "compile" );
59  
60          // check all transitive deps of a test dependency are test, except test and provided which is skipped
61          artifact = getArtifact( project, "maven-test-test", "scope-provided" );
62          assertNull( "Check no provided dependencies are transitive", artifact );
63          artifact = getArtifact( project, "maven-test-test", "scope-test" );
64          assertNull( "Check no test dependencies are transitive", artifact );
65          artifact = getArtifact( project, "maven-test-test", "scope-compile" );
66          assertEquals( "Check scope", "test", artifact.getScope() );
67          artifact = getArtifact( project, "maven-test-test", "scope-default" );
68          assertEquals( "Check scope", "test", artifact.getScope() );
69          artifact = getArtifact( project, "maven-test-test", "scope-runtime" );
70          assertEquals( "Check scope", "test", artifact.getScope() );
71  
72          // check all transitive deps of a provided dependency are provided scope, except for test
73          checkGroupIdScope( project, "provided", "maven-test-provided" );
74          artifact = getArtifact( project, "maven-test-provided", "scope-runtime" );
75          assertEquals( "Check scope", "provided", artifact.getScope() );
76  
77          // check all transitive deps of a runtime dependency are runtime scope, except for test
78          checkGroupIdScope( project, "runtime", "maven-test-runtime" );
79          artifact = getArtifact( project, "maven-test-runtime", "scope-runtime" );
80          assertEquals( "Check scope", "runtime", artifact.getScope() );
81  
82          // check all transitive deps of a compile dependency are compile scope, except for runtime and test
83          checkGroupIdScope( project, "compile", "maven-test-compile" );
84          artifact = getArtifact( project, "maven-test-compile", "scope-runtime" );
85          assertEquals( "Check scope", "runtime", artifact.getScope() );
86  
87          // check all transitive deps of a default dependency are compile scope, except for runtime and test
88          checkGroupIdScope( project, "compile", "maven-test-default" );
89          artifact = getArtifact( project, "maven-test-default", "scope-runtime" );
90          assertEquals( "Check scope", "runtime", artifact.getScope() );
91      }
92  
93      private void checkGroupIdScope( MavenProject project, String scopeValue, String groupId )
94      {
95          Artifact artifact;
96          artifact = getArtifact( project, groupId, "scope-compile" );
97          assertEquals( "Check scope", scopeValue, artifact.getScope() );
98          artifact = getArtifact( project, groupId, "scope-test" );
99          assertNull( "Check test dependency is not transitive", artifact );
100         artifact = getArtifact( project, groupId, "scope-provided" );
101         assertNull( "Check provided dependency is not transitive", artifact );
102         artifact = getArtifact( project, groupId, "scope-default" );
103         assertEquals( "Check scope", scopeValue, artifact.getScope() );
104     }
105 
106     private void checkArtifactIdScope( MavenProject project, String scope, String scopeValue )
107     {
108         String artifactId = "scope-" + scope;
109         Artifact artifact = getArtifact( project, "maven-test", artifactId );
110         assertEquals( "Check scope", scopeValue, artifact.getScope() );
111     }
112 
113     private Artifact getArtifact( MavenProject project, String groupId, String artifactId )
114     {
115         for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); )
116         {
117             Artifact a = (Artifact) i.next();
118             if ( artifactId.equals( a.getArtifactId() ) && a.getGroupId().equals( groupId ) )
119             {
120                 return a;
121             }
122         }
123         return null;
124     }
125 }