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 org.apache.maven.artifact.repository.ArtifactRepository;
23  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
24  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
25  import org.apache.maven.model.Build;
26  import org.apache.maven.model.Plugin;
27  import org.apache.maven.model.Profile;
28  import org.apache.maven.model.Repository;
29  import org.apache.maven.model.Resource;
30  import org.apache.maven.profiles.DefaultProfileManager;
31  import org.apache.maven.profiles.ProfileManager;
32  import org.codehaus.plexus.util.FileUtils;
33  
34  import java.io.File;
35  import java.util.ArrayList;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.Properties;
39  
40  public class DefaultMavenProjectBuilderTest
41      extends AbstractMavenProjectTestCase
42  {
43  
44      private List filesToDelete = new ArrayList();
45  
46      private File localRepoDir;
47  
48      public void setUp()
49          throws Exception
50      {
51          super.setUp();
52  
53          localRepoDir = new File( System.getProperty( "java.io.tmpdir" ), "local-repo." + System.currentTimeMillis() );
54          localRepoDir.mkdirs();
55  
56          filesToDelete.add( localRepoDir );
57      }
58  
59      public void tearDown()
60          throws Exception
61      {
62          super.tearDown();
63  
64          if ( !filesToDelete.isEmpty() )
65          {
66              for ( Iterator it = filesToDelete.iterator(); it.hasNext(); )
67              {
68                  File file = (File) it.next();
69  
70                  if ( file.exists() )
71                  {
72                      if ( file.isDirectory() )
73                      {
74                          FileUtils.deleteDirectory( file );
75                      }
76                      else
77                      {
78                          file.delete();
79                      }
80                  }
81              }
82          }
83      }
84  
85      public void testShouldInjectOneProfileToStandaloneSuperPom()
86          throws Exception
87      {
88          ProfileManager pm = new DefaultProfileManager( getContainer(), new Properties() );
89  
90          String profileId = "test-profile";
91          String key = "test";
92          String value = "value";
93  
94          Profile profile = new Profile();
95          profile.setId( profileId );
96          profile.addProperty( key, value );
97  
98          pm.addProfile( profile );
99          pm.explicitlyActivate( profileId );
100 
101         MavenProject project = projectBuilder.buildStandaloneSuperProject( getLocalRepository(), pm );
102 
103         assertEquals( value, project.getProperties().getProperty( key ) );
104     }
105 
106     public void testShouldInjectProfileWithRepositoryToStandaloneSuperPom()
107         throws Exception
108     {
109         ProfileManager pm = new DefaultProfileManager( getContainer(), new Properties() );
110 
111         String profileId = "test-profile";
112         String repoId = "test-repo";
113 
114         Profile profile = new Profile();
115         profile.setId( profileId );
116 
117         Repository repo = new Repository();
118         repo.setId( repoId );
119         repo.setUrl( "http://www.google.com" );
120 
121         profile.addRepository( repo );
122 
123         pm.addProfile( profile );
124         pm.explicitlyActivate( profileId );
125 
126         MavenProject project = projectBuilder.buildStandaloneSuperProject( getLocalRepository(), pm );
127 
128         List repositories = project.getRepositories();
129 
130         assertNotNull( repositories );
131 
132         Repository result = null;
133 
134         for ( Iterator it = repositories.iterator(); it.hasNext(); )
135         {
136             Repository candidate = (Repository) it.next();
137 
138             if ( repoId.equals( candidate.getId() ) )
139             {
140                 result = candidate;
141                 break;
142             }
143         }
144 
145         assertNotNull( "Profile-injected repository not found in super-POM.", result );
146 
147         assertEquals( "Profile-injected repository was not first in repo list within super-POM", repoId,
148                       ( (Repository) repositories.get( 0 ) ).getId() );
149     }
150 
151     /**
152      * Check that we can build ok from the middle pom of a (parent,child,grandchild) heirarchy
153      * @throws Exception
154      */
155     public void testBuildFromMiddlePom() throws Exception
156     {
157         File f1 = getTestFile( "src/test/resources/projects/grandchild-check/child/pom.xml");
158         File f2 = getTestFile( "src/test/resources/projects/grandchild-check/child/grandchild/pom.xml");
159 
160         getProject( f1 );
161 
162         // it's the building of the grandchild project, having already cached the child project
163         // (but not the parent project), which causes the problem.
164         getProject( f2 );
165     }
166 
167      public void testDuplicatePluginDefinitionsMerged()
168          throws Exception
169      {
170          File f1 = getTestFile( "src/test/resources/projects/duplicate-plugins-merged-pom.xml" );
171 
172          MavenProject project = getProject( f1 );
173 
174          assertEquals( 2, ( (Plugin) project.getBuildPlugins().get( 0 ) ).getDependencies().size() );
175      }
176 
177      public void testBuildDirectoryExpressionInterpolatedWithTranslatedValue()
178         throws Exception
179      {
180          File pom = getTestFile( "src/test/resources/projects/build-path-expression-pom.xml" );
181 
182          MavenProject project = getProject( pom );
183 
184          Build build = project.getBuild();
185          assertNotNull( "Project should have a build section containing the test resource.", build );
186 
187          String sourceDirectory = build.getSourceDirectory();
188          assertNotNull( "Project build should contain a valid source directory.", sourceDirectory );
189 
190          List resources = build.getResources();
191          assertNotNull( "Project should contain a build resource.", resources );
192          assertEquals( "Project should contain exactly one build resource.", 1, resources.size() );
193 
194          Resource res = (Resource) resources.get( 0 );
195          assertEquals( "Project resource should be the same directory as the source directory.", sourceDirectory, res.getDirectory() );
196 
197          System.out.println( "Interpolated, translated resource directory is: " + res.getDirectory() );
198      }
199 
200     protected ArtifactRepository getLocalRepository()
201         throws Exception
202     {
203         ArtifactRepositoryLayout repoLayout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE,
204                                                                                  "legacy" );
205 
206         ArtifactRepository r = new DefaultArtifactRepository( "local", "file://" + localRepoDir.getAbsolutePath(),
207                                                               repoLayout );
208 
209         return r;
210     }
211 }