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