1   package org.apache.maven;
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.manager.WagonConfigurationException;
23  import org.apache.maven.artifact.manager.WagonManager;
24  import org.apache.maven.artifact.manager.WagonProviderMapping;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.repository.DefaultArtifactRepository;
27  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
28  import org.apache.maven.artifact.versioning.ArtifactVersion;
29  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
30  import org.apache.maven.execution.DefaultMavenExecutionRequest;
31  import org.apache.maven.execution.MavenExecutionRequest;
32  import org.apache.maven.execution.RuntimeInformation;
33  import org.apache.maven.model.Model;
34  import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
35  import org.apache.maven.monitor.event.DefaultEventDispatcher;
36  import org.apache.maven.monitor.event.EventDispatcher;
37  import org.apache.maven.profiles.DefaultProfileManager;
38  import org.apache.maven.profiles.ProfileManager;
39  import org.apache.maven.reactor.MavenExecutionException;
40  import org.apache.maven.settings.Settings;
41  import org.apache.maven.wagon.UnsupportedProtocolException;
42  import org.apache.maven.wagon.Wagon;
43  import org.apache.maven.wagon.providers.http.HttpWagon;
44  import org.apache.maven.wagon.providers.http.LightweightHttpWagon;
45  import org.apache.maven.wagon.providers.http.LightweightHttpsWagon;
46  import org.apache.maven.wagon.repository.Repository;
47  import org.codehaus.plexus.PlexusTestCase;
48  import org.codehaus.plexus.util.FileUtils;
49  import org.codehaus.plexus.util.IOUtil;
50  import org.codehaus.plexus.util.xml.Xpp3Dom;
51  
52  import java.io.File;
53  import java.io.FileWriter;
54  import java.io.IOException;
55  import java.util.ArrayList;
56  import java.util.List;
57  import java.util.Properties;
58  
59  public class WagonSelectorTest
60      extends PlexusTestCase
61  {
62  
63      private WagonProviderMapping mapping;
64  
65      private WagonManager manager;
66  
67      private ArtifactRepository localRepository;
68  
69      private Maven maven;
70  
71      private File dir;
72  
73      public void setUp()
74          throws Exception
75      {
76          super.setUp();
77  
78          mapping = (WagonProviderMapping) lookup( WagonProviderMapping.ROLE );
79  
80          manager = (WagonManager) lookup( WagonManager.ROLE );
81  
82          ArtifactRepositoryLayout layout =
83              (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.class.getName(), "default" );
84          localRepository = new DefaultArtifactRepository( "local", System.getProperty( "java.io.tmpdir" ), layout );
85  
86          maven = (Maven) lookup( Maven.class.getName() );
87      }
88  
89      public void tearDown()
90          throws Exception
91      {
92          release( mapping );
93          release( manager );
94          super.tearDown();
95          
96          if ( dir != null )
97          {
98              try
99              {
100                 FileUtils.forceDelete( dir );
101             }
102             catch ( IOException e )
103             {
104             }
105         }
106     }
107 
108     public void testSelectHttpWagonFromDefault()
109         throws Exception
110     {
111         Properties executionProperties = new Properties();
112 
113         MavenExecutionRequest req = createMavenRequest( executionProperties );
114 
115         Wagon wagon = manager.getWagon( new Repository( "id", "http://www.google.com/" ) );
116 
117         assertTrue( "Should use " + LightweightHttpWagon.class.getName(), wagon instanceof LightweightHttpWagon );
118     }
119 
120     public void testSelectHttpsWagonFromDefault()
121         throws Exception
122     {
123         Properties executionProperties = new Properties();
124 
125         MavenExecutionRequest req = createMavenRequest( executionProperties );
126 
127         Wagon wagon = manager.getWagon( new Repository( "id", "https://www.google.com/" ) );
128 
129         assertTrue( "Should use " + LightweightHttpsWagon.class.getName(), wagon instanceof LightweightHttpsWagon );
130     }
131 
132     public void testSelectHttpclientWagonFromSimulatedMavenCliConfiguration()
133         throws WagonConfigurationException, UnsupportedProtocolException
134     {
135         mapping.setWagonProvider( "http", "httpclient" );
136 
137         Wagon wagon = manager.getWagon( new Repository( "id", "http://www.google.com/" ) );
138 
139         assertTrue( "Should use " + HttpWagon.class.getName(), wagon instanceof HttpWagon );
140     }
141 
142     public void testSelectHttpclientWagonFromServerConfiguration()
143         throws WagonConfigurationException, UnsupportedProtocolException
144     {
145         Xpp3Dom config = new Xpp3Dom( "configuration" );
146         Xpp3Dom provider = new Xpp3Dom( "wagonProvider" );
147         provider.setValue( "httpclient" );
148         config.addChild( provider );
149 
150         manager.addConfiguration( "id", config );
151 
152         Wagon wagon = manager.getWagon( new Repository( "id", "http://www.google.com/" ) );
153         assertTrue( "Should use " + HttpWagon.class.getName(), wagon instanceof HttpWagon );
154     }
155 
156     public void testSelectHttpclientWagonFromMavenCLIParameter()
157         throws WagonConfigurationException, UnsupportedProtocolException, MavenExecutionException, IOException
158     {
159         Properties executionProperties = new Properties();
160         executionProperties.setProperty( "maven.wagon.provider.http", "httpclient" );
161 
162         MavenExecutionRequest req = createMavenRequest( executionProperties );
163 
164         Wagon wagon = manager.getWagon( new Repository( "id", "http://www.google.com/" ) );
165         assertTrue( "Should use " + HttpWagon.class.getName(), wagon instanceof HttpWagon );
166     }
167 
168     public MavenExecutionRequest createMavenRequest( Properties executionProperties )
169         throws IOException, MavenExecutionException
170     {
171         Settings settings = new Settings();
172         EventDispatcher eventDispatcher = new DefaultEventDispatcher();
173 
174         List<String> goals = new ArrayList<String>();
175         goals.add( "validate" );
176 
177         dir = File.createTempFile( "WagonSelectorTest.", ".dir" );
178         dir.delete();
179         dir.mkdirs();
180 
181         File pom = new File( dir, "pom.xml" );
182 
183         Model model = new Model();
184         model.setModelVersion( "4.0.0" );
185         model.setGroupId( "wagon.selector.test" );
186         model.setArtifactId( "wagon-selector-test" );
187         model.setVersion( "1" );
188         model.setPackaging( "pom" );
189         
190         FileWriter writer = null;
191         try
192         {
193             writer = new FileWriter( pom );
194             new MavenXpp3Writer().write( writer, model );
195         }
196         finally
197         {
198             IOUtil.close( writer );
199         }
200 
201         String baseDirectory = dir.getAbsolutePath();
202         ProfileManager globalProfileManager = new DefaultProfileManager( getContainer(), new Properties() );
203 
204         boolean showErrors = false;
205         Properties userProperties = new Properties();
206 
207         MavenExecutionRequest req =
208             new DefaultMavenExecutionRequest( localRepository, settings, eventDispatcher, goals, baseDirectory,
209                                               globalProfileManager, executionProperties, userProperties, showErrors );
210         
211         req.setPomFile( pom.getAbsolutePath() );
212 
213         maven.execute( req );
214 
215         return req;
216     }
217 
218     public static final class TestRuntimeInformation
219         implements RuntimeInformation
220     {
221 
222         public ArtifactVersion getApplicationVersion()
223         {
224             return new DefaultArtifactVersion( "TEST" );
225         }
226 
227     }
228 
229 }