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