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