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