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.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Set;
26  
27  import junit.framework.TestCase;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
31  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
32  import org.apache.maven.plugin.testing.ArtifactStubFactory;
33  import org.apache.maven.project.MavenProject;
34  
35  // TODO: Auto-generated Javadoc
36  /**
37   * The Class TestBannedDependencies.
38   *
39   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
40   */
41  public class TestBannedDependencies
42      extends TestCase
43  {
44  
45      /**
46       * Test rule.
47       *
48       * @throws IOException Signals that an I/O exception has occurred.
49       */
50      public void testRule()
51          throws IOException
52      {
53          ArtifactStubFactory factory = new ArtifactStubFactory();
54          MockProject project = new MockProject();
55          EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
56          project.setArtifacts( factory.getMixedArtifacts() );
57          project.setDependencyArtifacts( factory.getScopedArtifacts() );
58          
59          BannedDependencies rule = newBannedDependenciesRule();
60  
61          List<String> excludes = new ArrayList<String>();
62          rule.setSearchTransitive( false );
63  
64          // test whole name
65          excludes.add( "testGroupId:release:1.0" );
66          rule.setExcludes( excludes );
67  
68          execute( rule, helper, false );
69  
70          // test group:artifact
71          excludes.clear();
72          excludes.add( "testGroupId:release" );
73          execute( rule, helper, false );
74  
75          // test group
76          excludes.clear();
77          excludes.add( "testGroupId" );
78          execute( rule, helper, false );
79  
80          // now check one that should be found in direct
81          // dependencies
82          excludes.clear();
83          excludes.add( "g:compile:1.0" );
84          execute( rule, helper, true );
85          rule.setSearchTransitive( true );
86  
87          // whole name
88          excludes.clear();
89          excludes.add( "testGroupId:release:1.0" );
90          execute( rule, helper, true );
91  
92          // group:artifact
93          excludes.clear();
94          excludes.add( "testGroupId:release" );
95          execute( rule, helper, true );
96  
97          // group
98          excludes.clear();
99          excludes.add( "testGroupId" );
100         execute( rule, helper, true );
101 
102         // now check wildcards
103         excludes.clear();
104         excludes.add( "*:release" );
105         execute( rule, helper, true );
106 
107         // now check wildcards
108         excludes.clear();
109         excludes.add( "*:*:1.0" );
110         execute( rule, helper, true );
111 
112         // now check wildcards
113         excludes.clear();
114         excludes.add( "*:release:*" );
115         execute( rule, helper, true );
116 
117         // now check wildcards
118         excludes.clear();
119         excludes.add( "*:release:1.2" );
120         execute( rule, helper, false );
121 
122         // now check multiple excludes
123         excludes.add( "*:release:*" );
124         execute( rule, helper, true );
125 
126         // now check space trimming
127         excludes.clear();
128         excludes.add( "  testGroupId  :  release   :   1.0    " );
129         execute( rule, helper, true );
130 
131         // now check weirdness
132         excludes.clear();
133         excludes.add( ":::" ); // null entry, won't match anything
134         execute( rule, helper, false );
135     }
136 
137     /**
138      * Test includes.
139      *
140      * @throws IOException Signals that an I/O exception has occurred.
141      */
142     public void testIncludes()
143         throws IOException
144     {
145         ArtifactStubFactory factory = new ArtifactStubFactory();
146         MockProject project = new MockProject();
147         EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( project );
148         project.setArtifacts( factory.getMixedArtifacts() );
149         project.setDependencyArtifacts( factory.getScopedArtifacts() );
150         BannedDependencies rule = newBannedDependenciesRule();
151 
152         List<String> excludes = new ArrayList<String>();
153         List<String> includes = new ArrayList<String>();
154 
155         rule.setSearchTransitive( false );
156 
157         excludes.add( "*" );
158         includes.add( "*" );
159 
160         rule.setExcludes( excludes );
161         rule.setIncludes( includes );
162 
163         execute( rule, helper, false );
164 
165         excludes.clear();
166         excludes.add( "*:runtime" );
167         rule.setExcludes( excludes );
168 
169         execute( rule, helper, false );
170 
171         includes.clear();
172         includes.add( "*:test" );
173         rule.setIncludes( includes );
174         execute( rule, helper, true );
175     }
176 
177     private BannedDependencies newBannedDependenciesRule()
178     {
179         BannedDependencies rule = new BannedDependencies()
180         {
181             @Override
182             protected Set<Artifact> getDependenciesToCheck( MavenProject project )
183             {
184                 // the integration with dependencyGraphTree is verified with the integration tests
185                 // for unit-testing 
186                 return isSearchTransitive() ? project.getArtifacts() : project.getDependencyArtifacts();
187             }
188         };
189         return rule;
190     }
191 
192     /**
193      * Simpler wrapper to execute and deal with the expected result.
194      *
195      * @param rule the rule
196      * @param helper the helper
197      * @param shouldFail the should fail
198      */
199     private void execute( BannedDependencies rule, EnforcerRuleHelper helper, boolean shouldFail )
200     {
201         try
202         {
203             rule.setMessage( null );
204             rule.execute( helper );
205             if ( shouldFail )
206             {
207                 fail( "Exception expected." );
208             }
209         }
210         catch ( EnforcerRuleException e )
211         {
212             if ( !shouldFail )
213             {
214                 fail( "No Exception expected:" + e.getLocalizedMessage() );
215             }
216             // helper.getLog().debug(e.getMessage());
217         }
218     }
219 }