001package 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 022import java.io.File; 023 024import org.apache.maven.artifact.Artifact; 025import org.apache.maven.repository.RepositorySystem; 026import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader; 027import org.eclipse.aether.impl.ArtifactDescriptorReader; 028import org.eclipse.aether.impl.ArtifactResolver; 029 030public class ProjectClasspathTest 031 extends AbstractMavenProjectTestCase 032{ 033 static final String dir = "projects/scope/"; 034 035 public void setUp() 036 throws Exception 037 { 038 ArtifactResolver resolver = lookup( ArtifactResolver.class, "classpath" ); 039 DefaultArtifactDescriptorReader pomReader = (DefaultArtifactDescriptorReader)lookup(ArtifactDescriptorReader.class); 040 pomReader.setArtifactResolver( resolver ); 041 042 projectBuilder = lookup( ProjectBuilder.class, "classpath" ); 043 044 // the metadata source looks up the default impl, so we have to trick it 045 getContainer().addComponent( projectBuilder, ProjectBuilder.class, "default" ); 046 047 repositorySystem = lookup( RepositorySystem.class ); 048 } 049 050 @Override 051 protected String getCustomConfigurationName() 052 { 053 return null; 054 } 055 056 public void testProjectClasspath() 057 throws Exception 058 { 059 File f = getFileForClasspathResource( dir + "project-with-scoped-dependencies.xml" ); 060 061 MavenProject project = getProjectWithDependencies( f ); 062 063 Artifact artifact; 064 065 assertNotNull( "Test project can't be null!", project ); 066 067 checkArtifactIdScope( project, "provided", "provided" ); 068 checkArtifactIdScope( project, "test", "test" ); 069 checkArtifactIdScope( project, "compile", "compile" ); 070 checkArtifactIdScope( project, "runtime", "runtime" ); 071 checkArtifactIdScope( project, "default", "compile" ); 072 073 // check all transitive deps of a test dependency are test, except test and provided which is skipped 074 artifact = getArtifact( project, "maven-test-test", "scope-provided" ); 075 assertNull( "Check no provided dependencies are transitive", artifact ); 076 artifact = getArtifact( project, "maven-test-test", "scope-test" ); 077 assertNull( "Check no test dependencies are transitive", artifact ); 078 079 artifact = getArtifact( project, "maven-test-test", "scope-compile" ); 080 assertNotNull( artifact ); 081 082 System.out.println( "a = " + artifact ); 083 System.out.println( "b = " + artifact.getScope() ); 084 assertEquals( "Check scope", "test", artifact.getScope() ); 085 artifact = getArtifact( project, "maven-test-test", "scope-default" ); 086 assertEquals( "Check scope", "test", artifact.getScope() ); 087 artifact = getArtifact( project, "maven-test-test", "scope-runtime" ); 088 assertEquals( "Check scope", "test", artifact.getScope() ); 089 090 // check all transitive deps of a provided dependency are provided scope, except for test 091 checkGroupIdScope( project, "provided", "maven-test-provided" ); 092 artifact = getArtifact( project, "maven-test-provided", "scope-runtime" ); 093 assertEquals( "Check scope", "provided", artifact.getScope() ); 094 095 // check all transitive deps of a runtime dependency are runtime scope, except for test 096 checkGroupIdScope( project, "runtime", "maven-test-runtime" ); 097 artifact = getArtifact( project, "maven-test-runtime", "scope-runtime" ); 098 assertEquals( "Check scope", "runtime", artifact.getScope() ); 099 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}