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.IOException;
22  import java.util.HashSet;
23  import java.util.Set;
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.versioning.VersionRange;
26  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
27  import org.apache.maven.plugins.dependency.utils.DependencyStatusSets;
28  
29  public class ResolveDependenciesMojoTest extends AbstractDependencyMojoTestCase {
30      protected void setUp() throws Exception {
31          // required for mojo lookups to work
32          super.setUp("dss", true);
33      }
34  
35      public void testDependencyStatusLog() throws IOException {
36          Set<Artifact> artifacts = this.stubFactory.getMixedArtifacts();
37          doTestDependencyStatusLog(artifacts);
38      }
39  
40      public void testDependencyStatusLogNullFiles() throws IOException {
41          this.stubFactory.setCreateFiles(false);
42          Set<Artifact> artifacts = this.stubFactory.getMixedArtifacts();
43          doTestDependencyStatusLog(artifacts);
44      }
45  
46      public void testDependencyStatusEmptySet() {
47          doTestDependencyStatusLog(new HashSet<Artifact>());
48      }
49  
50      public void testOptionalDependencyFormatting() throws IOException {
51          Set<Artifact> set = new HashSet<>();
52          Artifact artifact =
53                  stubFactory.createArtifact("g", "a", VersionRange.createFromVersion("1.0"), "test", "jar", null, true);
54          set.add(artifact);
55          doTestDependencyStatusLog(set);
56          ResolveDependenciesMojo mojo = newMojo(new DependencyStatusSets());
57          mojo.results.setResolvedDependencies(set);
58          String output = mojo.getOutput(false, true, false);
59          assertTrue(output.contains("g:a:jar:1.0:test (optional)" + System.lineSeparator()));
60      }
61  
62      public void doTestDependencyStatusLog(Set<Artifact> artifacts) {
63          // TODO: implement logger to check correct output
64          // this test is just looking for unexpected exceptions.
65  
66          ResolveDependenciesMojo mojo = newMojo(new DependencyStatusSets());
67          mojo.getOutput(false, true, false);
68          mojo.getOutput(true, true, false);
69  
70          mojo = newMojo(new DependencyStatusSets(artifacts, null, null));
71          mojo.getOutput(false, true, false);
72          mojo.getOutput(true, true, false);
73  
74          mojo = newMojo(new DependencyStatusSets(null, artifacts, null));
75          mojo.getOutput(false, true, false);
76          mojo.getOutput(true, true, false);
77  
78          mojo = newMojo(new DependencyStatusSets(null, null, artifacts));
79          mojo.getOutput(false, true, false);
80          mojo.getOutput(true, true, false);
81  
82          mojo = newMojo(new DependencyStatusSets(artifacts, artifacts, null));
83          mojo.getOutput(false, true, false);
84          mojo.getOutput(true, true, false);
85  
86          mojo = newMojo(new DependencyStatusSets(null, artifacts, artifacts));
87          mojo.getOutput(false, true, false);
88          mojo.getOutput(true, true, false);
89  
90          mojo = newMojo(new DependencyStatusSets(artifacts, null, artifacts));
91          mojo.getOutput(false, true, false);
92          mojo.getOutput(true, true, false);
93  
94          mojo = newMojo(new DependencyStatusSets(artifacts, artifacts, artifacts));
95          mojo.getOutput(false, true, false);
96          mojo.getOutput(true, true, false);
97          mojo.getOutput(false, false, false);
98          mojo.getOutput(true, false, false);
99      }
100 
101     private ResolveDependenciesMojo newMojo(final DependencyStatusSets dss) {
102         ResolveDependenciesMojo mojo = new ResolveDependenciesMojo();
103         mojo.results = dss;
104         return mojo;
105     }
106 }