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