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;
026
027import org.apache.maven.model.DependencyManagement;
028import org.apache.maven.model.Model;
029import org.apache.maven.model.Parent;
030import org.apache.maven.model.Profile;
031
032public class MavenProjectTest
033    extends AbstractMavenProjectTestCase
034{
035
036    public void testShouldInterpretChildPathAdjustmentBasedOnModulePaths()
037        throws IOException
038    {
039        Model parentModel = new Model();
040        parentModel.addModule( "../child" );
041
042        MavenProject parentProject = new MavenProject( parentModel );
043
044        Model childModel = new Model();
045        childModel.setArtifactId( "artifact" );
046
047        MavenProject childProject = new MavenProject( childModel );
048        
049        File childFile =
050            new File( System.getProperty( "java.io.tmpdir" ), "maven-project-tests" + System.currentTimeMillis()
051                + "/child/pom.xml" );
052
053        childProject.setFile( childFile );
054
055        String adjustment = parentProject.getModulePathAdjustment( childProject );
056
057        assertNotNull( adjustment );
058        
059        assertEquals( "..", adjustment );
060    }
061
062    public void testIdentityProtoInheritance()
063    {
064        Parent parent = new Parent();
065
066        parent.setGroupId( "test-group" );
067        parent.setVersion( "1000" );
068        parent.setArtifactId( "test-artifact" );
069
070        Model model = new Model();
071
072        model.setParent( parent );
073        model.setArtifactId( "real-artifact" );
074
075        MavenProject project = new MavenProject( model );
076
077        assertEquals( "groupId proto-inheritance failed.", "test-group", project.getGroupId() );
078        assertEquals( "artifactId is masked.", "real-artifact", project.getArtifactId() );
079        assertEquals( "version proto-inheritance failed.", "1000", project.getVersion() );
080
081        // draw the NPE.
082        project.getId();
083    }
084
085    public void testEmptyConstructor()
086    {
087        MavenProject project = new MavenProject();
088
089        assertEquals( MavenProject.EMPTY_PROJECT_GROUP_ID + ":" + MavenProject.EMPTY_PROJECT_ARTIFACT_ID + ":jar:"
090                        + MavenProject.EMPTY_PROJECT_VERSION, project.getId() );
091    }
092
093    public void testClone()
094        throws Exception
095    {
096        File f = getFileForClasspathResource( "canonical-pom.xml" );
097        MavenProject projectToClone = getProject( f );
098
099        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}