View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.enforcer.rules.utils;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.DefaultArtifact;
26  import org.apache.maven.artifact.handler.ArtifactHandler;
27  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
28  import org.apache.maven.artifact.versioning.ArtifactVersion;
29  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
30  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
31  import org.apache.maven.artifact.versioning.VersionRange;
32  import org.apache.maven.enforcer.rules.utils.ArtifactMatcher.Pattern;
33  import org.apache.maven.enforcer.rules.version.RequireJavaVersion;
34  import org.junit.jupiter.api.Test;
35  
36  import static org.junit.jupiter.api.Assertions.assertEquals;
37  import static org.junit.jupiter.api.Assertions.assertFalse;
38  import static org.junit.jupiter.api.Assertions.assertTrue;
39  import static org.junit.jupiter.api.Assertions.fail;
40  
41  class TestArtifactMatcher {
42  
43      Collection<String> patterns = new ArrayList<>();
44  
45      Collection<String> ignorePatterns = new ArrayList<>();
46  
47      @Test
48      void testPatternInvalidInput() {
49          try {
50              new Pattern(null);
51              fail("NullPointerException expected.");
52          } catch (NullPointerException ignored) {
53          }
54  
55          try {
56              new Pattern("a:b:c:d:e:f:g");
57              fail("IllegalArgumentException expected.");
58          } catch (IllegalArgumentException ignored) {
59          }
60  
61          try {
62              new Pattern("a::");
63              fail("IllegalArgumentException expected.");
64          } catch (IllegalArgumentException ignored) {
65          }
66  
67          try {
68              Pattern p = new Pattern("*");
69              p.match((Artifact) null);
70              fail("NullPointerException expected.");
71          } catch (NullPointerException ignored) {
72          }
73      }
74  
75      @Test
76      void testPattern() throws InvalidVersionSpecificationException {
77          executePatternMatch(
78                  "groupId:artifactId:1.0:jar:compile", "groupId", "artifactId", "1.0", "compile", "jar", true);
79  
80          executePatternMatch("groupId:artifactId:1.0:jar:compile", "groupId", "artifactId", "1.0", "", "", true);
81  
82          executePatternMatch("groupId:artifactId:1.0", "groupId", "artifactId", "1.0", "", "", true);
83  
84          executePatternMatch("groupId:artifactId:1.0", "groupId", "artifactId", "1.1", "", "", true);
85  
86          executePatternMatch("groupId:artifactId:[1.0]", "groupId", "artifactId", "1.1", "", "", false);
87  
88          executePatternMatch("groupId:*:1.0", "groupId", "artifactId", "1.0", "test", "", true);
89  
90          executePatternMatch("*:*:1.0", "groupId", "artifactId", "1.0", "", "", true);
91  
92          executePatternMatch("*:artifactId:*", "groupId", "artifactId", "1.0", "", "", true);
93  
94          executePatternMatch("*", "groupId", "artifactId", "1.0", "", "", true);
95  
96          // MENFORCER-74/75
97          executePatternMatch("*:*:*:jar:compile:tests", "groupId", "artifactId", "1.0", "", "", "tests", true);
98  
99          // MENFORCER-83
100         executePatternMatch("*upId", "groupId", "artifactId", "1.0", "", "", true);
101 
102         executePatternMatch("gr*pId:?rt?f?ct?d:1.0", "groupId", "artifactId", "1.0", "", "", true);
103 
104         executePatternMatch("org.apache.*:maven-*:*", "org.apache.maven", "maven-core", "3.0", "", "", true);
105     }
106 
107     @Test
108     void testMatch() {
109         patterns.add("groupId:artifactId:1.0");
110         patterns.add("*:anotherArtifact");
111 
112         ignorePatterns.add("badGroup:*:*:test");
113         ignorePatterns.add("*:anotherArtifact:1.1");
114 
115         ArtifactMatcher matcher = new ArtifactMatcher(patterns, ignorePatterns);
116 
117         executeMatch(matcher, "groupId", "artifactId", "1.0", "", "", true);
118 
119         executeMatch(matcher, "groupId", "anotherArtifact", "1.0", "", "", true);
120 
121         executeMatch(matcher, "badGroup", "artifactId", "1.0", "", "test", false);
122 
123         executeMatch(matcher, "badGroup", "anotherArtifact", "1.0", "", "", true);
124 
125         executeMatch(matcher, "groupId", "anotherArtifact", "1.1", "", "", false);
126     }
127 
128     private void executePatternMatch(
129             final String pattern,
130             final String groupId,
131             final String artifactId,
132             final String versionRange,
133             final String scope,
134             final String type,
135             boolean expectedResult)
136             throws InvalidVersionSpecificationException {
137         executePatternMatch(pattern, groupId, artifactId, versionRange, scope, type, "", expectedResult);
138     }
139 
140     private void executePatternMatch(
141             final String pattern,
142             final String groupId,
143             final String artifactId,
144             final String versionRange,
145             final String scope,
146             final String type,
147             final String classifier,
148             boolean expectedResult) {
149         assertEquals(
150                 expectedResult,
151                 new ArtifactMatcher.Pattern(pattern)
152                         .match(createMockArtifact(groupId, artifactId, versionRange, scope, type, classifier)));
153     }
154 
155     private void executeMatch(
156             final ArtifactMatcher matcher,
157             final String groupId,
158             final String artifactId,
159             final String versionRange,
160             final String scope,
161             final String type,
162             final boolean expectedResult) {
163         assertEquals(
164                 expectedResult, matcher.match(createMockArtifact(groupId, artifactId, versionRange, scope, type, "")));
165     }
166 
167     private static Artifact createMockArtifact(
168             final String groupId,
169             final String artifactId,
170             final String versionRange,
171             final String scope,
172             final String type,
173             final String classifier) {
174         ArtifactHandler artifactHandler = new DefaultArtifactHandler();
175 
176         VersionRange version = VersionRange.createFromVersion(versionRange);
177         return new DefaultArtifact(groupId, artifactId, version, scope, type, classifier, artifactHandler);
178     }
179 
180     /**
181      * Test contains version.
182      *
183      * @throws InvalidVersionSpecificationException the invalid version specification exception
184      */
185     @Test
186     void testContainsVersion() throws InvalidVersionSpecificationException {
187         ArtifactVersion version = new DefaultArtifactVersion("2.0.5");
188         // test ranges
189         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[2.0.5,)"), version));
190         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[2.0.4,)"), version));
191         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[2.0.4,2.0.5]"), version));
192         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[2.0.4,2.0.6]"), version));
193         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[2.0.4,2.0.6)"), version));
194         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[2.0,)"), version));
195         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[2.0.0,)"), version));
196         // not matching versions
197         assertFalse(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[2.0.4,2.0.5)"), version));
198         assertFalse(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[2.0.6,)"), version));
199         assertFalse(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("(2.0.5,)"), version));
200 
201         // test singular versions -> 2.0.5 == [2.0.5,) or x >= 2.0.5
202         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("2.0"), version));
203         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("2.0.4"), version));
204         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("2.0.5"), version));
205 
206         assertFalse(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("2.0.6"), version));
207 
208         version = new DefaultArtifactVersion("1.5.0-7");
209         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[1.5.0,)"), version));
210         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[1.5,1.6)"), version));
211 
212         version = new DefaultArtifactVersion(RequireJavaVersion.normalizeJDKVersion("1.5.0-07"));
213         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[1.5.0,)"), version));
214         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[1.5,1.6)"), version));
215 
216         // MENFORCER-50
217         version = new DefaultArtifactVersion("2.1.0-M1-RC12");
218         assertTrue(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[2.1.0-M1-RC12,)"), version));
219         assertFalse(ArtifactMatcher.containsVersion(VersionRange.createFromVersionSpec("[2.1.0-M1,)"), version));
220     }
221 }