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  
24  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
25  import org.junit.Before;
26  import org.junit.Test;
27  import org.junit.experimental.runners.Enclosed;
28  import org.junit.runner.RunWith;
29  
30  /**
31   * The Class TestBannedDependencies.
32   * 
33   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
34   */
35  @RunWith( Enclosed.class )
36  public class TestBannedDependencies
37  {
38  
39      public static class ExcludesDoNotUseTransitiveDependencies
40      {
41          private BannedDependenciesTestSetup setup;
42  
43          @Before
44          public void beforeMethod()
45              throws IOException
46          {
47              this.setup = new BannedDependenciesTestSetup();
48              this.setup.setSearchTransitive( false );
49          }
50  
51          private void addExcludeAndRunRule( String toAdd )
52              throws EnforcerRuleException
53          {
54              this.setup.addExcludeAndRunRule( toAdd );
55          }
56  
57          @Test
58          public void testGroupIdArtifactIdVersion()
59              throws Exception
60          {
61              addExcludeAndRunRule( "testGroupId:release:1.0" );
62          }
63  
64          @Test
65          public void testGroupIdArtifactId()
66              throws Exception
67          {
68              addExcludeAndRunRule( "testGroupId:release" );
69          }
70  
71          @Test
72          public void testGroupId()
73              throws Exception
74          {
75              addExcludeAndRunRule( "testGroupId" );
76          }
77  
78      }
79  
80      public static class ExcludesUsingTransitiveDependencies
81      {
82  
83          private BannedDependenciesTestSetup setup;
84  
85          @Before
86          public void beforeMethod()
87              throws IOException
88          {
89              this.setup = new BannedDependenciesTestSetup();
90              this.setup.setSearchTransitive( true );
91          }
92  
93          private void addExcludeAndRunRule( String toAdd )
94              throws EnforcerRuleException
95          {
96              this.setup.addExcludeAndRunRule( toAdd );
97          }
98  
99          @Test( expected = EnforcerRuleException.class )
100         public void testGroupIdArtifactIdVersion()
101             throws Exception
102         {
103             addExcludeAndRunRule( "testGroupId:release:1.0" );
104         }
105 
106         @Test( expected = EnforcerRuleException.class )
107         public void testGroupIdArtifactId()
108             throws Exception
109         {
110             addExcludeAndRunRule( "testGroupId:release" );
111         }
112 
113         @Test( expected = EnforcerRuleException.class )
114         public void testGroupId()
115             throws Exception
116         {
117             addExcludeAndRunRule( "testGroupId" );
118         }
119 
120         @Test( expected = EnforcerRuleException.class )
121         public void testSpaceTrimmingGroupIdArtifactIdVersion()
122             throws Exception
123         {
124             addExcludeAndRunRule( "  testGroupId  :  release   :   1.0    " );
125         }
126 
127         @Test( expected = EnforcerRuleException.class )
128         public void groupIdArtifactIdVersionType()
129             throws Exception
130         {
131             addExcludeAndRunRule( "g:a:1.0:war" );
132         }
133 
134         @Test( expected = EnforcerRuleException.class )
135         public void groupIdArtifactIdVersionTypeScope()
136             throws Exception
137         {
138             addExcludeAndRunRule( "g:a:1.0:war:compile" );
139         }
140 
141         // @Test(expected = EnforcerRuleException.class)
142         // public void groupIdArtifactIdVersionTypeScopeClassifier() throws Exception {
143         // addExcludeAndRunRule("g:compile:1.0:jar:compile:one");
144         // }
145         //
146     }
147 
148     public static class WildcardExcludesUsingTransitiveDependencies
149     {
150 
151         private BannedDependenciesTestSetup setup;
152 
153         @Before
154         public void beforeMethod()
155             throws IOException
156         {
157             this.setup = new BannedDependenciesTestSetup();
158             this.setup.setSearchTransitive( true );
159         }
160 
161         private void addExcludeAndRunRule( String toAdd )
162             throws EnforcerRuleException
163         {
164             this.setup.addExcludeAndRunRule( toAdd );
165         }
166 
167         @Test
168         public void testWildcardForGroupIdArtifactIdVersion()
169             throws Exception
170         {
171             addExcludeAndRunRule( "*:release:1.2" );
172         }
173 
174         @Test( expected = EnforcerRuleException.class )
175         public void testWildCardForGroupIdArtifactId()
176             throws Exception
177         {
178             addExcludeAndRunRule( "*:release" );
179         }
180 
181         @Test( expected = EnforcerRuleException.class )
182         public void testWildcardForGroupIdWildcardForArtifactIdVersion()
183             throws Exception
184         {
185             addExcludeAndRunRule( "*:*:1.0" );
186         }
187 
188         @Test( expected = EnforcerRuleException.class )
189         public void testWildcardForGroupIdArtifactIdWildcardForVersion()
190             throws Exception
191         {
192             addExcludeAndRunRule( "*:release:*" );
193         }
194 
195     }
196 
197     public static class PartialWildcardExcludesUsingTransitiveDependencies
198     {
199 
200         private BannedDependenciesTestSetup setup;
201 
202         @Before
203         public void beforeMethod()
204             throws IOException
205         {
206             this.setup = new BannedDependenciesTestSetup();
207             this.setup.setSearchTransitive( true );
208         }
209 
210         private void addExcludeAndRunRule( String toAdd )
211             throws EnforcerRuleException
212         {
213             this.setup.addExcludeAndRunRule( toAdd );
214         }
215 
216         @Test( expected = EnforcerRuleException.class )
217         public void groupIdArtifactIdWithWildcard()
218             throws EnforcerRuleException
219         {
220             addExcludeAndRunRule( "testGroupId:re*" );
221         }
222 
223         @Test( expected = EnforcerRuleException.class )
224         public void groupIdArtifactIdVersionTypeWildcardScope()
225             throws EnforcerRuleException
226         {
227             addExcludeAndRunRule( "g:a:1.0:war:co*" );
228         }
229 
230         @Test( expected = EnforcerRuleException.class )
231         public void groupIdArtifactIdVersionWildcardTypeScope()
232             throws EnforcerRuleException
233         {
234             addExcludeAndRunRule( "g:a:1.0:w*:compile" );
235         }
236     }
237 
238     public static class IllegalFormatsTests
239     {
240         private BannedDependenciesTestSetup setup;
241 
242         @Before
243         public void beforeMethod()
244             throws IOException
245         {
246             this.setup = new BannedDependenciesTestSetup();
247             this.setup.setSearchTransitive( true );
248         }
249 
250         private void addExcludeAndRunRule( String toAdd )
251             throws EnforcerRuleException
252         {
253             this.setup.addExcludeAndRunRule( toAdd );
254         }
255 
256         @Test( expected = IllegalArgumentException.class )
257         public void onlyThreeColonsWithoutAnythingElse()
258             throws EnforcerRuleException
259         {
260             addExcludeAndRunRule( ":::" );
261         }
262 
263         @Test( expected = IllegalArgumentException.class )
264         public void onlySevenColonsWithoutAnythingElse()
265             throws EnforcerRuleException
266         {
267             addExcludeAndRunRule( ":::::::" );
268         }
269 
270     }
271 
272     public static class IncludesExcludesNoTransitive
273     {
274         private BannedDependenciesTestSetup setup;
275 
276         @Before
277         public void beforeMethod()
278             throws IOException
279         {
280             this.setup = new BannedDependenciesTestSetup();
281             this.setup.setSearchTransitive( false );
282         }
283 
284         private void addIncludeExcludeAndRunRule( String incAdd, String excAdd )
285             throws EnforcerRuleException
286         {
287             this.setup.addIncludeExcludeAndRunRule( incAdd, excAdd );
288         }
289 
290         @Test
291         public void includeEverythingAndExcludeEverythign()
292             throws EnforcerRuleException
293         {
294             addIncludeExcludeAndRunRule( "*", "*" );
295         }
296 
297         @Test
298         public void includeEverythingAndExcludeEveryGroupIdAndScopeRuntime()
299             throws EnforcerRuleException
300         {
301             addIncludeExcludeAndRunRule( "*", "*:runtime" );
302         }
303 
304         @Test( expected = EnforcerRuleException.class )
305         public void includeEverythingAndExcludeEveryGroupIdAndScopeRuntimeYYYY()
306             throws EnforcerRuleException
307         {
308             addIncludeExcludeAndRunRule( "*:test", "*:runtime" );
309         }
310     }
311 
312 }