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.util.Set;
22  
23  import org.apache.maven.api.plugin.testing.InjectMojo;
24  import org.apache.maven.api.plugin.testing.MojoParameter;
25  import org.apache.maven.api.plugin.testing.MojoTest;
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.plugins.dependency.resolvers.CollectDependenciesMojo;
28  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
29  import org.apache.maven.plugins.dependency.utils.DependencySilentLog;
30  import org.apache.maven.plugins.dependency.utils.DependencyStatusSets;
31  import org.apache.maven.project.MavenProject;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  
35  import static org.junit.jupiter.api.Assertions.assertEquals;
36  import static org.junit.jupiter.api.Assertions.assertFalse;
37  import static org.junit.jupiter.api.Assertions.assertNotNull;
38  import static org.junit.jupiter.api.Assertions.assertTrue;
39  
40  @MojoTest
41  class TestCollectMojo {
42  
43      private DependencyArtifactStubFactory stubFactory;
44  
45      @BeforeEach
46      void setUp() throws Exception {
47          stubFactory = new DependencyArtifactStubFactory(null, false);
48      }
49  
50      /**
51       * tests the proper discovery and configuration of the mojo
52       *
53       * @throws Exception if a problem occurs
54       */
55      @Test
56      @InjectMojo(goal = "collect")
57      void testCollectTestEnvironment(CollectDependenciesMojo mojo) throws Exception {
58  
59          assertNotNull(mojo);
60          assertNotNull(mojo.getProject());
61          MavenProject project = mojo.getProject();
62  
63          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
64          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
65          artifacts.addAll(directArtifacts);
66  
67          project.setArtifacts(artifacts);
68          project.setDependencyArtifacts(directArtifacts);
69  
70          mojo.execute();
71          DependencyStatusSets results = mojo.getResults();
72          assertNotNull(results);
73          assertEquals(artifacts.size(), results.getResolvedDependencies().size());
74      }
75  
76      /**
77       * tests the proper discovery and configuration of the mojo
78       *
79       * @throws Exception if a problem occurs
80       */
81      @Test
82      @InjectMojo(goal = "collect")
83      @MojoParameter(name = "excludeTransitive", value = "true")
84      void testCollectTestEnvironmentExcludeTransitive(CollectDependenciesMojo mojo) throws Exception {
85          assertNotNull(mojo);
86          assertNotNull(mojo.getProject());
87          MavenProject project = mojo.getProject();
88  
89          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
90          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
91          artifacts.addAll(directArtifacts);
92  
93          project.setArtifacts(artifacts);
94          project.setDependencyArtifacts(directArtifacts);
95  
96          mojo.execute();
97          DependencyStatusSets results = mojo.getResults();
98          assertNotNull(results);
99          assertEquals(directArtifacts.size(), results.getResolvedDependencies().size());
100     }
101 
102     @Test
103     @InjectMojo(goal = "collect")
104     void testSilent(CollectDependenciesMojo mojo) throws Exception {
105         assertFalse(mojo.getLog() instanceof DependencySilentLog);
106 
107         mojo.setSilent(true);
108         assertTrue(mojo.getLog() instanceof DependencySilentLog);
109 
110         mojo.setSilent(false);
111         assertFalse(mojo.getLog() instanceof DependencySilentLog);
112     } // TODO: Test skipping artifacts.
113 }