a11d7964649ce9ebe98994ba6192dc9c5631cfe7
[sdc.git] /
1 /*
2  * Copyright © 2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on a "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.onboarding;
18
19 import static org.openecomp.sdc.onboarding.Constants.JACOCO_SKIP;
20 import static org.openecomp.sdc.onboarding.Constants.RESOURCES_CHANGED;
21 import static org.openecomp.sdc.onboarding.Constants.SKIP_TESTS;
22 import static org.openecomp.sdc.onboarding.Constants.SKIP_TEST_RUN;
23 import static org.openecomp.sdc.onboarding.Constants.UNICORN;
24
25 import java.util.List;
26 import org.apache.maven.model.Plugin;
27 import org.apache.maven.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugins.annotations.LifecyclePhase;
31 import org.apache.maven.plugins.annotations.Mojo;
32 import org.apache.maven.plugins.annotations.Parameter;
33 import org.apache.maven.plugins.annotations.ResolutionScope;
34 import org.apache.maven.project.MavenProject;
35 import org.codehaus.plexus.util.xml.Xpp3Dom;
36
37 @Mojo(name = "pre-test-compile-helper", threadSafe = true, defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES,
38         requiresDependencyResolution = ResolutionScope.TEST)
39 public class PreTestCompileHelperMojo extends AbstractMojo {
40
41     @Parameter
42     private BuildState buildState;
43     @Parameter(defaultValue = "${project}", readonly = true)
44     private MavenProject project;
45     @Parameter(defaultValue = "${project.artifact.groupId}:${project.artifact.artifactId}")
46     private String moduleCoordinates;
47     @Parameter(defaultValue = "${project.buildPlugins}", readonly = true)
48     private List<Plugin> plugins;
49     @Parameter
50     private String excludePackaging;
51
52
53     public void execute() throws MojoExecutionException, MojoFailureException {
54         if (!System.getProperties().containsKey(UNICORN)) {
55             return;
56         }
57         if (project.getPackaging().equals(excludePackaging)) {
58             return;
59         }
60         if (buildState.isTestExecutionMandatory()) {
61             project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.FALSE.toString());
62         }
63         boolean isTestMust = buildState.isTestMust(moduleCoordinates);
64         if (isTestMust) {
65             project.getProperties().setProperty(RESOURCES_CHANGED, Boolean.TRUE.toString());
66             if (!project.getProperties().containsKey(SKIP_TEST_RUN)) {
67                 project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.FALSE.toString());
68             }
69         }
70         if (!project.getProperties().containsKey(SKIP_TEST_RUN) || isTestSkippedExplicitly()) {
71             project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.TRUE.toString());
72         }
73         if (System.getProperties().containsKey(JACOCO_SKIP) && Boolean.FALSE.equals(Boolean.valueOf(
74                 System.getProperties().getProperty(JACOCO_SKIP)))) {
75             project.getProperties().setProperty(SKIP_TEST_RUN, Boolean.FALSE.toString());
76         }
77     }
78
79
80     private boolean isTestSkippedExplicitly() {
81         for (Plugin p : plugins) {
82             if ("org.apache.maven.plugins:maven-surefire-plugin".equals(p.getKey())) {
83                 Xpp3Dom dom = Xpp3Dom.class.cast(p.getConfiguration());
84                 if (dom.getChild(SKIP_TESTS) != null) {
85                     return Boolean.TRUE.equals(Boolean.valueOf(dom.getValue()));
86                 }
87             }
88         }
89         return false;
90     }
91 }