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  
23  import org.apache.maven.execution.MavenSession;
24  import org.apache.maven.model.Plugin;
25  import org.apache.maven.plugin.Mojo;
26  import org.apache.maven.plugin.MojoExecution;
27  import org.apache.maven.plugin.descriptor.MojoDescriptor;
28  import org.apache.maven.plugin.descriptor.PluginDescriptor;
29  import org.apache.maven.plugin.logging.Log;
30  import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub;
31  import org.apache.maven.project.MavenProject;
32  import org.mockito.ArgumentCaptor;
33  
34  import static org.mockito.Mockito.atLeastOnce;
35  import static org.mockito.Mockito.mock;
36  import static org.mockito.Mockito.verify;
37  
38  public class TestSkip extends AbstractDependencyMojoTestCase {
39  
40      @Override
41      protected void setUp() throws Exception {
42          super.setUp();
43          MavenProject project = new DependencyProjectStub();
44          getContainer().addComponent(project, MavenProject.class.getName());
45  
46          MavenSession session = newMavenSession(project);
47          getContainer().addComponent(session, MavenSession.class.getName());
48      }
49  
50      public void testSkipAnalyze() throws Exception {
51          doTest("analyze");
52      }
53  
54      public void testSkipAnalyzeDepMgt() throws Exception {
55          doTest("analyze-dep-mgt");
56      }
57  
58      public void testSkipAnalyzeOnly() throws Exception {
59          doTest("analyze-only");
60      }
61  
62      public void testSkipAnalyzeReport() throws Exception {
63          doSpecialTest("analyze-report", true);
64      }
65  
66      public void testSkipAnalyzeDuplicate() throws Exception {
67          doTest("analyze-duplicate");
68      }
69  
70      public void testSkipBuildClasspath() throws Exception {
71          doTest("build-classpath");
72      }
73  
74      public void testSkipCopy() throws Exception {
75          doTest("copy");
76      }
77  
78      public void testSkipCopyDependencies() throws Exception {
79          doTest("copy-dependencies");
80      }
81  
82      public void testSkipGet() throws Exception {
83          doSpecialTest("get");
84      }
85  
86      public void testSkipGoOffline() throws Exception {
87          doTest("go-offline");
88      }
89  
90      public void testSkipList() throws Exception {
91          doTest("list");
92      }
93  
94      public void testSkipProperties() throws Exception {
95          doTest("properties");
96      }
97  
98      public void testSkipPurgeLocalRepository() throws Exception {
99          doSpecialTest("purge-local-repository");
100     }
101 
102     public void testSkipResolve() throws Exception {
103         doTest("resolve");
104     }
105 
106     public void testSkipResolvePlugins() throws Exception {
107         doTest("resolve-plugins");
108     }
109 
110     public void testSkipSources() throws Exception {
111         doTest("sources");
112     }
113 
114     public void testSkipTree() throws Exception {
115         doTest("tree");
116     }
117 
118     public void testSkipUnpack() throws Exception {
119         doTest("unpack");
120     }
121 
122     public void testSkipUnpackDependencies() throws Exception {
123         doTest("unpack-dependencies");
124     }
125 
126     protected void doTest(String mojoName) throws Exception {
127         doConfigTest(mojoName, "plugin-config.xml");
128     }
129 
130     protected void doSpecialTest(String mojoName) throws Exception {
131         doConfigTest(mojoName, "plugin-" + mojoName + "-config.xml", false);
132     }
133 
134     protected void doSpecialTest(String mojoName, boolean addMojoExecution) throws Exception {
135         doConfigTest(mojoName, "plugin-" + mojoName + "-config.xml", addMojoExecution);
136     }
137 
138     private void doConfigTest(String mojoName, String configFile) throws Exception {
139         doConfigTest(mojoName, configFile, false);
140     }
141 
142     private void doConfigTest(String mojoName, String configFile, boolean addMojoExecution) throws Exception {
143         File testPom = new File(getBasedir(), "target/test-classes/unit/skip-test/" + configFile);
144         Mojo mojo = lookupMojo(mojoName, testPom);
145         assertNotNull("Mojo not found.", mojo);
146 
147         if (addMojoExecution) {
148             setVariableValueToObject(mojo, "mojoExecution", getMockMojoExecution(mojoName));
149         }
150         Log log = mock(Log.class);
151         mojo.setLog(log);
152         mojo.execute();
153 
154         ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
155         verify(log, atLeastOnce()).info(captor.capture());
156         String skipMessage;
157         if (addMojoExecution) {
158             MojoExecution me = getMockMojoExecution(mojoName);
159             String reportMojoInfo = me.getPlugin().getId() + ":" + me.getGoal();
160             skipMessage = "Skipping " + reportMojoInfo + " report goal";
161         } else {
162             skipMessage = "Skipping plugin execution";
163         }
164         assertTrue(captor.getValue().contains(skipMessage));
165     }
166 
167     private MojoExecution getMockMojoExecution(String goal) {
168         MojoDescriptor md = new MojoDescriptor();
169         md.setGoal(goal);
170 
171         MojoExecution me = new MojoExecution(md);
172 
173         PluginDescriptor pd = new PluginDescriptor();
174         Plugin p = new Plugin();
175         p.setGroupId("org.apache.maven.plugins");
176         p.setArtifactId("maven-dependency-plugin");
177         pd.setPlugin(p);
178         md.setPluginDescriptor(pd);
179 
180         return me;
181     }
182 }