001 package 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
022 import java.io.File;
023 import java.util.ArrayList;
024 import java.util.Iterator;
025 import java.util.List;
026
027 import org.apache.maven.artifact.Artifact;
028 import org.apache.maven.artifact.repository.ArtifactRepository;
029 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
030 import org.codehaus.plexus.util.FileUtils;
031
032 public class DefaultMavenProjectBuilderTest
033 extends AbstractMavenProjectTestCase
034 {
035
036 private List<File> filesToDelete = new ArrayList<File>();
037
038 private File localRepoDir;
039
040 @Override
041 public void setUp()
042 throws Exception
043 {
044 super.setUp();
045
046 projectBuilder = lookup( ProjectBuilder.class );
047
048 localRepoDir = new File( System.getProperty( "java.io.tmpdir" ), "local-repo." + System.currentTimeMillis() );
049 localRepoDir.mkdirs();
050
051 filesToDelete.add( localRepoDir );
052 }
053
054 @Override
055 public void tearDown()
056 throws Exception
057 {
058 super.tearDown();
059
060 if ( !filesToDelete.isEmpty() )
061 {
062 for ( File file : filesToDelete )
063 {
064 if ( file.exists() )
065 {
066 if ( file.isDirectory() )
067 {
068 FileUtils.deleteDirectory( file );
069 }
070 else
071 {
072 file.delete();
073 }
074 }
075 }
076 }
077 }
078
079 protected MavenProject getProject( Artifact pom, boolean allowStub )
080 throws Exception
081 {
082 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
083 configuration.setLocalRepository( getLocalRepository() );
084 initRepoSession( configuration );
085
086 return projectBuilder.build( pom, allowStub, configuration ).getProject();
087 }
088
089 /**
090 * Check that we can build ok from the middle pom of a (parent,child,grandchild) heirarchy
091 * @throws Exception
092 */
093 public void testBuildFromMiddlePom() throws Exception
094 {
095 File f1 = getTestFile( "src/test/resources/projects/grandchild-check/child/pom.xml");
096 File f2 = getTestFile( "src/test/resources/projects/grandchild-check/child/grandchild/pom.xml");
097
098 getProject( f1 );
099
100 // it's the building of the grandchild project, having already cached the child project
101 // (but not the parent project), which causes the problem.
102 getProject( f2 );
103 }
104
105 public void testDuplicatePluginDefinitionsMerged()
106 throws Exception
107 {
108 File f1 = getTestFile( "src/test/resources/projects/duplicate-plugins-merged-pom.xml" );
109
110 MavenProject project = getProject( f1 );
111 assertEquals( 2, project.getBuildPlugins().get( 0 ).getDependencies().size() );
112 assertEquals( 2, project.getBuildPlugins().get( 0 ).getExecutions().size() );
113 assertEquals( "first", project.getBuildPlugins().get( 0 ).getExecutions().get( 0 ).getId() );
114 }
115
116 public void testBuildStubModelForMissingRemotePom()
117 throws Exception
118 {
119 Artifact pom = repositorySystem.createProjectArtifact( "org.apache.maven.its", "missing", "0.1" );
120 MavenProject project = getProject( pom, true );
121
122 assertNotNull( project.getArtifactId() );
123
124 assertNotNull( project.getRemoteArtifactRepositories() );
125 assertFalse( project.getRemoteArtifactRepositories().isEmpty() );
126
127 assertNotNull( project.getPluginArtifactRepositories() );
128 assertFalse( project.getPluginArtifactRepositories().isEmpty() );
129
130 assertNull( project.getParent() );
131 assertNull( project.getParentArtifact() );
132
133 assertFalse( project.isExecutionRoot() );
134 }
135
136 @Override
137 protected ArtifactRepository getLocalRepository()
138 throws Exception
139 {
140 ArtifactRepositoryLayout repoLayout = lookup( ArtifactRepositoryLayout.class, "default" );
141 ArtifactRepository r =
142 repositorySystem.createArtifactRepository( "local", "file://" + localRepoDir.getAbsolutePath(), repoLayout,
143 null, null );
144 return r;
145 }
146
147 public void xtestLoop()
148 throws Exception
149 {
150 while ( true )
151 {
152 File f1 = getTestFile( "src/test/resources/projects/duplicate-plugins-merged-pom.xml" );
153 getProject( f1 );
154 }
155 }
156
157 public void testPartialResultUponBadDependencyDeclaration()
158 throws Exception
159 {
160 File pomFile = getTestFile( "src/test/resources/projects/bad-dependency.xml" );
161
162 try
163 {
164 ProjectBuildingRequest request = newBuildingRequest();
165 request.setProcessPlugins( false );
166 request.setResolveDependencies( true );
167 projectBuilder.build( pomFile, request );
168 fail( "Project building did not fail despite invalid POM" );
169 }
170 catch ( ProjectBuildingException e )
171 {
172 List<ProjectBuildingResult> results = e.getResults();
173 assertNotNull( results );
174 assertEquals( 1, results.size() );
175 ProjectBuildingResult result = results.get( 0 );
176 assertNotNull( result );
177 assertNotNull( result.getProject() );
178 assertEquals( 1, result.getProblems().size() );
179 assertEquals( 1, result.getProject().getArtifacts().size() );
180 assertNotNull( result.getDependencyResolutionResult() );
181 }
182 }
183
184 }