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.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  public class DefaultMavenProjectBuilderTest
33      extends AbstractMavenProjectTestCase
34  {
35  
36      private List<File> filesToDelete = new ArrayList<File>();
37  
38      private File localRepoDir;
39  
40      @Override
41      public void setUp()
42          throws Exception
43      {
44          super.setUp();
45  
46          projectBuilder = lookup( ProjectBuilder.class );
47  
48          localRepoDir = new File( System.getProperty( "java.io.tmpdir" ), "local-repo." + System.currentTimeMillis() );
49          localRepoDir.mkdirs();
50  
51          filesToDelete.add( localRepoDir );
52      }
53  
54      @Override
55      public void tearDown()
56          throws Exception
57      {
58          super.tearDown();
59  
60          if ( !filesToDelete.isEmpty() )
61          {
62              for ( File file : filesToDelete )
63              {
64                  if ( file.exists() )
65                  {
66                      if ( file.isDirectory() )
67                      {
68                          FileUtils.deleteDirectory( file );
69                      }
70                      else
71                      {
72                          file.delete();
73                      }
74                  }
75              }
76          }
77      }
78  
79      protected MavenProject getProject( Artifact pom, boolean allowStub )
80          throws Exception
81      {
82          ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
83          configuration.setLocalRepository( getLocalRepository() );
84          initRepoSession( configuration );
85  
86          return projectBuilder.build( pom, allowStub, configuration ).getProject();
87      }
88  
89      /**
90       * Check that we can build ok from the middle pom of a (parent,child,grandchild) heirarchy
91       * @throws Exception
92       */
93      public void testBuildFromMiddlePom() throws Exception
94      {
95          File f1 = getTestFile( "src/test/resources/projects/grandchild-check/child/pom.xml");
96          File f2 = getTestFile( "src/test/resources/projects/grandchild-check/child/grandchild/pom.xml");
97  
98          getProject( f1 );
99  
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     public void testImportScopePomResolvesFromPropertyBasedRepository()
185             throws Exception
186     {
187         File pomFile = getTestFile( "src/test/resources/projects/import-scope-pom-resolves-from-property-based-repository.xml" );
188         ProjectBuildingRequest request = newBuildingRequest();
189         request.setProcessPlugins( false );
190         request.setResolveDependencies( true );
191         projectBuilder.build( pomFile, request );
192     }
193 }