1 package org.apache.maven.project;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
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
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
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
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 }