View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.settings;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.Reader;
24  
25  import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
26  import org.apache.maven.model.Profile;
27  import org.apache.maven.project.DefaultProjectBuilder;
28  import org.apache.maven.project.DefaultProjectBuildingRequest;
29  import org.apache.maven.project.ProjectBuilder;
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.ContainerConfiguration;
36  import org.codehaus.plexus.PlexusConstants;
37  import org.codehaus.plexus.PlexusTestCase;
38  import org.codehaus.plexus.util.ReaderFactory;
39  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
40  import org.eclipse.aether.DefaultRepositorySystemSession;
41  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
42  import org.eclipse.aether.repository.LocalRepository;
43  
44  public class PomConstructionWithSettingsTest extends PlexusTestCase {
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      @Override
56      protected void customizeContainerConfiguration(ContainerConfiguration containerConfiguration) {
57          super.customizeContainerConfiguration(containerConfiguration);
58          containerConfiguration.setAutoWiring(true);
59          containerConfiguration.setClassPathScanning(PlexusConstants.SCANNING_INDEX);
60      }
61  
62      protected void setUp() throws Exception {
63          testDirectory = new File(getBasedir(), BASE_POM_DIR);
64          projectBuilder = (DefaultProjectBuilder) lookup(ProjectBuilder.class);
65          repositorySystem = lookup(RepositorySystem.class);
66      }
67  
68      @Override
69      protected void tearDown() throws Exception {
70          projectBuilder = null;
71  
72          super.tearDown();
73      }
74  
75      public void testSettingsNoPom() throws Exception {
76          PomTestWrapper pom = buildPom("settings-no-pom");
77          assertEquals("local-profile-prop-value", pom.getValue("properties/local-profile-prop"));
78      }
79  
80      /**
81       * MNG-4107
82       */
83      public void testPomAndSettingsInterpolation() throws Exception {
84          PomTestWrapper pom = buildPom("test-pom-and-settings-interpolation");
85          assertEquals("applied", pom.getValue("properties/settingsProfile"));
86          assertEquals("applied", pom.getValue("properties/pomProfile"));
87          assertEquals("settings", pom.getValue("properties/pomVsSettings"));
88          assertEquals("settings", pom.getValue("properties/pomVsSettingsInterpolated"));
89      }
90  
91      /**
92       * MNG-4107
93       */
94      public void testRepositories() throws Exception {
95          PomTestWrapper pom = buildPom("repositories");
96          assertEquals("maven-core-it-0", pom.getValue("repositories[1]/id"));
97      }
98  
99      private PomTestWrapper buildPom(String pomPath) throws Exception {
100         File pomFile = new File(testDirectory + File.separator + pomPath, "pom.xml");
101         File settingsFile = new File(testDirectory + File.separator + pomPath, "settings.xml");
102         Settings settings = readSettingsFile(settingsFile);
103 
104         ProjectBuildingRequest config = new DefaultProjectBuildingRequest();
105 
106         for (org.apache.maven.settings.Profile rawProfile : settings.getProfiles()) {
107             Profile profile = SettingsUtils.convertFromSettingsProfile(rawProfile);
108             config.addProfile(profile);
109         }
110 
111         String localRepoUrl =
112                 System.getProperty("maven.repo.local", System.getProperty("user.home") + "/.m2/repository");
113         localRepoUrl = "file://" + localRepoUrl;
114         config.setLocalRepository(repositorySystem.createArtifactRepository(
115                 "local", localRepoUrl, new DefaultRepositoryLayout(), null, null));
116         config.setActiveProfileIds(settings.getActiveProfiles());
117 
118         DefaultRepositorySystemSession repoSession = MavenRepositorySystemUtils.newSession();
119         LocalRepository localRepo =
120                 new LocalRepository(config.getLocalRepository().getBasedir());
121         repoSession.setLocalRepositoryManager(
122                 new SimpleLocalRepositoryManagerFactory().newInstance(repoSession, localRepo));
123         config.setRepositorySession(repoSession);
124 
125         return new PomTestWrapper(pomFile, projectBuilder.build(pomFile, config).getProject());
126     }
127 
128     private static Settings readSettingsFile(File settingsFile) throws IOException, XmlPullParserException {
129         Settings settings = null;
130 
131         try (Reader reader = ReaderFactory.newXmlReader(settingsFile)) {
132             SettingsXpp3Reader modelReader = new SettingsXpp3Reader();
133 
134             settings = modelReader.read(reader);
135         }
136         return settings;
137     }
138 }