1   package org.apache.maven.project.injection;
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.util.Collections;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  
27  import junit.framework.TestCase;
28  
29  import org.apache.maven.model.Build;
30  import org.apache.maven.model.BuildBase;
31  import org.apache.maven.model.Model;
32  import org.apache.maven.model.Plugin;
33  import org.apache.maven.model.PluginContainer;
34  import org.apache.maven.model.PluginExecution;
35  import org.apache.maven.model.Profile;
36  import org.apache.maven.model.Repository;
37  import org.codehaus.plexus.util.xml.Xpp3Dom;
38  import org.apache.maven.model.Dependency;
39  
40  import junit.framework.TestCase;
41  
42  public class DefaultProfileInjectorTest
43      extends TestCase
44  {
45  
46      public void testShouldUseProfilePluginDependencyVersionOverMainPluginDepVersion()
47      {
48          PluginContainer profile = new PluginContainer();
49          Plugin profilePlugin = createPlugin( "group", "artifact", "1", Collections.EMPTY_MAP );
50          Dependency profileDep = createDependency( "g", "a", "2" );
51          profilePlugin.addDependency( profileDep );
52          profile.addPlugin( profilePlugin );
53  
54          PluginContainer model = new PluginContainer();
55          Plugin plugin = createPlugin( "group", "artifact", "1", Collections.EMPTY_MAP );
56          Dependency dep = createDependency( "g", "a", "1" );
57          plugin.addDependency( dep );
58          model.addPlugin( plugin );
59  
60          new DefaultProfileInjector().injectPlugins( profile, model );
61  
62          assertEquals( profileDep.getVersion(), ((Dependency) plugin.getDependencies().get( 0 ) ).getVersion() );
63      }
64  
65      private Dependency createDependency( String gid,
66                                           String aid,
67                                           String ver )
68      {
69          Dependency dep = new Dependency();
70          dep.setGroupId( gid );
71          dep.setArtifactId( aid );
72          dep.setVersion( ver );
73  
74          return dep;
75      }
76  
77      /**
78       * Test that this is the resulting ordering of plugins after merging:
79       *
80       * Given:
81       *
82       *   model: X -> A -> B -> D -> E
83       *   profile: Y -> A -> C -> D -> F
84       *
85       * Result:
86       *
87       *   X -> Y -> A -> B -> C -> D -> E -> F
88       */
89      public void testShouldPreserveOrderingOfPluginsAfterProfileMerge()
90      {
91          PluginContainer profile = new PluginContainer();
92  
93          profile.addPlugin( createPlugin( "group", "artifact", "1.0", Collections.EMPTY_MAP ) );
94          profile.addPlugin( createPlugin( "group2", "artifact2", "1.0", Collections.singletonMap( "key", "value" ) ) );
95  
96          PluginContainer model = new PluginContainer();
97  
98          model.addPlugin( createPlugin( "group3", "artifact3", "1.0", Collections.EMPTY_MAP ) );
99          model.addPlugin( createPlugin( "group2", "artifact2", "1.0", Collections.singletonMap( "key2", "value2" ) ) );
100 
101         new DefaultProfileInjector().injectPlugins( profile, model );
102 
103         List results = model.getPlugins();
104 
105         assertEquals( 3, results.size() );
106 
107         Plugin result1 = (Plugin) results.get( 0 );
108 
109         assertEquals( "group3", result1.getGroupId() );
110         assertEquals( "artifact3", result1.getArtifactId() );
111 
112         Plugin result2 = (Plugin) results.get( 1 );
113 
114         assertEquals( "group", result2.getGroupId() );
115         assertEquals( "artifact", result2.getArtifactId() );
116 
117         Plugin result3 = (Plugin) results.get( 2 );
118 
119         assertEquals( "group2", result3.getGroupId() );
120         assertEquals( "artifact2", result3.getArtifactId() );
121 
122         Xpp3Dom result3Config = (Xpp3Dom) result3.getConfiguration();
123 
124         assertNotNull( result3Config );
125 
126         assertEquals( "value", result3Config.getChild( "key" ).getValue() );
127         assertEquals( "value2", result3Config.getChild( "key2" ).getValue() );
128     }
129 
130     private Plugin createPlugin( String groupId, String artifactId, String version, Map configuration )
131     {
132         Plugin plugin = new Plugin();
133         plugin.setGroupId( groupId );
134         plugin.setArtifactId( artifactId );
135         plugin.setVersion( version );
136 
137         Xpp3Dom config = new Xpp3Dom( "configuration" );
138 
139         if( configuration != null )
140         {
141             for ( Iterator it = configuration.entrySet().iterator(); it.hasNext(); )
142             {
143                 Map.Entry entry = (Map.Entry) it.next();
144 
145                 Xpp3Dom param = new Xpp3Dom( String.valueOf( entry.getKey() ) );
146                 param.setValue( String.valueOf( entry.getValue() ) );
147 
148                 config.addChild( param );
149             }
150         }
151 
152         plugin.setConfiguration( config );
153 
154         return plugin;
155     }
156 
157     public void testProfilePluginConfigurationShouldOverrideCollidingModelPluginConfiguration()
158     {
159         Plugin mPlugin = new Plugin();
160         mPlugin.setGroupId( "test" );
161         mPlugin.setArtifactId( "test-artifact" );
162         mPlugin.setVersion( "1.0-SNAPSHOT" );
163 
164         Xpp3Dom mConfigChild = new Xpp3Dom( "test" );
165         mConfigChild.setValue( "value" );
166 
167         Xpp3Dom mConfigChild2 = new Xpp3Dom( "test2" );
168         mConfigChild2.setValue( "value2" );
169 
170         Xpp3Dom mConfig = new Xpp3Dom( "configuration" );
171         mConfig.addChild( mConfigChild );
172         mConfig.addChild( mConfigChild2 );
173 
174         mPlugin.setConfiguration( mConfig );
175 
176         Build mBuild = new Build();
177         mBuild.addPlugin( mPlugin );
178 
179         Model model = new Model();
180         model.setBuild( mBuild );
181 
182         Plugin pPlugin = new Plugin();
183         pPlugin.setGroupId( "test" );
184         pPlugin.setArtifactId( "test-artifact" );
185         pPlugin.setVersion( "1.0-SNAPSHOT" );
186 
187         Xpp3Dom pConfigChild = new Xpp3Dom( "test" );
188         pConfigChild.setValue( "replacedValue" );
189 
190         Xpp3Dom pConfig = new Xpp3Dom( "configuration" );
191         pConfig.addChild( pConfigChild );
192 
193         pPlugin.setConfiguration( pConfig );
194 
195         BuildBase pBuild = new BuildBase();
196         pBuild.addPlugin( pPlugin );
197 
198         Profile profile = new Profile();
199         profile.setId( "testId" );
200 
201         profile.setBuild( pBuild );
202 
203         new DefaultProfileInjector().inject( profile, model );
204 
205         Build rBuild = model.getBuild();
206         Plugin rPlugin = (Plugin) rBuild.getPlugins().get( 0 );
207         Xpp3Dom rConfig = (Xpp3Dom) rPlugin.getConfiguration();
208 
209         Xpp3Dom rChild = rConfig.getChild( "test" );
210 
211         assertEquals( "replacedValue", rChild.getValue() );
212 
213         Xpp3Dom rChild2 = rConfig.getChild( "test2" );
214 
215         assertEquals( "value2", rChild2.getValue() );
216     }
217 
218     public void testModelConfigShouldPersistWhenPluginHasExecConfigs()
219     {
220         Plugin mPlugin = new Plugin();
221         mPlugin.setGroupId( "test" );
222         mPlugin.setArtifactId( "test-artifact" );
223         mPlugin.setVersion( "1.0-SNAPSHOT" );
224 
225         Xpp3Dom mConfigChild = new Xpp3Dom( "test" );
226         mConfigChild.setValue( "value" );
227 
228         Xpp3Dom mConfigChild2 = new Xpp3Dom( "test2" );
229         mConfigChild2.setValue( "value2" );
230 
231         Xpp3Dom mConfig = new Xpp3Dom( "configuration" );
232         mConfig.addChild( mConfigChild );
233         mConfig.addChild( mConfigChild2 );
234 
235         mPlugin.setConfiguration( mConfig );
236 
237         Build mBuild = new Build();
238         mBuild.addPlugin( mPlugin );
239 
240         Model model = new Model();
241         model.setBuild( mBuild );
242 
243         Plugin pPlugin = new Plugin();
244         pPlugin.setGroupId( "test" );
245         pPlugin.setArtifactId( "test-artifact" );
246         pPlugin.setVersion( "1.0-SNAPSHOT" );
247 
248         PluginExecution pExec = new PluginExecution();
249         pExec.setId("profile-injected");
250 
251         Xpp3Dom pConfigChild = new Xpp3Dom( "test" );
252         pConfigChild.setValue( "replacedValue" );
253 
254         Xpp3Dom pConfig = new Xpp3Dom( "configuration" );
255         pConfig.addChild( pConfigChild );
256 
257         pExec.setConfiguration( pConfig );
258 
259         pPlugin.addExecution( pExec );
260 
261         BuildBase pBuild = new BuildBase();
262         pBuild.addPlugin( pPlugin );
263 
264         Profile profile = new Profile();
265         profile.setId( "testId" );
266 
267         profile.setBuild( pBuild );
268 
269         new DefaultProfileInjector().inject( profile, model );
270 
271         Build rBuild = model.getBuild();
272         Plugin rPlugin = (Plugin) rBuild.getPlugins().get( 0 );
273 
274         PluginExecution rExec = (PluginExecution) rPlugin.getExecutionsAsMap().get( "profile-injected" );
275 
276         assertNotNull( rExec );
277 
278         Xpp3Dom rExecConfig = (Xpp3Dom) rExec.getConfiguration();
279 
280         Xpp3Dom rChild = rExecConfig.getChild( "test" );
281 
282         assertEquals( "replacedValue", rChild.getValue() );
283 
284         Xpp3Dom rConfig = (Xpp3Dom) rPlugin.getConfiguration();
285 
286         assertNotNull( rConfig );
287 
288         Xpp3Dom rChild2 = rConfig.getChild( "test2" );
289 
290         assertEquals( "value2", rChild2.getValue() );
291     }
292 
293     public void testProfileRepositoryShouldOverrideModelRepository()
294     {
295         Repository mRepository = new Repository();
296         mRepository.setId( "testId" );
297         mRepository.setName( "Test repository" );
298         mRepository.setUrl( "http://www.google.com" );
299 
300         Model model = new Model();
301         model.addRepository( mRepository );
302 
303         Repository pRepository = new Repository();
304         pRepository.setId( "testId" );
305         pRepository.setName( "Test repository" );
306         pRepository.setUrl( "http://www.yahoo.com" );
307 
308         Profile profile = new Profile();
309         profile.setId( "testId" );
310 
311         profile.addRepository( pRepository );
312 
313         new DefaultProfileInjector().inject( profile, model );
314 
315         Repository rRepository = (Repository) model.getRepositories().get( 0 );
316 
317         assertEquals( "http://www.yahoo.com", rRepository.getUrl() );
318     }
319 
320     public void testShouldPreserveModelModulesWhenProfileHasNone()
321     {
322         Model model = new Model();
323 
324         model.addModule( "module1" );
325 
326         Profile profile = new Profile();
327         profile.setId( "testId" );
328 
329         new DefaultProfileInjector().inject( profile, model );
330 
331         List rModules = model.getModules();
332 
333         assertEquals( 1, rModules.size() );
334         assertEquals( "module1", rModules.get( 0 ) );
335     }
336 
337     // NOTE: The execution-id's are important, because they are NOT in
338     // alphabetical order. The trunk version of Maven currently injects
339     // profiles into a TreeMap, then calls map.values(), which puts the
340     // executions in alphabetical order...the WRONG order.
341     public void testShouldPreserveOrderingOfProfileInjectedPluginExecutions()
342     {
343         Plugin profilePlugin = new Plugin();
344         profilePlugin.setGroupId( "group" );
345         profilePlugin.setArtifactId( "artifact" );
346         profilePlugin.setVersion( "version" );
347 
348         PluginExecution exec1 = new PluginExecution();
349         exec1.setId( "z" );
350         profilePlugin.addExecution( exec1 );
351 
352         PluginExecution exec2 = new PluginExecution();
353         exec2.setId( "y" );
354         profilePlugin.addExecution( exec2 );
355 
356         BuildBase buildBase = new BuildBase();
357         buildBase.addPlugin( profilePlugin );
358 
359         Profile profile = new Profile();
360         profile.setBuild( buildBase );
361 
362         Plugin modelPlugin = new Plugin();
363         modelPlugin.setGroupId( "group" );
364         modelPlugin.setArtifactId( "artifact" );
365         modelPlugin.setVersion( "version" );
366 
367         PluginExecution exec3 = new PluginExecution();
368         exec3.setId( "w" );
369         modelPlugin.addExecution( exec3 );
370 
371         PluginExecution exec4 = new PluginExecution();
372         exec4.setId( "x" );
373         modelPlugin.addExecution( exec4 );
374 
375         Build build = new Build();
376         build.addPlugin( modelPlugin );
377 
378         Model model = new Model();
379         model.setBuild( build );
380 
381         new DefaultProfileInjector().inject( profile, model );
382 
383         List plugins = model.getBuild().getPlugins();
384         assertNotNull( plugins );
385         assertEquals( 1, plugins.size() );
386 
387         Plugin plugin = (Plugin) plugins.get( 0 );
388 
389         List executions = plugin.getExecutions();
390         assertNotNull( executions );
391         assertEquals( 4, executions.size() );
392 
393         Iterator it = executions.iterator();
394 
395         PluginExecution e = (PluginExecution) it.next();
396         assertEquals( "w", e.getId() );
397 
398         e = (PluginExecution) it.next();
399         assertEquals( "x", e.getId() );
400 
401         e = (PluginExecution) it.next();
402         assertEquals( "z", e.getId() );
403 
404         e = (PluginExecution) it.next();
405         assertEquals( "y", e.getId() );
406 
407     }
408 }