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; 023import java.io.IOException; 024import java.util.List; 025import java.util.Map; 026import java.util.concurrent.atomic.AtomicInteger; 027 028import org.apache.maven.lifecycle.internal.stub.LoggerStub; 029import org.apache.maven.model.DependencyManagement; 030import org.apache.maven.model.Model; 031import org.apache.maven.model.Parent; 032import org.apache.maven.model.Profile; 033 034public class MavenProjectTest 035 extends AbstractMavenProjectTestCase 036{ 037 038 public void testShouldInterpretChildPathAdjustmentBasedOnModulePaths() 039 throws IOException 040 { 041 Model parentModel = new Model(); 042 parentModel.addModule( "../child" ); 043 044 MavenProject parentProject = new MavenProject( parentModel ); 045 046 Model childModel = new Model(); 047 childModel.setArtifactId( "artifact" ); 048 049 MavenProject childProject = new MavenProject( childModel ); 050 051 File childFile = 052 new File( System.getProperty( "java.io.tmpdir" ), "maven-project-tests" + System.currentTimeMillis() 053 + "/child/pom.xml" ); 054 055 childProject.setFile( childFile ); 056 057 String adjustment = parentProject.getModulePathAdjustment( childProject ); 058 059 assertNotNull( adjustment ); 060 061 assertEquals( "..", adjustment ); 062 } 063 064 public void testIdentityProtoInheritance() 065 { 066 Parent parent = new Parent(); 067 068 parent.setGroupId( "test-group" ); 069 parent.setVersion( "1000" ); 070 parent.setArtifactId( "test-artifact" ); 071 072 Model model = new Model(); 073 074 model.setParent( parent ); 075 model.setArtifactId( "real-artifact" ); 076 077 MavenProject project = new MavenProject( model ); 078 079 assertEquals( "groupId proto-inheritance failed.", "test-group", project.getGroupId() ); 080 assertEquals( "artifactId is masked.", "real-artifact", project.getArtifactId() ); 081 assertEquals( "version proto-inheritance failed.", "1000", project.getVersion() ); 082 083 // draw the NPE. 084 project.getId(); 085 } 086 087 public void testEmptyConstructor() 088 { 089 MavenProject project = new MavenProject(); 090 091 assertEquals( MavenProject.EMPTY_PROJECT_GROUP_ID + ":" + MavenProject.EMPTY_PROJECT_ARTIFACT_ID + ":jar:" 092 + MavenProject.EMPTY_PROJECT_VERSION, project.getId() ); 093 } 094 095 public void testClone() 096 throws Exception 097 { 098 File f = getFileForClasspathResource( "canonical-pom.xml" ); 099 MavenProject projectToClone = getProject( f ); 100 101 MavenProject clonedProject = projectToClone.clone(); 102 assertEquals( "maven-core", clonedProject.getArtifactId() ); 103 Map<?, ?> clonedMap = clonedProject.getManagedVersionMap(); 104 assertNotNull( "ManagedVersionMap not copied", clonedMap ); 105 assertTrue( "ManagedVersionMap is not empty", clonedMap.isEmpty() ); 106 } 107 108 public void testCloneWithDependencyManagement() 109 throws Exception 110 { 111 File f = getFileForClasspathResource( "dependencyManagement-pom.xml" ); 112 MavenProject projectToClone = getProjectWithDependencies( f ); 113 DependencyManagement dep = projectToClone.getDependencyManagement(); 114 assertNotNull( "No dependencyManagement", dep ); 115 List<?> list = dep.getDependencies(); 116 assertNotNull( "No dependencies", list ); 117 assertTrue( "Empty dependency list", !list.isEmpty() ); 118 119 Map<?, ?> map = projectToClone.getManagedVersionMap(); 120 assertNotNull( "No ManagedVersionMap", map ); 121 assertTrue( "ManagedVersionMap is empty", !map.isEmpty() ); 122 123 MavenProject clonedProject = projectToClone.clone(); 124 assertEquals( "maven-core", clonedProject.getArtifactId() ); 125 Map<?, ?> clonedMap = clonedProject.getManagedVersionMap(); 126 assertNotNull( "ManagedVersionMap not copied", clonedMap ); 127 assertTrue( "ManagedVersionMap is empty", !clonedMap.isEmpty() ); 128 assertTrue( "ManagedVersionMap does not contain test key", 129 clonedMap.containsKey( "maven-test:maven-test-b:jar" ) ); 130 } 131 132 public void testGetModulePathAdjustment() 133 throws IOException 134 { 135 Model moduleModel = new Model(); 136 137 MavenProject module = new MavenProject( moduleModel ); 138 module.setFile( new File( "module-dir/pom.xml" ) ); 139 140 Model parentModel = new Model(); 141 parentModel.addModule( "../module-dir" ); 142 143 MavenProject parent = new MavenProject( parentModel ); 144 parent.setFile( new File( "parent-dir/pom.xml" ) ); 145 146 String pathAdjustment = parent.getModulePathAdjustment( module ); 147 148 assertEquals( "..", pathAdjustment ); 149 } 150 151 public void testCloneWithDistributionManagement() 152 throws Exception 153 { 154 155 File f = getFileForClasspathResource( "distributionManagement-pom.xml" ); 156 MavenProject projectToClone = getProject( f ); 157 158 MavenProject clonedProject = projectToClone.clone(); 159 assertNotNull( "clonedProject - distributionManagement", clonedProject.getDistributionManagementArtifactRepository() ); 160 } 161 162 public void testCloneWithActiveProfile() 163 throws Exception 164 { 165 166 File f = getFileForClasspathResource( "withActiveByDefaultProfile-pom.xml" ); 167 MavenProject projectToClone = getProject( f ); 168 List<Profile> activeProfilesOrig = projectToClone.getActiveProfiles(); 169 170 assertEquals( "Expecting 1 active profile", 1, activeProfilesOrig.size() ); 171 172 MavenProject clonedProject = projectToClone.clone(); 173 174 List<Profile> activeProfilesClone = clonedProject.getActiveProfiles(); 175 176 assertEquals( "Expecting 1 active profile", 1, activeProfilesClone.size() ); 177 178 assertNotSame( "The list of active profiles should have been cloned too but is same", activeProfilesOrig, 179 activeProfilesClone ); 180 } 181 182 public void testUndefinedOutputDirectory() 183 throws Exception 184 { 185 MavenProject p = new MavenProject(); 186 assertNoNulls( p.getCompileClasspathElements() ); 187 assertNoNulls( p.getSystemClasspathElements() ); 188 assertNoNulls( p.getRuntimeClasspathElements() ); 189 assertNoNulls( p.getTestClasspathElements() ); 190 } 191 192 private void assertNoNulls( List<String> elements ) 193 { 194 assertFalse( elements.contains( null ) ); 195 } 196 197}