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 ( Iterator<File> it = filesToDelete.iterator(); it.hasNext(); )
63              {
64                  File file = it.next();
65  
66                  if ( file.exists() )
67                  {
68                      if ( file.isDirectory() )
69                      {
70                          FileUtils.deleteDirectory( file );
71                      }
72                      else
73                      {
74                          file.delete();
75                      }
76                  }
77              }
78          }
79      }
80  
81      protected MavenProject getProject( Artifact pom, boolean allowStub )
82          throws Exception
83      {
84          ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
85          configuration.setLocalRepository( getLocalRepository() );
86          initRepoSession( configuration );
87  
88          return projectBuilder.build( pom, allowStub, configuration ).getProject();
89      }
90  
91      /**
92       * Check that we can build ok from the middle pom of a (parent,child,grandchild) heirarchy
93       * @throws Exception
94       */
95      public void testBuildFromMiddlePom() throws Exception
96      {
97          File f1 = getTestFile( "src/test/resources/projects/grandchild-check/child/pom.xml");
98          File f2 = getTestFile( "src/test/resources/projects/grandchild-check/child/grandchild/pom.xml");
99  
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 = repositorySystem.createArtifactRepository( "local", "file://" + localRepoDir.getAbsolutePath(), repoLayout, null, null );
144         return r;
145     }
146     
147     public void xtestLoop() throws Exception
148     {
149         while( true )
150         {
151         File f1 = getTestFile( "src/test/resources/projects/duplicate-plugins-merged-pom.xml" );
152         getProject( f1 );
153         }
154     }
155     
156 }