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.execution.MavenSession;
26  import org.apache.maven.plugins.dependency.resolvers.CollectDependenciesMojo;
27  import org.apache.maven.plugins.dependency.resolvers.ResolveDependenciesMojo;
28  import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub;
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  
33  public class TestCollectMojo extends AbstractDependencyMojoTestCase {
34  
35      @Override
36      protected String getTestDirectoryName() {
37          return "markers";
38      }
39  
40      @Override
41      protected boolean shouldCreateFiles() {
42          return false;
43      }
44  
45      @Override
46      protected void setUp() throws Exception {
47          // required for mojo lookups to work
48          super.setUp();
49  
50          MavenProject project = new DependencyProjectStub();
51          getContainer().addComponent(project, MavenProject.class.getName());
52  
53          MavenSession session = newMavenSession(project);
54          getContainer().addComponent(session, MavenSession.class.getName());
55      }
56  
57      /**
58       * tests the proper discovery and configuration of the mojo
59       *
60       * @throws Exception if a problem occurs
61       */
62      public void testCollectTestEnvironment() throws Exception {
63          File testPom = new File(getBasedir(), "target/test-classes/unit/collect-test/plugin-config.xml");
64          CollectDependenciesMojo mojo = (CollectDependenciesMojo) lookupMojo("collect", testPom);
65  
66          assertNotNull(mojo);
67          assertNotNull(mojo.getProject());
68          MavenProject project = mojo.getProject();
69  
70          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
71          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
72          artifacts.addAll(directArtifacts);
73  
74          project.setArtifacts(artifacts);
75          project.setDependencyArtifacts(directArtifacts);
76  
77          mojo.execute();
78          DependencyStatusSets results = mojo.getResults();
79          assertNotNull(results);
80          assertEquals(artifacts.size(), results.getResolvedDependencies().size());
81      }
82  
83      /**
84       * tests the proper discovery and configuration of the mojo
85       *
86       * @throws Exception if a problem occurs
87       */
88      public void testCollectTestEnvironmentExcludeTransitive() throws Exception {
89          File testPom = new File(getBasedir(), "target/test-classes/unit/collect-test/plugin-config.xml");
90          CollectDependenciesMojo mojo = (CollectDependenciesMojo) lookupMojo("collect", testPom);
91  
92          assertNotNull(mojo);
93          assertNotNull(mojo.getProject());
94          MavenProject project = mojo.getProject();
95  
96          Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
97          Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
98          artifacts.addAll(directArtifacts);
99  
100         project.setArtifacts(artifacts);
101         project.setDependencyArtifacts(directArtifacts);
102 
103         setVariableValueToObject(mojo, "excludeTransitive", Boolean.TRUE);
104 
105         mojo.execute();
106         DependencyStatusSets results = mojo.getResults();
107         assertNotNull(results);
108         assertEquals(directArtifacts.size(), results.getResolvedDependencies().size());
109     }
110 
111     public void testSilent() throws Exception {
112         File testPom = new File(getBasedir(), "target/test-classes/unit/resolve-test/plugin-config.xml");
113         ResolveDependenciesMojo mojo = (ResolveDependenciesMojo) lookupMojo("resolve", testPom);
114 
115         assertFalse(mojo.getLog() instanceof DependencySilentLog);
116 
117         mojo.setSilent(true);
118         assertTrue(mojo.getLog() instanceof DependencySilentLog);
119 
120         mojo.setSilent(false);
121         assertFalse(mojo.getLog() instanceof DependencySilentLog);
122     } // TODO: Test skipping artifacts.
123 }