View Javadoc
1   package org.apache.maven.project;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.concurrent.atomic.AtomicInteger;
27  
28  import org.apache.maven.lifecycle.internal.stub.LoggerStub;
29  import org.apache.maven.model.DependencyManagement;
30  import org.apache.maven.model.Model;
31  import org.apache.maven.model.Parent;
32  import org.apache.maven.model.Profile;
33  
34  public class MavenProjectTest
35      extends AbstractMavenProjectTestCase
36  {
37  
38      public void testShouldInterpretChildPathAdjustmentBasedOnModulePaths()
39          throws IOException
40      {
41          Model parentModel = new Model();
42          parentModel.addModule( "../child" );
43  
44          MavenProject parentProject = new MavenProject( parentModel );
45  
46          Model childModel = new Model();
47          childModel.setArtifactId( "artifact" );
48  
49          MavenProject childProject = new MavenProject( childModel );
50  
51          File childFile =
52              new File( System.getProperty( "java.io.tmpdir" ), "maven-project-tests" + System.currentTimeMillis()
53                  + "/child/pom.xml" );
54  
55          childProject.setFile( childFile );
56  
57          String adjustment = parentProject.getModulePathAdjustment( childProject );
58  
59          assertNotNull( adjustment );
60  
61          assertEquals( "..", adjustment );
62      }
63  
64      public void testIdentityProtoInheritance()
65      {
66          Parent parent = new Parent();
67  
68          parent.setGroupId( "test-group" );
69          parent.setVersion( "1000" );
70          parent.setArtifactId( "test-artifact" );
71  
72          Model model = new Model();
73  
74          model.setParent( parent );
75          model.setArtifactId( "real-artifact" );
76  
77          MavenProject project = new MavenProject( model );
78  
79          assertEquals( "groupId proto-inheritance failed.", "test-group", project.getGroupId() );
80          assertEquals( "artifactId is masked.", "real-artifact", project.getArtifactId() );
81          assertEquals( "version proto-inheritance failed.", "1000", project.getVersion() );
82  
83          // draw the NPE.
84          project.getId();
85      }
86  
87      public void testEmptyConstructor()
88      {
89          MavenProject project = new MavenProject();
90  
91          assertEquals( MavenProject.EMPTY_PROJECT_GROUP_ID + ":" + MavenProject.EMPTY_PROJECT_ARTIFACT_ID + ":jar:"
92                          + MavenProject.EMPTY_PROJECT_VERSION, project.getId() );
93      }
94  
95      public void testClone()
96          throws Exception
97      {
98          File f = getFileForClasspathResource( "canonical-pom.xml" );
99          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 }