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