1   package org.apache.maven.settings;
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.io.IOException;
24  import java.io.Reader;
25  
26  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
27  import org.apache.maven.model.Profile;
28  import org.apache.maven.project.DefaultProjectBuilder;
29  import org.apache.maven.project.DefaultProjectBuildingRequest;
30  import org.apache.maven.project.ProjectBuilder;
31  import org.apache.maven.project.ProjectBuildingRequest;
32  import org.apache.maven.project.harness.PomTestWrapper;
33  import org.apache.maven.repository.RepositorySystem;
34  import org.apache.maven.repository.internal.MavenRepositorySystemSession;
35  import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
36  import org.codehaus.plexus.PlexusTestCase;
37  import org.codehaus.plexus.util.IOUtil;
38  import org.codehaus.plexus.util.ReaderFactory;
39  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
40  import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
41  
42  public class PomConstructionWithSettingsTest
43      extends PlexusTestCase
44  {
45      private static final String BASE_DIR = "src/test";
46  
47      private static final String BASE_POM_DIR = BASE_DIR + "/resources-settings";
48  
49      private DefaultProjectBuilder projectBuilder;
50  
51      private RepositorySystem repositorySystem;
52  
53      private File testDirectory;
54  
55      protected void setUp()
56          throws Exception
57      {
58          testDirectory = new File( getBasedir(), BASE_POM_DIR );
59          projectBuilder = (DefaultProjectBuilder) lookup( ProjectBuilder.class );
60          repositorySystem = lookup( RepositorySystem.class );
61      }
62  
63      @Override
64      protected void tearDown()
65          throws Exception
66      {
67          projectBuilder = null;
68  
69          super.tearDown();
70      }
71  
72      public void testSettingsNoPom() throws Exception
73      {
74          PomTestWrapper pom = buildPom( "settings-no-pom" );
75          assertEquals( "local-profile-prop-value", pom.getValue( "properties/local-profile-prop" ) );
76      }
77  
78      /**MNG-4107 */
79      public void testPomAndSettingsInterpolation() throws Exception
80      {
81          PomTestWrapper pom = buildPom( "test-pom-and-settings-interpolation" );
82          assertEquals( "applied", pom.getValue( "properties/settingsProfile" ) );
83          assertEquals( "applied", pom.getValue( "properties/pomProfile" ) );
84          assertEquals( "settings", pom.getValue( "properties/pomVsSettings" ) );
85          assertEquals( "settings", pom.getValue( "properties/pomVsSettingsInterpolated" ) );
86      }
87  
88      /**MNG-4107 */
89      public void testRepositories() throws Exception
90      {
91          PomTestWrapper pom = buildPom( "repositories" );
92          assertEquals( "maven-core-it-0", pom.getValue( "repositories[1]/id" ) );
93      }
94  
95      private PomTestWrapper buildPom( String pomPath )
96          throws Exception
97      {
98          File pomFile = new File( testDirectory + File.separator + pomPath, "pom.xml" );
99          File settingsFile = new File( testDirectory + File.separator + pomPath, "settings.xml" );
100         Settings settings = readSettingsFile( settingsFile );
101 
102         ProjectBuildingRequest config = new DefaultProjectBuildingRequest();
103 
104         for ( org.apache.maven.settings.Profile rawProfile : settings.getProfiles() )
105         {
106             Profile profile = SettingsUtils.convertFromSettingsProfile( rawProfile );
107             config.addProfile( profile );
108         }
109 
110         String localRepoUrl =
111             System.getProperty( "maven.repo.local", System.getProperty( "user.home" ) + "/.m2/repository" );
112         localRepoUrl = "file://" + localRepoUrl;
113         config.setLocalRepository( repositorySystem.createArtifactRepository( "local", localRepoUrl,
114                                                                               new DefaultRepositoryLayout(), null, null ) );
115         config.setActiveProfileIds( settings.getActiveProfiles() );
116         MavenRepositorySystemSession repoSession = new MavenRepositorySystemSession();
117         repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager(
118                                                                                  new File(
119                                                                                            config.getLocalRepository().getBasedir() ) ) );
120         config.setRepositorySession( repoSession );
121 
122         return new PomTestWrapper( pomFile, projectBuilder.build( pomFile, config ).getProject() );
123     }
124 
125     private static Settings readSettingsFile( File settingsFile )
126         throws IOException, XmlPullParserException
127     {
128         Settings settings = null;
129 
130         Reader reader = null;
131 
132         try
133         {
134             reader = ReaderFactory.newXmlReader( settingsFile );
135 
136             SettingsXpp3Reader modelReader = new SettingsXpp3Reader();
137 
138             settings = modelReader.read( reader );
139         }
140         finally
141         {
142             IOUtil.close( reader );
143         }
144 
145         return settings;
146     }
147 }