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  
27  import org.apache.maven.model.DependencyManagement;
28  import org.apache.maven.model.Model;
29  import org.apache.maven.model.Parent;
30  import org.apache.maven.model.Profile;
31  
32  public class MavenProjectTest
33      extends AbstractMavenProjectTestCase
34  {
35  
36      public void testShouldInterpretChildPathAdjustmentBasedOnModulePaths()
37          throws IOException
38      {
39          Model parentModel = new Model();
40          parentModel.addModule( "../child" );
41  
42          MavenProject parentProject = new MavenProject( parentModel );
43  
44          Model childModel = new Model();
45          childModel.setArtifactId( "artifact" );
46  
47          MavenProject childProject = new MavenProject( childModel );
48  
49          File childFile =
50              new File( System.getProperty( "java.io.tmpdir" ), "maven-project-tests" + System.currentTimeMillis()
51                  + "/child/pom.xml" );
52  
53          childProject.setFile( childFile );
54  
55          String adjustment = parentProject.getModulePathAdjustment( childProject );
56  
57          assertNotNull( adjustment );
58  
59          assertEquals( "..", adjustment );
60      }
61  
62      public void testIdentityProtoInheritance()
63      {
64          Parent parent = new Parent();
65  
66          parent.setGroupId( "test-group" );
67          parent.setVersion( "1000" );
68          parent.setArtifactId( "test-artifact" );
69  
70          Model model = new Model();
71  
72          model.setParent( parent );
73          model.setArtifactId( "real-artifact" );
74  
75          MavenProject project = new MavenProject( model );
76  
77          assertEquals( "groupId proto-inheritance failed.", "test-group", project.getGroupId() );
78          assertEquals( "artifactId is masked.", "real-artifact", project.getArtifactId() );
79          assertEquals( "version proto-inheritance failed.", "1000", project.getVersion() );
80  
81          // draw the NPE.
82          project.getId();
83      }
84  
85      public void testEmptyConstructor()
86      {
87          MavenProject project = new MavenProject();
88  
89          assertEquals( MavenProject.EMPTY_PROJECT_GROUP_ID + ":" + MavenProject.EMPTY_PROJECT_ARTIFACT_ID + ":jar:"
90                          + MavenProject.EMPTY_PROJECT_VERSION, project.getId() );
91      }
92  
93      public void testClone()
94          throws Exception
95      {
96          File f = getFileForClasspathResource( "canonical-pom.xml" );
97          MavenProject projectToClone = getProject( f );
98  
99          MavenProject clonedProject = projectToClone.clone();
100         assertEquals( "maven-core", clonedProject.getArtifactId() );
101         Map<?, ?> clonedMap = clonedProject.getManagedVersionMap();
102         assertNotNull( "ManagedVersionMap not copied", clonedMap );
103         assertTrue( "ManagedVersionMap is not empty", clonedMap.isEmpty() );
104     }
105 
106     public void testCloneWithDependencyManagement()
107         throws Exception
108     {
109         File f = getFileForClasspathResource( "dependencyManagement-pom.xml" );
110         MavenProject projectToClone = getProjectWithDependencies( f );
111         DependencyManagement dep = projectToClone.getDependencyManagement();
112         assertNotNull( "No dependencyManagement", dep );
113         List<?> list = dep.getDependencies();
114         assertNotNull( "No dependencies", list );
115         assertTrue( "Empty dependency list", !list.isEmpty() );
116 
117         Map<?, ?> map = projectToClone.getManagedVersionMap();
118         assertNotNull( "No ManagedVersionMap", map );
119         assertTrue( "ManagedVersionMap is empty", !map.isEmpty() );
120 
121         MavenProject clonedProject = projectToClone.clone();
122         assertEquals( "maven-core", clonedProject.getArtifactId() );
123         Map<?, ?> clonedMap = clonedProject.getManagedVersionMap();
124         assertNotNull( "ManagedVersionMap not copied", clonedMap );
125         assertTrue( "ManagedVersionMap is empty", !clonedMap.isEmpty() );
126         assertTrue( "ManagedVersionMap does not contain test key",
127                     clonedMap.containsKey( "maven-test:maven-test-b:jar" ) );
128     }
129 
130     public void testGetModulePathAdjustment()
131         throws IOException
132     {
133         Model moduleModel = new Model();
134 
135         MavenProject module = new MavenProject( moduleModel );
136         module.setFile( new File( "module-dir/pom.xml" ) );
137 
138         Model parentModel = new Model();
139         parentModel.addModule( "../module-dir" );
140 
141         MavenProject parent = new MavenProject( parentModel );
142         parent.setFile( new File( "parent-dir/pom.xml" ) );
143 
144         String pathAdjustment = parent.getModulePathAdjustment( module );
145 
146         assertEquals( "..", pathAdjustment );
147     }
148 
149     public void testCloneWithDistributionManagement()
150         throws Exception
151     {
152 
153         File f = getFileForClasspathResource( "distributionManagement-pom.xml" );
154         MavenProject projectToClone = getProject( f );
155 
156         MavenProject clonedProject = projectToClone.clone();
157         assertNotNull( "clonedProject - distributionManagement", clonedProject.getDistributionManagementArtifactRepository() );
158     }
159 
160     public void testCloneWithActiveProfile()
161         throws Exception
162     {
163 
164         File f = getFileForClasspathResource( "withActiveByDefaultProfile-pom.xml" );
165         MavenProject projectToClone = getProject( f );
166         List<Profile> activeProfilesOrig = projectToClone.getActiveProfiles();
167 
168         assertEquals( "Expecting 1 active profile", 1, activeProfilesOrig.size() );
169 
170         MavenProject clonedProject = projectToClone.clone();
171 
172         List<Profile> activeProfilesClone = clonedProject.getActiveProfiles();
173 
174         assertEquals( "Expecting 1 active profile", 1, activeProfilesClone.size() );
175 
176         assertNotSame( "The list of active profiles should have been cloned too but is same", activeProfilesOrig,
177                        activeProfilesClone );
178     }
179 
180     public void testUndefinedOutputDirectory()
181         throws Exception
182     {
183         MavenProject p = new MavenProject();
184         assertNoNulls( p.getCompileClasspathElements() );
185         assertNoNulls( p.getSystemClasspathElements() );
186         assertNoNulls( p.getRuntimeClasspathElements() );
187         assertNoNulls( p.getTestClasspathElements() );
188     }
189 
190     private void assertNoNulls( List<String> elements )
191     {
192         assertFalse( elements.contains( null ) );
193     }
194 
195 }