001 package org.apache.maven.settings;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import java.io.File;
023 import java.io.IOException;
024 import java.io.Reader;
025
026 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
027 import org.apache.maven.model.Profile;
028 import org.apache.maven.project.DefaultProjectBuilder;
029 import org.apache.maven.project.DefaultProjectBuildingRequest;
030 import org.apache.maven.project.ProjectBuilder;
031 import org.apache.maven.project.ProjectBuildingRequest;
032 import org.apache.maven.project.harness.PomTestWrapper;
033 import org.apache.maven.repository.RepositorySystem;
034 import org.apache.maven.repository.internal.MavenRepositorySystemSession;
035 import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
036 import org.codehaus.plexus.PlexusTestCase;
037 import org.codehaus.plexus.util.IOUtil;
038 import org.codehaus.plexus.util.ReaderFactory;
039 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
040 import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
041
042 public class PomConstructionWithSettingsTest
043 extends PlexusTestCase
044 {
045 private static final String BASE_DIR = "src/test";
046
047 private static final String BASE_POM_DIR = BASE_DIR + "/resources-settings";
048
049 private DefaultProjectBuilder projectBuilder;
050
051 private RepositorySystem repositorySystem;
052
053 private File testDirectory;
054
055 protected void setUp()
056 throws Exception
057 {
058 testDirectory = new File( getBasedir(), BASE_POM_DIR );
059 projectBuilder = (DefaultProjectBuilder) lookup( ProjectBuilder.class );
060 repositorySystem = lookup( RepositorySystem.class );
061 }
062
063 @Override
064 protected void tearDown()
065 throws Exception
066 {
067 projectBuilder = null;
068
069 super.tearDown();
070 }
071
072 public void testSettingsNoPom() throws Exception
073 {
074 PomTestWrapper pom = buildPom( "settings-no-pom" );
075 assertEquals( "local-profile-prop-value", pom.getValue( "properties/local-profile-prop" ) );
076 }
077
078 /**MNG-4107 */
079 public void testPomAndSettingsInterpolation() throws Exception
080 {
081 PomTestWrapper pom = buildPom( "test-pom-and-settings-interpolation" );
082 assertEquals( "applied", pom.getValue( "properties/settingsProfile" ) );
083 assertEquals( "applied", pom.getValue( "properties/pomProfile" ) );
084 assertEquals( "settings", pom.getValue( "properties/pomVsSettings" ) );
085 assertEquals( "settings", pom.getValue( "properties/pomVsSettingsInterpolated" ) );
086 }
087
088 /**MNG-4107 */
089 public void testRepositories() throws Exception
090 {
091 PomTestWrapper pom = buildPom( "repositories" );
092 assertEquals( "maven-core-it-0", pom.getValue( "repositories[1]/id" ) );
093 }
094
095 private PomTestWrapper buildPom( String pomPath )
096 throws Exception
097 {
098 File pomFile = new File( testDirectory + File.separator + pomPath, "pom.xml" );
099 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 }