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.plugins.dependency.resolvers;
20  
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.DefaultArtifact;
26  import org.apache.maven.plugin.logging.Log;
27  import org.apache.maven.project.MavenProject;
28  import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
29  import org.junit.jupiter.api.Test;
30  import org.junit.jupiter.api.extension.ExtendWith;
31  import org.mockito.ArgumentCaptor;
32  import org.mockito.Mock;
33  import org.mockito.junit.jupiter.MockitoExtension;
34  
35  import static java.util.Collections.singleton;
36  import static java.util.Collections.singletonList;
37  import static org.junit.jupiter.api.Assertions.assertEquals;
38  import static org.junit.jupiter.api.Assertions.assertTrue;
39  import static org.mockito.ArgumentMatchers.any;
40  import static org.mockito.Mockito.never;
41  import static org.mockito.Mockito.verify;
42  import static org.mockito.Mockito.when;
43  
44  @ExtendWith(MockitoExtension.class)
45  class ExcludeReactorProjectsArtifactFilterTest {
46  
47      @Mock
48      private Log log;
49  
50      @Mock
51      private MavenProject project;
52  
53      @Test
54      void testFilter() throws ArtifactFilterException {
55          Artifact artifact1 = anArtifact("maven-dependency-plugin-dummy");
56          Artifact artifact2 = anArtifact("maven-dependency-plugin-other-dummy");
57  
58          Set<Artifact> artifacts = new HashSet<>();
59          artifacts.add(artifact1);
60          artifacts.add(artifact2);
61  
62          when(project.getArtifact()).thenReturn(artifact1);
63          when(log.isDebugEnabled()).thenReturn(false);
64  
65          ExcludeReactorProjectsArtifactFilter filter =
66                  new ExcludeReactorProjectsArtifactFilter(singletonList(project), log);
67  
68          Set<Artifact> result = filter.filter(artifacts);
69  
70          assertEquals(1, result.size());
71          verify(log, never()).debug(any(String.class));
72      }
73  
74      @Test
75      void testFilterWithLogging() throws ArtifactFilterException {
76          Artifact artifact = anArtifact("maven-dependency-plugin-dummy");
77  
78          when(project.getArtifact()).thenReturn(artifact);
79          when(log.isDebugEnabled()).thenReturn(true);
80  
81          ExcludeReactorProjectsArtifactFilter filter =
82                  new ExcludeReactorProjectsArtifactFilter(singletonList(project), log);
83  
84          filter.filter(singleton(artifact));
85  
86          ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
87          verify(log).debug(captor.capture());
88          assertTrue(captor.getValue().contains("Skipped artifact"));
89      }
90  
91      private Artifact anArtifact(String artifactId) {
92          return new DefaultArtifact("org.apache.maven.plugins", artifactId, "1.0", null, "jar", "", null);
93      }
94  }