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