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