KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > project > ProjectClasspathTest


1 package org.apache.maven.project;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.artifact.Artifact;
20
21 import java.io.File JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 /**
25  * @todo relocate to maven-artifact in entirety
26  */

27 public class ProjectClasspathTest
28     extends AbstractMavenProjectTestCase
29 {
30     private String JavaDoc dir = "projects/scope/";
31
32     public void testProjectClasspath()
33         throws Exception JavaDoc
34     {
35         File JavaDoc f = getFileForClasspathResource( dir + "project-with-scoped-dependencies.xml" );
36
37 // assertEquals( TestArtifactResolver.class, getContainer().lookup( ArtifactResolver.ROLE ).getClass() );
38
TestProjectBuilder builder = (TestProjectBuilder) getContainer().lookup( MavenProjectBuilder.ROLE, "test" );
39         
40         TestArtifactResolver testArtifactResolver = (TestArtifactResolver) getContainer().lookup( TestArtifactResolver.class.getName() );
41         
42         builder.setArtifactResolver( testArtifactResolver );
43         builder.setArtifactMetadataSource( testArtifactResolver.source() );
44         
45         MavenProject project = getProjectWithDependencies( f );
46
47         Artifact artifact;
48
49         assertNotNull( "Test project can't be null!", project );
50
51         checkArtifactIdScope( project, "provided", "provided" );
52         checkArtifactIdScope( project, "test", "test" );
53         checkArtifactIdScope( project, "compile", "compile" );
54         checkArtifactIdScope( project, "runtime", "runtime" );
55         checkArtifactIdScope( project, "default", "compile" );
56
57         // check all transitive deps of a test dependency are test, except test and provided which is skipped
58
artifact = getArtifact( project, "maven-test-test", "scope-provided" );
59         assertNull( "Check no provided dependencies are transitive", artifact );
60         artifact = getArtifact( project, "maven-test-test", "scope-test" );
61         assertNull( "Check no test dependencies are transitive", artifact );
62         artifact = getArtifact( project, "maven-test-test", "scope-compile" );
63         assertEquals( "Check scope", "test", artifact.getScope() );
64         artifact = getArtifact( project, "maven-test-test", "scope-default" );
65         assertEquals( "Check scope", "test", artifact.getScope() );
66         artifact = getArtifact( project, "maven-test-test", "scope-runtime" );
67         assertEquals( "Check scope", "test", artifact.getScope() );
68
69         // check all transitive deps of a provided dependency are provided scope, except for test
70
checkGroupIdScope( project, "provided", "maven-test-provided" );
71         artifact = getArtifact( project, "maven-test-provided", "scope-runtime" );
72         assertEquals( "Check scope", "provided", artifact.getScope() );
73
74         // check all transitive deps of a runtime dependency are runtime scope, except for test
75
checkGroupIdScope( project, "runtime", "maven-test-runtime" );
76         artifact = getArtifact( project, "maven-test-runtime", "scope-runtime" );
77         assertEquals( "Check scope", "runtime", artifact.getScope() );
78
79         // check all transitive deps of a compile dependency are compile scope, except for runtime and test
80
checkGroupIdScope( project, "compile", "maven-test-compile" );
81         artifact = getArtifact( project, "maven-test-compile", "scope-runtime" );
82         assertEquals( "Check scope", "runtime", artifact.getScope() );
83
84         // check all transitive deps of a default dependency are compile scope, except for runtime and test
85
checkGroupIdScope( project, "compile", "maven-test-default" );
86         artifact = getArtifact( project, "maven-test-default", "scope-runtime" );
87         assertEquals( "Check scope", "runtime", artifact.getScope() );
88     }
89
90     private void checkGroupIdScope( MavenProject project, String JavaDoc scopeValue, String JavaDoc groupId )
91     {
92         Artifact artifact;
93         artifact = getArtifact( project, groupId, "scope-compile" );
94         assertEquals( "Check scope", scopeValue, artifact.getScope() );
95         artifact = getArtifact( project, groupId, "scope-test" );
96         assertNull( "Check test dependency is not transitive", artifact );
97         artifact = getArtifact( project, groupId, "scope-provided" );
98         assertNull( "Check provided dependency is not transitive", artifact );
99         artifact = getArtifact( project, groupId, "scope-default" );
100         assertEquals( "Check scope", scopeValue, artifact.getScope() );
101     }
102
103     private void checkArtifactIdScope( MavenProject project, String JavaDoc scope, String JavaDoc scopeValue )
104     {
105         String JavaDoc artifactId = "scope-" + scope;
106         Artifact artifact = getArtifact( project, "maven-test", artifactId );
107         assertEquals( "Check scope", scopeValue, artifact.getScope() );
108     }
109
110     private Artifact getArtifact( MavenProject project, String JavaDoc groupId, String JavaDoc artifactId )
111     {
112         for ( Iterator JavaDoc i = project.getArtifacts().iterator(); i.hasNext(); )
113         {
114             Artifact a = (Artifact) i.next();
115             if ( artifactId.equals( a.getArtifactId() ) && a.getGroupId().equals( groupId ) )
116             {
117                 return a;
118             }
119         }
120         return null;
121     }
122 }
123
Popular Tags