View Javadoc
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  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.repository.RepositorySystem;
26  import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader;
27  import org.eclipse.aether.impl.ArtifactDescriptorReader;
28  import org.eclipse.aether.impl.ArtifactResolver;
29  
30  public class ProjectClasspathTest
31      extends AbstractMavenProjectTestCase
32  {
33      static final String dir = "projects/scope/";
34  
35      public void setUp()
36          throws Exception
37      {
38          ArtifactResolver resolver = lookup( ArtifactResolver.class, "classpath" );
39          DefaultArtifactDescriptorReader pomReader = (DefaultArtifactDescriptorReader)lookup(ArtifactDescriptorReader.class);
40          pomReader.setArtifactResolver( resolver );
41  
42          projectBuilder = lookup( ProjectBuilder.class, "classpath" );
43  
44          // the metadata source looks up the default impl, so we have to trick it
45          getContainer().addComponent( projectBuilder, ProjectBuilder.class, "default" );
46  
47          repositorySystem = lookup( RepositorySystem.class );
48      }
49  
50      @Override
51      protected String getCustomConfigurationName()
52      {
53          return null;
54      }
55  
56      public void testProjectClasspath()
57          throws Exception
58      {
59          File f = getFileForClasspathResource( dir + "project-with-scoped-dependencies.xml" );
60  
61          MavenProject project = getProjectWithDependencies( f );
62  
63          Artifact artifact;
64  
65          assertNotNull( "Test project can't be null!", project );
66  
67          checkArtifactIdScope( project, "provided", "provided" );
68          checkArtifactIdScope( project, "test", "test" );
69          checkArtifactIdScope( project, "compile", "compile" );
70          checkArtifactIdScope( project, "runtime", "runtime" );
71          checkArtifactIdScope( project, "default", "compile" );
72  
73          // check all transitive deps of a test dependency are test, except test and provided which is skipped
74          artifact = getArtifact( project, "maven-test-test", "scope-provided" );
75          assertNull( "Check no provided dependencies are transitive", artifact );
76          artifact = getArtifact( project, "maven-test-test", "scope-test" );
77          assertNull( "Check no test dependencies are transitive", artifact );
78  
79          artifact = getArtifact( project, "maven-test-test", "scope-compile" );
80          assertNotNull( artifact );
81  
82          System.out.println( "a = " + artifact );
83          System.out.println( "b = " + artifact.getScope() );
84          assertEquals( "Check scope", "test", artifact.getScope() );
85          artifact = getArtifact( project, "maven-test-test", "scope-default" );
86          assertEquals( "Check scope", "test", artifact.getScope() );
87          artifact = getArtifact( project, "maven-test-test", "scope-runtime" );
88          assertEquals( "Check scope", "test", artifact.getScope() );
89  
90          // check all transitive deps of a provided dependency are provided scope, except for test
91          checkGroupIdScope( project, "provided", "maven-test-provided" );
92          artifact = getArtifact( project, "maven-test-provided", "scope-runtime" );
93          assertEquals( "Check scope", "provided", artifact.getScope() );
94  
95          // check all transitive deps of a runtime dependency are runtime scope, except for test
96          checkGroupIdScope( project, "runtime", "maven-test-runtime" );
97          artifact = getArtifact( project, "maven-test-runtime", "scope-runtime" );
98          assertEquals( "Check scope", "runtime", artifact.getScope() );
99  
100         // check all transitive deps of a compile dependency are compile scope, except for runtime and test
101         checkGroupIdScope( project, "compile", "maven-test-compile" );
102         artifact = getArtifact( project, "maven-test-compile", "scope-runtime" );
103         assertEquals( "Check scope", "runtime", artifact.getScope() );
104 
105         // check all transitive deps of a default dependency are compile scope, except for runtime and test
106         checkGroupIdScope( project, "compile", "maven-test-default" );
107         artifact = getArtifact( project, "maven-test-default", "scope-runtime" );
108         assertEquals( "Check scope", "runtime", artifact.getScope() );
109     }
110 
111     private void checkGroupIdScope( MavenProject project, String scopeValue, String groupId )
112     {
113         Artifact artifact;
114         artifact = getArtifact( project, groupId, "scope-compile" );
115         assertEquals( "Check scope", scopeValue, artifact.getScope() );
116         artifact = getArtifact( project, groupId, "scope-test" );
117         assertNull( "Check test dependency is not transitive", artifact );
118         artifact = getArtifact( project, groupId, "scope-provided" );
119         assertNull( "Check provided dependency is not transitive", artifact );
120         artifact = getArtifact( project, groupId, "scope-default" );
121         assertEquals( "Check scope", scopeValue, artifact.getScope() );
122     }
123 
124     private void checkArtifactIdScope( MavenProject project, String scope, String scopeValue )
125     {
126         String artifactId = "scope-" + scope;
127         Artifact artifact = getArtifact( project, "maven-test", artifactId );
128         assertNotNull( artifact );
129         assertEquals( "Check scope", scopeValue, artifact.getScope() );
130     }
131 
132     private Artifact getArtifact( MavenProject project, String groupId, String artifactId )
133     {
134         System.out.println( "[ Looking for " + groupId + ":" + artifactId + " ]" );
135         for ( Artifact a : project.getArtifacts() )
136         {
137             System.out.println( a.toString() );
138             if ( artifactId.equals( a.getArtifactId() ) && a.getGroupId().equals( groupId ) )
139             {
140                 System.out.println( "RETURN" );
141                 return a;
142             }
143         }
144         System.out.println( "Return null" );
145         return null;
146     }
147 
148 }