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 java.io.File;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.nio.file.Paths;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.apache.maven.execution.MavenSession;
29  import org.apache.maven.plugin.logging.Log;
30  import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub;
31  import org.apache.maven.plugins.dependency.utils.ResolverUtil;
32  import org.apache.maven.project.MavenProject;
33  import org.eclipse.aether.RepositorySystem;
34  import org.junit.Assert;
35  import org.mockito.ArgumentCaptor;
36  import org.mockito.Mockito;
37  
38  public class TestListClassesMojo extends AbstractDependencyMojoTestCase {
39      private ListClassesMojo mojo;
40  
41      protected void setUp() throws Exception {
42          super.setUp("markers", false);
43  
44          MavenProject project = new DependencyProjectStub();
45          getContainer().addComponent(project, MavenProject.class.getName());
46  
47          MavenSession session = newMavenSession(project);
48  
49          RepositorySystem repositorySystem = lookup(RepositorySystem.class);
50          ResolverUtil resolverUtil = new ResolverUtil(repositorySystem, () -> session);
51          getContainer().addComponent(resolverUtil, ResolverUtil.class.getName());
52  
53          getContainer().addComponent(session, MavenSession.class.getName());
54  
55          File testPom = new File(getBasedir(), "target/test-classes/unit/get-test/plugin-config.xml");
56  
57          assertTrue(testPom.exists());
58          mojo = (ListClassesMojo) lookupMojo("list-classes", testPom);
59  
60          assertNotNull(mojo);
61  
62          installLocalRepository(session.getRepositorySession());
63      }
64  
65      public void testListClassesNotTransitive() throws Exception {
66          Path path = Paths.get("src/test/resources/unit/list-test/testListClassesNotTransitive.txt");
67          List<String> expectedLogArgs = Files.readAllLines(path);
68          ArgumentCaptor<String> infoArgsCaptor = ArgumentCaptor.forClass(String.class);
69  
70          setVariableValueToObject(
71                  mojo,
72                  "remoteRepositories",
73                  Arrays.asList(
74                          "central::default::https://repo.maven.apache.org/maven2",
75                          "central::::https://repo.maven.apache.org/maven2",
76                          "https://repo.maven.apache.org/maven2"));
77          mojo.setArtifact("org.apache.commons:commons-lang3:3.6");
78          setVariableValueToObject(mojo, "transitive", Boolean.FALSE);
79  
80          Log log = Mockito.mock(Log.class);
81          mojo.setLog(log);
82  
83          mojo.execute();
84  
85          Mockito.verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
86          Assert.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
87      }
88  
89      public void testListClassesNotTransitiveByGAV() throws Exception {
90          Path path = Paths.get("src/test/resources/unit/list-test/testListClassesNotTransitive.txt");
91          List<String> expectedLogArgs = Files.readAllLines(path);
92          ArgumentCaptor<String> infoArgsCaptor = ArgumentCaptor.forClass(String.class);
93  
94          setVariableValueToObject(
95                  mojo,
96                  "remoteRepositories",
97                  Arrays.asList(
98                          "central1::default::https://repo.maven.apache.org/maven2",
99                          "central2::::https://repo.maven.apache.org/maven2",
100                         "https://repo.maven.apache.org/maven2"));
101 
102         mojo.setGroupId("org.apache.commons");
103         mojo.setArtifactId("commons-lang3");
104         mojo.setVersion("3.6");
105 
106         setVariableValueToObject(mojo, "transitive", Boolean.FALSE);
107 
108         Log log = Mockito.mock(Log.class);
109         mojo.setLog(log);
110 
111         mojo.execute();
112 
113         Mockito.verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
114         Assert.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
115     }
116 
117     public void testListClassesTransitive() throws Exception {
118         Path path = Paths.get("src/test/resources/unit/list-test/testListClassesTransitive.txt");
119         List<String> expectedLogArgs = Files.readAllLines(path);
120         ArgumentCaptor<String> infoArgsCaptor = ArgumentCaptor.forClass(String.class);
121 
122         setVariableValueToObject(
123                 mojo,
124                 "remoteRepositories",
125                 Arrays.asList(
126                         "central::default::https://repo.maven.apache.org/maven2",
127                         "central::::https://repo.maven.apache.org/maven2",
128                         "https://repo.maven.apache.org/maven2"));
129 
130         mojo.setArtifact("org.apache.commons:commons-lang3:3.6");
131         setVariableValueToObject(mojo, "transitive", Boolean.TRUE);
132 
133         Log log = Mockito.mock(Log.class);
134         mojo.setLog(log);
135 
136         mojo.execute();
137 
138         Mockito.verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
139         Assert.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
140     }
141 
142     public void testListClassesTransitiveByGAV() throws Exception {
143         Path path = Paths.get("src/test/resources/unit/list-test/testListClassesTransitive.txt");
144         List<String> expectedLogArgs = Files.readAllLines(path);
145         ArgumentCaptor<String> infoArgsCaptor = ArgumentCaptor.forClass(String.class);
146 
147         setVariableValueToObject(
148                 mojo,
149                 "remoteRepositories",
150                 Arrays.asList(
151                         "central::default::https://repo.maven.apache.org/maven2",
152                         "central::::https://repo.maven.apache.org/maven2",
153                         "https://repo.maven.apache.org/maven2"));
154         mojo.setGroupId("org.apache.commons");
155         mojo.setArtifactId("commons-lang3");
156         mojo.setVersion("3.6");
157         setVariableValueToObject(mojo, "transitive", Boolean.TRUE);
158 
159         Log log = Mockito.mock(Log.class);
160         mojo.setLog(log);
161 
162         mojo.execute();
163 
164         Mockito.verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
165         Assert.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
166     }
167 }