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 ( Iterator<File> it = filesToDelete.iterator(); it.hasNext(); )
063 {
064 File file = it.next();
065
066 if ( file.exists() )
067 {
068 if ( file.isDirectory() )
069 {
070 FileUtils.deleteDirectory( file );
071 }
072 else
073 {
074 file.delete();
075 }
076 }
077 }
078 }
079 }
080
081 protected MavenProject getProject( Artifact pom, boolean allowStub )
082 throws Exception
083 {
084 ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
085 configuration.setLocalRepository( getLocalRepository() );
086 initRepoSession( configuration );
087
088 return projectBuilder.build( pom, allowStub, configuration ).getProject();
089 }
090
091 /**
092 * Check that we can build ok from the middle pom of a (parent,child,grandchild) heirarchy
093 * @throws Exception
094 */
095 public void testBuildFromMiddlePom() throws Exception
096 {
097 File f1 = getTestFile( "src/test/resources/projects/grandchild-check/child/pom.xml");
098 File f2 = getTestFile( "src/test/resources/projects/grandchild-check/child/grandchild/pom.xml");
099
100 getProject( f1 );
101
102 // it's the building of the grandchild project, having already cached the child project
103 // (but not the parent project), which causes the problem.
104 getProject( f2 );
105 }
106
107 public void testDuplicatePluginDefinitionsMerged()
108 throws Exception
109 {
110 File f1 = getTestFile( "src/test/resources/projects/duplicate-plugins-merged-pom.xml" );
111
112 MavenProject project = getProject( f1 );
113 assertEquals( 2, project.getBuildPlugins().get( 0 ).getDependencies().size() );
114 assertEquals( 2, project.getBuildPlugins().get( 0 ).getExecutions().size() );
115 assertEquals( "first", project.getBuildPlugins().get( 0 ).getExecutions().get( 0 ).getId() );
116 }
117
118 public void testBuildStubModelForMissingRemotePom()
119 throws Exception
120 {
121 Artifact pom = repositorySystem.createProjectArtifact( "org.apache.maven.its", "missing", "0.1" );
122 MavenProject project = getProject( pom, true );
123
124 assertNotNull( project.getArtifactId() );
125
126 assertNotNull( project.getRemoteArtifactRepositories() );
127 assertFalse( project.getRemoteArtifactRepositories().isEmpty() );
128
129 assertNotNull( project.getPluginArtifactRepositories() );
130 assertFalse( project.getPluginArtifactRepositories().isEmpty() );
131
132 assertNull( project.getParent() );
133 assertNull( project.getParentArtifact() );
134
135 assertFalse( project.isExecutionRoot() );
136 }
137
138 @Override
139 protected ArtifactRepository getLocalRepository()
140 throws Exception
141 {
142 ArtifactRepositoryLayout repoLayout = lookup( ArtifactRepositoryLayout.class, "default" );
143 ArtifactRepository r =
144 repositorySystem.createArtifactRepository( "local", "file://" + localRepoDir.getAbsolutePath(), repoLayout,
145 null, null );
146 return r;
147 }
148
149 public void xtestLoop()
150 throws Exception
151 {
152 while ( true )
153 {
154 File f1 = getTestFile( "src/test/resources/projects/duplicate-plugins-merged-pom.xml" );
155 getProject( f1 );
156 }
157 }
158
159 public void testPartialResultUponBadDependencyDeclaration()
160 throws Exception
161 {
162 File pomFile = getTestFile( "src/test/resources/projects/bad-dependency.xml" );
163
164 try
165 {
166 ProjectBuildingRequest request = newBuildingRequest();
167 request.setProcessPlugins( false );
168 request.setResolveDependencies( true );
169 projectBuilder.build( pomFile, request );
170 fail( "Project building did not fail despite invalid POM" );
171 }
172 catch ( ProjectBuildingException e )
173 {
174 List<ProjectBuildingResult> results = e.getResults();
175 assertNotNull( results );
176 assertEquals( 1, results.size() );
177 ProjectBuildingResult result = results.get( 0 );
178 assertNotNull( result );
179 assertNotNull( result.getProject() );
180 assertEquals( 1, result.getProblems().size() );
181 assertEquals( 1, result.getProject().getArtifacts().size() );
182 assertNotNull( result.getDependencyResolutionResult() );
183 }
184 }
185
186 }