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.util.Set;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.plugin.testing.SilentLog;
26  import org.apache.maven.plugins.dependency.resolvers.CollectDependenciesMojo;
27  import org.apache.maven.plugins.dependency.utils.DependencyStatusSets;
28  import org.apache.maven.project.MavenProject;
29  
30  public class TestCollectMojo extends AbstractDependencyMojoTestCase {
31  
32      protected void setUp() throws Exception {
33          // required for mojo lookups to work
34          super.setUp("markers", false);
35      }
36  
37      /**
38       * tests the proper discovery and configuration of the mojo
39       *
40       * @throws Exception if a problem occurs
41       */
42      public void testCollectTestEnvironment() throws Exception {
43          File testPom = new File(getBasedir(), "target/test-classes/unit/collect-test/plugin-config.xml");
44          CollectDependenciesMojo mojo = (CollectDependenciesMojo) lookupMojo("collect", testPom);
45  
46          assertNotNull(mojo);
47          assertNotNull(mojo.getProject());
48          MavenProject project = mojo.getProject();
49  
50          mojo.setSilent(true);
51          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
52          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
53          artifacts.addAll(directArtifacts);
54  
55          project.setArtifacts(artifacts);
56          project.setDependencyArtifacts(directArtifacts);
57  
58          mojo.execute();
59          DependencyStatusSets results = mojo.getResults();
60          assertNotNull(results);
61          assertEquals(artifacts.size(), results.getResolvedDependencies().size());
62      }
63  
64      /**
65       * tests the proper discovery and configuration of the mojo
66       *
67       * @throws Exception if a problem occurs
68       */
69      public void testCollectTestEnvironment_excludeTransitive() throws Exception {
70          File testPom = new File(getBasedir(), "target/test-classes/unit/collect-test/plugin-config.xml");
71          CollectDependenciesMojo mojo = (CollectDependenciesMojo) lookupMojo("collect", testPom);
72  
73          assertNotNull(mojo);
74          assertNotNull(mojo.getProject());
75          MavenProject project = mojo.getProject();
76  
77          mojo.setSilent(true);
78          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
79          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
80          artifacts.addAll(directArtifacts);
81  
82          project.setArtifacts(artifacts);
83          project.setDependencyArtifacts(directArtifacts);
84  
85          setVariableValueToObject(mojo, "excludeTransitive", Boolean.TRUE);
86  
87          mojo.execute();
88          DependencyStatusSets results = mojo.getResults();
89          assertNotNull(results);
90          assertEquals(directArtifacts.size(), results.getResolvedDependencies().size());
91      }
92  
93      public void testSilent() throws Exception {
94          File testPom = new File(getBasedir(), "target/test-classes/unit/collect-test/plugin-config.xml");
95          CollectDependenciesMojo mojo = (CollectDependenciesMojo) lookupMojo("collect", testPom);
96          mojo.setSilent(false);
97  
98          assertFalse(mojo.getLog() instanceof SilentLog);
99      } // TODO: Test skipping artifacts.
100 }