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.List;
26  
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.plugin.LegacySupport;
29  import org.apache.maven.plugin.logging.Log;
30  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
31  import org.apache.maven.settings.Server;
32  import org.apache.maven.settings.Settings;
33  import org.junit.Assert;
34  import org.mockito.ArgumentCaptor;
35  import org.mockito.Mockito;
36  
37  public class TestListClassesMojo extends AbstractDependencyMojoTestCase {
38      private ListClassesMojo mojo;
39  
40      protected void setUp() throws Exception {
41          super.setUp("markers", false);
42          File testPom = new File(getBasedir(), "target/test-classes/unit/get-test/plugin-config.xml");
43  
44          assertTrue(testPom.exists());
45          mojo = (ListClassesMojo) lookupMojo("list-classes", testPom);
46  
47          assertNotNull(mojo);
48  
49          LegacySupport legacySupport = lookup(LegacySupport.class);
50          MavenSession session = newMavenSession(new MavenProjectStub());
51          Settings settings = session.getSettings();
52          Server server = new Server();
53          server.setId("myserver");
54          server.setUsername("foo");
55          server.setPassword("bar");
56          settings.addServer(server);
57          legacySupport.setSession(session);
58  
59          installLocalRepository(legacySupport);
60  
61          setVariableValueToObject(mojo, "session", legacySupport.getSession());
62      }
63  
64      public void testListClassesNotTransitive() throws Exception {
65          Path path = Paths.get("src/test/resources/unit/list-test/testListClassesNotTransitive.txt");
66          List<String> expectedLogArgs = Files.readAllLines(path);
67          ArgumentCaptor<String> infoArgsCaptor = ArgumentCaptor.forClass(String.class);
68  
69          setVariableValueToObject(
70                  mojo,
71                  "remoteRepositories",
72                  "central::default::https://repo.maven.apache.org/maven2,"
73                          + "central::::https://repo.maven.apache.org/maven2," + "https://repo.maven.apache.org/maven2");
74          setVariableValueToObject(mojo, "artifact", "org.apache.commons:commons-lang3:3.6");
75          setVariableValueToObject(mojo, "transitive", Boolean.FALSE);
76  
77          Log log = Mockito.mock(Log.class);
78          mojo.setLog(log);
79  
80          mojo.execute();
81  
82          Mockito.verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
83          Assert.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
84      }
85  
86      public void testListClassesNotTransitiveByGAV() throws Exception {
87          Path path = Paths.get("src/test/resources/unit/list-test/testListClassesNotTransitive.txt");
88          List<String> expectedLogArgs = Files.readAllLines(path);
89          ArgumentCaptor<String> infoArgsCaptor = ArgumentCaptor.forClass(String.class);
90  
91          setVariableValueToObject(
92                  mojo,
93                  "remoteRepositories",
94                  "central::default::https://repo.maven.apache.org/maven2,"
95                          + "central::::https://repo.maven.apache.org/maven2," + "https://repo.maven.apache.org/maven2");
96          setVariableValueToObject(mojo, "groupId", "org.apache.commons");
97          setVariableValueToObject(mojo, "artifactId", "commons-lang3");
98          setVariableValueToObject(mojo, "version", "3.6");
99          setVariableValueToObject(mojo, "transitive", Boolean.FALSE);
100 
101         Log log = Mockito.mock(Log.class);
102         mojo.setLog(log);
103 
104         mojo.execute();
105 
106         Mockito.verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
107         Assert.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
108     }
109 
110     public void testListClassesTransitive() throws Exception {
111         Path path = Paths.get("src/test/resources/unit/list-test/testListClassesTransitive.txt");
112         List<String> expectedLogArgs = Files.readAllLines(path);
113         ArgumentCaptor<String> infoArgsCaptor = ArgumentCaptor.forClass(String.class);
114 
115         setVariableValueToObject(
116                 mojo,
117                 "remoteRepositories",
118                 "central::default::https://repo.maven.apache.org/maven2,"
119                         + "central::::https://repo.maven.apache.org/maven2," + "https://repo.maven.apache.org/maven2");
120         setVariableValueToObject(mojo, "artifact", "org.apache.commons:commons-lang3:3.6");
121         setVariableValueToObject(mojo, "transitive", Boolean.TRUE);
122 
123         Log log = Mockito.mock(Log.class);
124         mojo.setLog(log);
125 
126         mojo.execute();
127 
128         Mockito.verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
129         Assert.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
130     }
131 
132     public void testListClassesTransitiveByGAV() throws Exception {
133         Path path = Paths.get("src/test/resources/unit/list-test/testListClassesTransitive.txt");
134         List<String> expectedLogArgs = Files.readAllLines(path);
135         ArgumentCaptor<String> infoArgsCaptor = ArgumentCaptor.forClass(String.class);
136 
137         setVariableValueToObject(
138                 mojo,
139                 "remoteRepositories",
140                 "central::default::https://repo.maven.apache.org/maven2,"
141                         + "central::::https://repo.maven.apache.org/maven2," + "https://repo.maven.apache.org/maven2");
142         setVariableValueToObject(mojo, "groupId", "org.apache.commons");
143         setVariableValueToObject(mojo, "artifactId", "commons-lang3");
144         setVariableValueToObject(mojo, "version", "3.6");
145         setVariableValueToObject(mojo, "transitive", Boolean.TRUE);
146 
147         Log log = Mockito.mock(Log.class);
148         mojo.setLog(log);
149 
150         mojo.execute();
151 
152         Mockito.verify(log, Mockito.times(expectedLogArgs.size())).info(infoArgsCaptor.capture());
153         Assert.assertEquals(expectedLogArgs, infoArgsCaptor.getAllValues());
154     }
155 }