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