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