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;
20  
21  import javax.inject.Inject;
22  
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.nio.file.Paths;
26  import java.util.List;
27  
28  import org.apache.maven.api.plugin.testing.Basedir;
29  import org.apache.maven.api.plugin.testing.InjectMojo;
30  import org.apache.maven.api.plugin.testing.MojoParameter;
31  import org.apache.maven.api.plugin.testing.MojoTest;
32  import org.apache.maven.execution.MavenSession;
33  import org.apache.maven.internal.aether.DefaultRepositorySystemSessionFactory;
34  import org.apache.maven.plugin.logging.Log;
35  import org.junit.jupiter.api.Assertions;
36  import org.junit.jupiter.api.Test;
37  import org.junit.jupiter.api.extension.ExtendWith;
38  import org.mockito.ArgumentCaptor;
39  import org.mockito.Mockito;
40  import org.mockito.junit.jupiter.MockitoExtension;
41  
42  import static org.apache.maven.api.plugin.testing.MojoExtension.getBasedir;
43  import static org.mockito.Mockito.verify;
44  
45  @ExtendWith(MockitoExtension.class)
46  @MojoTest(realRepositorySession = true)
47  class TestListClassesMojo {
48  
49      @Inject
50      private MavenSession mavenSession;
51  
52      @Inject
53      private DefaultRepositorySystemSessionFactory repoSessionFactory;
54  
55      @Inject
56      private Log log;
57  
58      @Test
59      @InjectMojo(goal = "list-classes")
60      @MojoParameter(
61              name = "remoteRepositories",
62              value = "central::default::https://repo.maven.apache.org/maven2,"
63                      + "central::::https://repo.maven.apache.org/maven2,"
64                      + "https://repo.maven.apache.org/maven2")
65      @MojoParameter(name = "transitive", value = "false")
66      @MojoParameter(name = "artifact", value = "org.apache.commons:commons-lang3:3.6")
67      @Basedir("/unit/list-test")
68      void testListClassesNotTransitive(ListClassesMojo mojo) throws Exception {
69          Path path = Paths.get(getBasedir(), "testListClassesNotTransitive.txt");
70          List<String> expectedLogArgs = Files.readAllLines(path);
71          ArgumentCaptor<String> infoArgsCaptor = ArgumentCaptor.forClass(String.class);
72  
73          mojo.execute();
74  
75          verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
76          Assertions.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
77      }
78  
79      @Test
80      @InjectMojo(goal = "list-classes")
81      @MojoParameter(
82              name = "remoteRepositories",
83              value = "central::default::https://repo.maven.apache.org/maven2,"
84                      + "central::::https://repo.maven.apache.org/maven2,"
85                      + "https://repo.maven.apache.org/maven2")
86      @MojoParameter(name = "transitive", value = "false")
87      @MojoParameter(name = "groupId", value = "org.apache.commons")
88      @MojoParameter(name = "artifactId", value = "commons-lang3")
89      @MojoParameter(name = "version", value = "3.6")
90      @Basedir("/unit/list-test")
91      void testListClassesNotTransitiveByGAV(ListClassesMojo mojo) throws Exception {
92          Path path = Paths.get(getBasedir(), "testListClassesNotTransitive.txt");
93          List<String> expectedLogArgs = Files.readAllLines(path);
94          ArgumentCaptor<String> infoArgsCaptor = ArgumentCaptor.forClass(String.class);
95  
96          mojo.execute();
97  
98          verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
99          Assertions.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
100     }
101 
102     @Test
103     @InjectMojo(goal = "list-classes")
104     @MojoParameter(
105             name = "remoteRepositories",
106             value = "central::default::https://repo.maven.apache.org/maven2,"
107                     + "central::::https://repo.maven.apache.org/maven2,"
108                     + "https://repo.maven.apache.org/maven2")
109     @MojoParameter(name = "transitive", value = "true")
110     @MojoParameter(name = "artifact", value = "org.apache.commons:commons-lang3:3.6")
111     @Basedir("/unit/list-test")
112     void testListClassesTransitive(ListClassesMojo mojo) throws Exception {
113         Path path = Paths.get(getBasedir(), "testListClassesTransitive.txt");
114         List<String> expectedLogArgs = Files.readAllLines(path);
115         ArgumentCaptor<String> infoArgsCaptor = ArgumentCaptor.forClass(String.class);
116 
117         mojo.execute();
118 
119         verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
120         Assertions.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
121     }
122 
123     @Test
124     @InjectMojo(goal = "list-classes")
125     @MojoParameter(
126             name = "remoteRepositories",
127             value = "central::default::https://repo.maven.apache.org/maven2,"
128                     + "central::::https://repo.maven.apache.org/maven2,"
129                     + "https://repo.maven.apache.org/maven2")
130     @MojoParameter(name = "transitive", value = "true")
131     @MojoParameter(name = "groupId", value = "org.apache.commons")
132     @MojoParameter(name = "artifactId", value = "commons-lang3")
133     @MojoParameter(name = "version", value = "3.6")
134     @Basedir("/unit/list-test")
135     void testListClassesTransitiveByGAV(ListClassesMojo mojo) throws Exception {
136         Path path = Paths.get(getBasedir(), "testListClassesTransitive.txt");
137         List<String> expectedLogArgs = Files.readAllLines(path);
138         ArgumentCaptor<String> infoArgsCaptor = ArgumentCaptor.forClass(String.class);
139 
140         mojo.execute();
141 
142         verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
143         Assertions.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
144     }
145 }