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.lifecycle.internal.stub;
20  
21  import java.util.Arrays;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.stream.Collectors;
26  
27  import org.apache.maven.lifecycle.DefaultLifecycles;
28  import org.apache.maven.lifecycle.Lifecycle;
29  import org.apache.maven.model.Plugin;
30  import org.apache.maven.plugin.descriptor.MojoDescriptor;
31  import org.apache.maven.plugin.descriptor.PluginDescriptor;
32  
33  import static org.junit.jupiter.api.Assertions.assertTrue;
34  import static org.mockito.Mockito.mock;
35  import static org.mockito.Mockito.when;
36  
37  public class LifecyclesTestUtils {
38  
39      public static final MojoDescriptor PRE_CLEAN = createMojoDescriptor("pre-clean");
40  
41      public static final MojoDescriptor CLEAN = createMojoDescriptor("clean");
42  
43      public static final MojoDescriptor POST_CLEAN = createMojoDescriptor("post-clean");
44  
45      // default (or at least some of them)
46  
47      public static final MojoDescriptor VALIDATE = createMojoDescriptor("validate");
48  
49      public static final MojoDescriptor INITIALIZE = createMojoDescriptor("initialize");
50  
51      public static final MojoDescriptor PROCESS_TEST_RESOURCES = createMojoDescriptor("process-test-resources");
52  
53      public static final MojoDescriptor PROCESS_RESOURCES = createMojoDescriptor("process-resources");
54  
55      public static final MojoDescriptor COMPILE = createMojoDescriptor("compile", true);
56  
57      public static final MojoDescriptor TEST_COMPILE = createMojoDescriptor("test-compile");
58  
59      public static final MojoDescriptor TEST = createMojoDescriptor("test");
60  
61      public static final MojoDescriptor PACKAGE = createMojoDescriptor("package");
62  
63      public static final MojoDescriptor INSTALL = createMojoDescriptor("install");
64  
65      // site
66  
67      public static final MojoDescriptor PRE_SITE = createMojoDescriptor("pre-site");
68  
69      public static final MojoDescriptor SITE = createMojoDescriptor("site");
70  
71      public static final MojoDescriptor POST_SITE = createMojoDescriptor("post-site");
72  
73      public static final MojoDescriptor SITE_DEPLOY = createMojoDescriptor("site-deploy");
74  
75      public static DefaultLifecycles createDefaultLifecycles() {
76  
77          List<String> stubDefaultCycle = Arrays.asList(
78                  VALIDATE.getPhase(),
79                  INITIALIZE.getPhase(),
80                  PROCESS_RESOURCES.getPhase(),
81                  COMPILE.getPhase(),
82                  TEST_COMPILE.getPhase(),
83                  TEST.getPhase(),
84                  PROCESS_TEST_RESOURCES.getPhase(),
85                  PACKAGE.getPhase(),
86                  "BEER",
87                  INSTALL.getPhase());
88  
89          // The two phases below are really for future expansion, some would say they lack a drink
90          // The point being that they do not really have to match the "real" stuff,
91          List<String> stubCleanCycle = Arrays.asList(PRE_CLEAN.getPhase(), CLEAN.getPhase(), POST_CLEAN.getPhase());
92  
93          List<String> stubSiteCycle =
94                  Arrays.asList(PRE_SITE.getPhase(), SITE.getPhase(), POST_SITE.getPhase(), SITE_DEPLOY.getPhase());
95  
96          Map<String, List<String>> stubLifeCycles = new HashMap<>();
97          stubLifeCycles.put("clean", stubCleanCycle);
98          stubLifeCycles.put("default", stubDefaultCycle);
99          stubLifeCycles.put("site", stubSiteCycle);
100 
101         List<Lifecycle> matchedLifecycles = Arrays.stream(DefaultLifecycles.STANDARD_LIFECYCLES)
102                 .filter(it -> !it.equals("wrapper"))
103                 .map(standardLc -> {
104                     assertTrue(stubLifeCycles.containsKey(standardLc), "Unsupported standard lifecycle: " + standardLc);
105                     return new Lifecycle(standardLc, stubLifeCycles.get(standardLc), null);
106                 })
107                 .collect(Collectors.toList());
108 
109         DefaultLifecycles defaultLifecycles = mock(DefaultLifecycles.class);
110         when(defaultLifecycles.getLifeCycles()).thenReturn(matchedLifecycles);
111         return defaultLifecycles;
112     }
113 
114     public static MojoDescriptor createMojoDescriptor(String phaseName) {
115         return createMojoDescriptor(phaseName, false);
116     }
117 
118     public static MojoDescriptor createMojoDescriptor(String phaseName, boolean threadSafe) {
119         final MojoDescriptor mojoDescriptor = new MojoDescriptor();
120         mojoDescriptor.setPhase(phaseName);
121         final PluginDescriptor descriptor = new PluginDescriptor();
122         Plugin plugin = new Plugin();
123         plugin.setArtifactId("org.apache.maven.test.MavenExecutionPlan");
124         plugin.setGroupId("stub-plugin-" + phaseName);
125         descriptor.setPlugin(plugin);
126         descriptor.setArtifactId("artifact." + phaseName);
127         mojoDescriptor.setPluginDescriptor(descriptor);
128         mojoDescriptor.setThreadSafe(threadSafe);
129         return mojoDescriptor;
130     }
131 }