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 java.io.File;
23  import java.util.Iterator;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.repository.RepositorySystem;
27  import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader;
28  import org.sonatype.aether.impl.ArtifactDescriptorReader;
29  import org.sonatype.aether.impl.ArtifactResolver;
30  
31  public class ProjectClasspathTest
32      extends AbstractMavenProjectTestCase
33  {
34      static final String dir = "projects/scope/";
35  
36      public void setUp()
37          throws Exception
38      {
39          ArtifactResolver resolver = lookup( ArtifactResolver.class, "classpath" );
40          DefaultArtifactDescriptorReader pomReader = (DefaultArtifactDescriptorReader)lookup(ArtifactDescriptorReader.class);
41          pomReader.setArtifactResolver( resolver );
42  
43          projectBuilder = lookup( ProjectBuilder.class, "classpath" );
44  
45          // the metadata source looks up the default impl, so we have to trick it
46          getContainer().addComponent( projectBuilder, ProjectBuilder.class, "default" );
47  
48          repositorySystem = lookup( RepositorySystem.class );        
49      }
50     
51      @Override
52      protected String getCustomConfigurationName()
53      {
54          return null;
55      }
56  
57      public void testProjectClasspath()
58          throws Exception
59      {
60          File f = getFileForClasspathResource( dir + "project-with-scoped-dependencies.xml" );
61  
62          MavenProject project = getProjectWithDependencies( f );
63  
64          Artifact artifact;
65  
66          assertNotNull( "Test project can't be null!", project );
67  
68          checkArtifactIdScope( project, "provided", "provided" );
69          checkArtifactIdScope( project, "test", "test" );
70          checkArtifactIdScope( project, "compile", "compile" );
71          checkArtifactIdScope( project, "runtime", "runtime" );
72          checkArtifactIdScope( project, "default", "compile" );
73  
74          // check all transitive deps of a test dependency are test, except test and provided which is skipped
75          artifact = getArtifact( project, "maven-test-test", "scope-provided" );
76          assertNull( "Check no provided dependencies are transitive", artifact );
77          artifact = getArtifact( project, "maven-test-test", "scope-test" );
78          assertNull( "Check no test dependencies are transitive", artifact );
79  
80          artifact = getArtifact( project, "maven-test-test", "scope-compile" );
81          assertNotNull( artifact );
82  
83          System.out.println( "a = " + artifact );
84          System.out.println( "b = " + artifact.getScope() );
85          assertEquals( "Check scope", "test", artifact.getScope() );
86          artifact = getArtifact( project, "maven-test-test", "scope-default" );
87          assertEquals( "Check scope", "test", artifact.getScope() );
88          artifact = getArtifact( project, "maven-test-test", "scope-runtime" );
89          assertEquals( "Check scope", "test", artifact.getScope() );
90  
91          // check all transitive deps of a provided dependency are provided scope, except for test
92          checkGroupIdScope( project, "provided", "maven-test-provided" );
93          artifact = getArtifact( project, "maven-test-provided", "scope-runtime" );
94          assertEquals( "Check scope", "provided", artifact.getScope() );
95  
96          // check all transitive deps of a runtime dependency are runtime scope, except for test
97          checkGroupIdScope( project, "runtime", "maven-test-runtime" );
98          artifact = getArtifact( project, "maven-test-runtime", "scope-runtime" );
99          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 ( Iterator<Artifact> i = project.getArtifacts().iterator(); i.hasNext(); )
137         {
138             Artifact a = i.next();
139             System.out.println( a.toString() );
140             if ( artifactId.equals( a.getArtifactId() ) && a.getGroupId().equals( groupId ) )
141             {
142                 System.out.println( "RETURN" );
143                 return a;
144             }
145         }
146         System.out.println( "Return null" );
147         return null;
148     }
149 
150 }