View Javadoc
1   package org.apache.maven.plugins.enforcer;
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.util.ArrayList;
25  import java.util.Collection;
26  import java.util.HashSet;
27  import java.util.List;
28  import java.util.Set;
29  
30  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
31  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
32  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
33  import org.apache.maven.model.Plugin;
34  import org.apache.maven.plugin.MojoExecutionException;
35  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
36  import org.apache.maven.plugins.enforcer.utils.EnforcerRuleUtils;
37  import org.apache.maven.plugins.enforcer.utils.PluginWrapper;
38  import org.codehaus.plexus.util.StringUtils;
39  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
40  
41  /**
42   * The Class TestRequirePluginVersions.
43   *
44   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
45   */
46  public class TestRequirePluginVersions
47      extends AbstractMojoTestCase
48  {
49  
50      /**
51       * Test has version specified.
52       */
53      public void testHasVersionSpecified()
54      {
55          Plugin source = new Plugin();
56          source.setArtifactId( "foo" );
57          source.setGroupId( "group" );
58  
59          // setup the plugins. I'm setting up the foo group
60          // with a few bogus entries and then a real one.
61          // this is to test that the list is exhaustively
62          // searched for versions before giving up.
63          // banLatest/Release will fail if it is found
64          // anywhere in the list
65          List<Plugin> plugins = new ArrayList<Plugin>();
66          plugins.add( EnforcerTestUtils.newPlugin( "group", "a-artifact", "1.0" ) );
67          plugins.add( EnforcerTestUtils.newPlugin( "group", "foo", null ) );
68          plugins.add( EnforcerTestUtils.newPlugin( "group", "foo", "" ) );
69          plugins.add( EnforcerTestUtils.newPlugin( "group", "b-artifact", "1.0" ) );
70          plugins.add( EnforcerTestUtils.newPlugin( "group", "foo", "1.0" ) );
71          plugins.add( EnforcerTestUtils.newPlugin( "group", "c-artifact", "LATEST" ) );
72          plugins.add( EnforcerTestUtils.newPlugin( "group", "c-artifact", "1.0" ) );
73          plugins.add( EnforcerTestUtils.newPlugin( "group", "d-artifact", "RELEASE" ) );
74          plugins.add( EnforcerTestUtils.newPlugin( "group", "d-artifact", "1.0" ) );
75          plugins.add( EnforcerTestUtils.newPlugin( "group", "e-artifact", "1.0" ) );
76          plugins.add( EnforcerTestUtils.newPlugin( "group", "e-artifact", "RELEASE" ) );
77          plugins.add( EnforcerTestUtils.newPlugin( "group", "f-artifact", "1.0" ) );
78          plugins.add( EnforcerTestUtils.newPlugin( "group", "f-artifact", "LATEST" ) );
79          plugins.add( EnforcerTestUtils.newPlugin( "group", "f-artifact", "1.0-SNAPSHOT" ) );
80          plugins.add( EnforcerTestUtils.newPlugin( "group", "g-artifact", "1.0-12345678.123456-1" ) );
81  
82  
83          List<PluginWrapper> pluginWrappers = PluginWrapper.addAll( plugins, "unit" );
84  
85          RequirePluginVersions rule = new RequirePluginVersions();
86          rule.setBanLatest( false );
87          rule.setBanRelease( false );
88          rule.setBanSnapshots( false );
89  
90          EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
91  
92          assertTrue( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
93  
94          // check that LATEST is allowed
95          source.setArtifactId( "c-artifact" );
96          assertTrue( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
97  
98          // check that LATEST is banned
99          rule.setBanLatest( true );
100         assertFalse( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
101 
102         // check that LATEST is exhausively checked
103         rule.setBanSnapshots( false );
104         source.setArtifactId( "f-artifact" );
105         assertFalse( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
106 
107         rule.setBanLatest( false );
108         rule.setBanSnapshots( true );
109         assertFalse( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
110 
111         // check that TIMESTAMP is allowed
112         rule.setBanTimestamps( false );
113         source.setArtifactId( "g-artifact" );
114         assertTrue( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
115 
116         // check that RELEASE is allowed
117         source.setArtifactId( "d-artifact" );
118         assertTrue( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
119 
120         // check that RELEASE is banned
121         rule.setBanRelease( true );
122         assertFalse( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
123 
124         // check that RELEASE is exhaustively checked
125         source.setArtifactId( "e-artifact" );
126         assertFalse( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
127     }
128 
129     /**
130      * Test has version specified with properties.
131      */
132     public void testHasVersionSpecifiedWithProperties()
133     {
134         Plugin source = new Plugin();
135         source.setGroupId( "group" );
136 
137         // setup the plugins.
138         List<Plugin> plugins = new ArrayList<Plugin>();
139         plugins.add( EnforcerTestUtils.newPlugin( "group", "a-artifact", "1.0-${SNAPSHOT}" ) );
140         plugins.add( EnforcerTestUtils.newPlugin( "group", "b-artifact", "${1.0}" ) );
141         plugins.add( EnforcerTestUtils.newPlugin( "group", "c-artifact", "${LATEST}" ) );
142         plugins.add( EnforcerTestUtils.newPlugin( "group", "d-artifact", "${RELEASE}" ) );
143         plugins.add( EnforcerTestUtils.newPlugin( "group", "e-artifact", "${}" ) );
144         plugins.add( EnforcerTestUtils.newPlugin( "group", "f-artifact", "${   }" ) );
145 
146         List<PluginWrapper> pluginWrappers = PluginWrapper.addAll( plugins, "unit" );
147 
148         RequirePluginVersions rule = new RequirePluginVersions();
149         rule.setBanLatest( false );
150         rule.setBanRelease( false );
151         rule.setBanSnapshots( false );
152 
153         EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( true );
154 
155         source.setArtifactId( "a-artifact" );
156         assertTrue( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
157 
158         source.setArtifactId( "b-artifact" );
159         assertTrue( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
160 
161         source.setArtifactId( "c-artifact" );
162         assertTrue( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
163 
164         source.setArtifactId( "d-artifact" );
165         assertTrue( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
166 
167         // this one checks empty property values
168         source.setArtifactId( "e-artifact" );
169         assertFalse( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
170 
171         // this one checks empty property values
172         source.setArtifactId( "f-artifact" );
173         assertFalse( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
174 
175         rule.setBanLatest( true );
176         source.setArtifactId( "c-artifact" );
177         assertFalse( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
178 
179         rule.setBanRelease( true );
180         source.setArtifactId( "d-artifact" );
181         assertFalse( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
182 
183         rule.setBanSnapshots( true );
184         source.setArtifactId( "a-artifact" );
185         assertFalse( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
186 
187         // release versions should pass everything
188         source.setArtifactId( "b-artifact" );
189         assertTrue( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
190     }
191 
192     /**
193      * Test get all plugins.
194      *
195      * @throws ArtifactResolutionException the artifact resolution exception
196      * @throws ArtifactNotFoundException the artifact not found exception
197      * @throws IOException Signals that an I/O exception has occurred.
198      * @throws XmlPullParserException the xml pull parser exception
199      */
200     public void testGetAllPlugins()
201         throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
202     {
203         RequirePluginVersions rule = new RequirePluginVersions();
204         String path = "target/test-classes/requirePluginVersions/getPomRecursively/b/c";
205 
206         StringUtils.replace( path, "/", File.separator );
207 
208         File projectDir = new File( getBasedir(), path );
209 
210         MockProject project = new MockProject();
211         project.setArtifactId( "c" );
212         project.setGroupId( "group" );
213         project.setVersion( "1.0" );
214         project.setBaseDir( projectDir );
215 
216         rule.setUtils( new EnforcerRuleUtils( EnforcerTestUtils.getHelper( project ) ) );
217         List<PluginWrapper> plugins = rule.getAllPluginEntries( project );
218 
219         // there should be 3
220         assertEquals( 3, plugins.size() );
221     }
222 
223     /**
224      * Test get additional plugins null.
225      *
226      * @throws MojoExecutionException the mojo execution exception
227      */
228     public void testGetAdditionalPluginsNull()
229         throws MojoExecutionException
230     {
231         RequirePluginVersions rule = new RequirePluginVersions();
232         rule.addAdditionalPlugins( null, null );
233     }
234 
235     /**
236      * Test get additional plugins invalid format.
237      */
238     public void testGetAdditionalPluginsInvalidFormat()
239     {
240         RequirePluginVersions rule = new RequirePluginVersions();
241 
242         List<String> additional = new ArrayList<String>();
243 
244         // invalid format (not enough sections)
245         additional.add( "group" );
246 
247         Set<Plugin> plugins = new HashSet<Plugin>();
248         try
249         {
250             rule.addAdditionalPlugins( plugins, additional );
251             fail( "Expected Exception because the format is invalid" );
252         }
253         catch ( MojoExecutionException e )
254         {
255         }
256 
257         // invalid format (too many sections)
258         additional.clear();
259         additional.add( "group:i:i" );
260         try
261         {
262             rule.addAdditionalPlugins( plugins, additional );
263             fail( "Expected Exception because the format is invalid" );
264         }
265         catch ( MojoExecutionException e )
266         {
267         }
268 
269     }
270 
271     /**
272      * Test get additional plugins empty set.
273      *
274      * @throws MojoExecutionException the mojo execution exception
275      */
276     public void testGetAdditionalPluginsEmptySet()
277         throws MojoExecutionException
278     {
279         RequirePluginVersions rule = new RequirePluginVersions();
280 
281         Set<Plugin> plugins = new HashSet<Plugin>();
282         plugins.add( EnforcerTestUtils.newPlugin( "group", "a-artifact", "1.0" ) );
283         plugins.add( EnforcerTestUtils.newPlugin( "group", "foo", null ) );
284         plugins.add( EnforcerTestUtils.newPlugin( "group", "foo2", "" ) );
285 
286         List<String> additional = new ArrayList<String>();
287         additional.add( "group:a-artifact" );
288         additional.add( "group:another-artifact" );
289 
290         // make sure a null set can be handled
291         Set<Plugin> results = rule.addAdditionalPlugins( null, additional );
292 
293         assertNotNull( results );
294         assertContainsPlugin( "group", "a-artifact", results );
295         assertContainsPlugin( "group", "another-artifact", results );
296 
297     }
298 
299     /**
300      * Test get additional plugins.
301      *
302      * @throws MojoExecutionException the mojo execution exception
303      */
304     public void testGetAdditionalPlugins()
305         throws MojoExecutionException
306     {
307         RequirePluginVersions rule = new RequirePluginVersions();
308 
309         Set<Plugin> plugins = new HashSet<Plugin>();
310         plugins.add( EnforcerTestUtils.newPlugin( "group", "a-artifact", "1.0" ) );
311         plugins.add( EnforcerTestUtils.newPlugin( "group", "foo", null ) );
312         plugins.add( EnforcerTestUtils.newPlugin( "group", "foo2", "" ) );
313 
314         List<String> additional = new ArrayList<String>();
315         additional.add( "group:a-artifact" );
316         additional.add( "group:another-artifact" );
317 
318         Set<Plugin> results = rule.addAdditionalPlugins( plugins, additional );
319 
320         // make sure only one new plugin has been added
321         assertNotNull( results );
322         assertEquals( 4, results.size() );
323         assertContainsPlugin( "group", "a-artifact", results );
324         assertContainsPlugin( "group", "another-artifact", results );
325 
326     }
327 
328     /**
329      * Test remove Unchecked plugins.
330      *
331      * @throws MojoExecutionException the mojo execution exception
332      */
333     public void testGetUncheckedPlugins()
334         throws MojoExecutionException
335     {
336         RequirePluginVersions rule = new RequirePluginVersions();
337 
338         Set <Plugin> plugins = new HashSet<Plugin>();
339         plugins.add( EnforcerTestUtils.newPlugin( "group", "a-artifact", "1.0" ) );
340         plugins.add( EnforcerTestUtils.newPlugin( "group", "foo", null ) );
341         plugins.add( EnforcerTestUtils.newPlugin( "group", "foo2", "" ) );
342 
343         List<String> unchecked = new ArrayList<String>();
344         //intentionally inserting spaces to make sure they are handled correctly.
345         unchecked.add( "group : a-artifact" );
346 
347         Collection<Plugin> results = rule.removeUncheckedPlugins( unchecked, plugins );
348 
349 
350         // make sure only one new plugin has been added
351         assertNotNull( results );
352         assertEquals( 2, results.size() );
353         assertContainsPlugin( "group", "foo", results );
354         assertContainsPlugin( "group", "foo2", results );
355         assertNotContainPlugin( "group", "a-artifact", results );
356 
357     }
358 
359     /**
360      * Test combining values from both lists
361      */
362     public void testCombinePlugins()
363     {
364         RequirePluginVersions rule = new RequirePluginVersions();
365 
366         Set<String> plugins = new HashSet<String>();
367         plugins.add( "group:a-artifact" );
368         plugins.add( "group:foo" );
369         plugins.add( "group:foo2" );
370 
371         Collection<String> results = rule.combineUncheckedPlugins( plugins, "group2:a,group3:b" );
372 
373         // make sure only one new plugin has been added
374         assertNotNull( results );
375         assertEquals( 5, results.size() );
376         assertTrue( results.contains( "group:foo") );
377         assertTrue( results.contains( "group:foo2") );
378         assertTrue( results.contains( "group:a-artifact") );
379         assertTrue( results.contains( "group2:a") );
380         assertTrue( results.contains( "group3:b") );
381     }
382 
383     /**
384      * Test combining with an empty list
385      */
386     public void testCombinePlugins1()
387     {
388         RequirePluginVersions rule = new RequirePluginVersions();
389 
390         Set<String> plugins = new HashSet<String>();
391         Collection<String> results = rule.combineUncheckedPlugins( plugins, "group2:a,group3:b" );
392 
393 
394         // make sure only one new plugin has been added
395         assertNotNull( results );
396         assertEquals( 2, results.size() );
397         assertTrue( results.contains( "group2:a") );
398         assertTrue( results.contains( "group3:b") );
399     }
400 
401     /**
402      * Test combining with a null list
403      */
404     public void testCombinePlugins2()
405     {
406         RequirePluginVersions rule = new RequirePluginVersions();
407 
408         Collection<String> results = rule.combineUncheckedPlugins( null, "group2:a,group3:b" );
409 
410 
411         // make sure only one new plugin has been added
412         assertNotNull( results );
413         assertEquals( 2, results.size() );
414         assertTrue( results.contains( "group2:a") );
415         assertTrue( results.contains( "group3:b") );
416     }
417 
418     /**
419      * Test combining with an empty string
420      */
421     public void testCombinePlugins3()
422     {
423         RequirePluginVersions rule = new RequirePluginVersions();
424 
425         Set<String> plugins = new HashSet<String>();
426         plugins.add( "group:a-artifact" );
427         plugins.add( "group:foo" );
428         plugins.add( "group:foo2" );
429 
430         Collection<String> results = rule.combineUncheckedPlugins( plugins, "" );
431         assertNotNull( results );
432         assertEquals( 3, results.size() );
433         assertTrue( results.contains( "group:foo") );
434         assertTrue( results.contains( "group:foo2") );
435         assertTrue( results.contains( "group:a-artifact") );
436     }
437 
438     /**
439      * Test combining with a null string
440      */
441     public void testCombinePlugins4()
442     {
443         RequirePluginVersions rule = new RequirePluginVersions();
444 
445         Set<String> plugins = new HashSet<String>();
446         plugins.add( "group:a-artifact" );
447         plugins.add( "group:foo" );
448         plugins.add( "group:foo2" );
449 
450         Collection<String> results = rule.combineUncheckedPlugins( plugins, null );
451         assertNotNull( results );
452         assertEquals( 3, results.size() );
453         assertTrue( results.contains( "group:foo") );
454         assertTrue( results.contains( "group:foo2") );
455         assertTrue( results.contains( "group:a-artifact") );
456     }
457 
458     /**
459      * Test combining with an invalid plugin string
460      */
461     public void testCombinePlugins5()
462     {
463         RequirePluginVersions rule = new RequirePluginVersions();
464 
465         Set<String> plugins = new HashSet<String>();
466         plugins.add( "group:a-artifact" );
467         plugins.add( "group:foo" );
468         plugins.add( "group:foo2" );
469 
470         Collection<String> results = rule.combineUncheckedPlugins( plugins, "a" );
471         assertNotNull( results );
472         assertEquals( 4, results.size() );
473         assertTrue( results.contains( "group:foo") );
474         assertTrue( results.contains( "group:foo2") );
475         //this should be here, the checking of a valid plugin string happens in another method.
476         assertTrue( results.contains( "a") );
477     }
478 
479 
480     /**
481      * Assert contains plugin.
482      *
483      * @param group the group
484      * @param artifact the artifact
485      * @param theSet the the set
486      */
487     private void assertContainsPlugin( String group, String artifact, Collection<Plugin> theSet )
488     {
489         Plugin p = new Plugin();
490         p.setGroupId( group );
491         p.setArtifactId( artifact );
492         assertTrue( theSet.contains( p ) );
493     }
494 
495     /**
496      * Assert doesn't contain plugin.
497      *
498      * @param group the group
499      * @param artifact the artifact
500      * @param theSet the the set
501      */
502     private void assertNotContainPlugin( String group, String artifact, Collection<Plugin> theSet )
503     {
504         Plugin p = new Plugin();
505         p.setGroupId( group );
506         p.setArtifactId( artifact );
507         assertFalse( theSet.contains( p ) );
508     }
509 
510     /**
511      * Test id.
512      */
513     public void testId()
514     {
515         RequirePluginVersions rule = new RequirePluginVersions();
516         rule.getCacheId();
517     }
518 }