001: package org.apache.maven.project;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.maven.artifact.Artifact;
023:
024: import java.io.File;
025: import java.util.Iterator;
026:
027: /**
028: * @todo relocate to maven-artifact in entirety
029: */
030: public class ProjectClasspathTest extends AbstractMavenProjectTestCase {
031: private String dir = "projects/scope/";
032:
033: public void testProjectClasspath() throws Exception {
034: File f = getFileForClasspathResource(dir
035: + "project-with-scoped-dependencies.xml");
036:
037: // assertEquals( TestArtifactResolver.class, getContainer().lookup( ArtifactResolver.ROLE ).getClass() );
038: TestProjectBuilder builder = (TestProjectBuilder) getContainer()
039: .lookup(MavenProjectBuilder.ROLE, "test");
040:
041: TestArtifactResolver testArtifactResolver = (TestArtifactResolver) getContainer()
042: .lookup(TestArtifactResolver.class.getName());
043:
044: builder.setArtifactResolver(testArtifactResolver);
045: builder
046: .setArtifactMetadataSource(testArtifactResolver
047: .source());
048:
049: MavenProject project = getProjectWithDependencies(f);
050:
051: Artifact artifact;
052:
053: assertNotNull("Test project can't be null!", project);
054:
055: checkArtifactIdScope(project, "provided", "provided");
056: checkArtifactIdScope(project, "test", "test");
057: checkArtifactIdScope(project, "compile", "compile");
058: checkArtifactIdScope(project, "runtime", "runtime");
059: checkArtifactIdScope(project, "default", "compile");
060:
061: // check all transitive deps of a test dependency are test, except test and provided which is skipped
062: artifact = getArtifact(project, "maven-test-test",
063: "scope-provided");
064: assertNull("Check no provided dependencies are transitive",
065: artifact);
066: artifact = getArtifact(project, "maven-test-test", "scope-test");
067: assertNull("Check no test dependencies are transitive",
068: artifact);
069: artifact = getArtifact(project, "maven-test-test",
070: "scope-compile");
071: assertEquals("Check scope", "test", artifact.getScope());
072: artifact = getArtifact(project, "maven-test-test",
073: "scope-default");
074: assertEquals("Check scope", "test", artifact.getScope());
075: artifact = getArtifact(project, "maven-test-test",
076: "scope-runtime");
077: assertEquals("Check scope", "test", artifact.getScope());
078:
079: // check all transitive deps of a provided dependency are provided scope, except for test
080: checkGroupIdScope(project, "provided", "maven-test-provided");
081: artifact = getArtifact(project, "maven-test-provided",
082: "scope-runtime");
083: assertEquals("Check scope", "provided", artifact.getScope());
084:
085: // check all transitive deps of a runtime dependency are runtime scope, except for test
086: checkGroupIdScope(project, "runtime", "maven-test-runtime");
087: artifact = getArtifact(project, "maven-test-runtime",
088: "scope-runtime");
089: assertEquals("Check scope", "runtime", artifact.getScope());
090:
091: // check all transitive deps of a compile dependency are compile scope, except for runtime and test
092: checkGroupIdScope(project, "compile", "maven-test-compile");
093: artifact = getArtifact(project, "maven-test-compile",
094: "scope-runtime");
095: assertEquals("Check scope", "runtime", artifact.getScope());
096:
097: // check all transitive deps of a default dependency are compile scope, except for runtime and test
098: checkGroupIdScope(project, "compile", "maven-test-default");
099: artifact = getArtifact(project, "maven-test-default",
100: "scope-runtime");
101: assertEquals("Check scope", "runtime", artifact.getScope());
102: }
103:
104: private void checkGroupIdScope(MavenProject project,
105: String scopeValue, String groupId) {
106: Artifact artifact;
107: artifact = getArtifact(project, groupId, "scope-compile");
108: assertEquals("Check scope", scopeValue, artifact.getScope());
109: artifact = getArtifact(project, groupId, "scope-test");
110: assertNull("Check test dependency is not transitive", artifact);
111: artifact = getArtifact(project, groupId, "scope-provided");
112: assertNull("Check provided dependency is not transitive",
113: artifact);
114: artifact = getArtifact(project, groupId, "scope-default");
115: assertEquals("Check scope", scopeValue, artifact.getScope());
116: }
117:
118: private void checkArtifactIdScope(MavenProject project,
119: String scope, String scopeValue) {
120: String artifactId = "scope-" + scope;
121: Artifact artifact = getArtifact(project, "maven-test",
122: artifactId);
123: assertEquals("Check scope", scopeValue, artifact.getScope());
124: }
125:
126: private Artifact getArtifact(MavenProject project, String groupId,
127: String artifactId) {
128: for (Iterator i = project.getArtifacts().iterator(); i
129: .hasNext();) {
130: Artifact a = (Artifact) i.next();
131: if (artifactId.equals(a.getArtifactId())
132: && a.getGroupId().equals(groupId)) {
133: return a;
134: }
135: }
136: return null;
137: }
138: }
|