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;
20  
21  import javax.inject.Inject;
22  
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.stream.Collectors;
30  
31  import org.apache.maven.internal.impl.DefaultLifecycleRegistry;
32  import org.apache.maven.internal.impl.DefaultLookup;
33  import org.apache.maven.lifecycle.mapping.LifecyclePhase;
34  import org.codehaus.plexus.PlexusContainer;
35  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
36  import org.codehaus.plexus.testing.PlexusTest;
37  import org.junit.jupiter.api.Test;
38  
39  import static org.hamcrest.MatcherAssert.assertThat;
40  import static org.hamcrest.Matchers.arrayWithSize;
41  import static org.hamcrest.Matchers.hasSize;
42  import static org.hamcrest.Matchers.is;
43  import static org.junit.jupiter.api.Assertions.assertNotNull;
44  import static org.junit.jupiter.api.Assertions.assertTrue;
45  import static org.mockito.Mockito.mock;
46  import static org.mockito.Mockito.when;
47  
48  /**
49   */
50  @PlexusTest
51  class DefaultLifecyclesTest {
52      @Inject
53      private DefaultLifecycles defaultLifeCycles;
54  
55      @Test
56      void testDefaultLifecycles() {
57          final List<Lifecycle> lifecycles = defaultLifeCycles.getLifeCycles();
58          assertThat(lifecycles, hasSize(3));
59          assertThat(DefaultLifecycles.STANDARD_LIFECYCLES, arrayWithSize(3));
60      }
61  
62      @Test
63      void testDefaultLifecycle() {
64          final Lifecycle lifecycle = getLifeCycleById("default");
65          assertThat(lifecycle.getId(), is("default"));
66          assertThat(lifecycle.getPhases(), hasSize(54));
67      }
68  
69      @Test
70      void testCleanLifecycle() {
71          final Lifecycle lifecycle = getLifeCycleById("clean");
72          assertThat(lifecycle.getId(), is("clean"));
73          assertThat(lifecycle.getPhases(), hasSize(3));
74      }
75  
76      @Test
77      void testSiteLifecycle() {
78          final Lifecycle lifecycle = getLifeCycleById("site");
79          assertThat(lifecycle.getId(), is("site"));
80          assertThat(lifecycle.getPhases(), hasSize(6));
81      }
82  
83      @Test
84      void testCustomLifecycle() throws ComponentLookupException {
85          List<Lifecycle> myLifecycles = new ArrayList<>();
86          Lifecycle myLifecycle =
87                  new Lifecycle("etl", Arrays.asList("extract", "transform", "load"), Collections.emptyMap());
88          myLifecycles.add(myLifecycle);
89          myLifecycles.addAll(defaultLifeCycles.getLifeCycles());
90  
91          Map<String, Lifecycle> lifeCycles = myLifecycles.stream().collect(Collectors.toMap(Lifecycle::getId, l -> l));
92          PlexusContainer mockedPlexusContainer = mock(PlexusContainer.class);
93          when(mockedPlexusContainer.lookupMap(Lifecycle.class)).thenReturn(lifeCycles);
94  
95          DefaultLifecycles dl = new DefaultLifecycles(
96                  new DefaultLifecycleRegistry(
97                          List.of(new DefaultLifecycleRegistry.LifecycleWrapperProvider(mockedPlexusContainer))),
98                  new DefaultLookup(mockedPlexusContainer));
99  
100         assertThat(dl.getLifeCycles().get(0).getId(), is("clean"));
101         assertThat(dl.getLifeCycles().get(1).getId(), is("default"));
102         assertThat(dl.getLifeCycles().get(2).getId(), is("site"));
103         assertThat(dl.getLifeCycles().get(3).getId(), is("etl"));
104     }
105 
106     /**
107      * Test for <a href="https://github.com/apache/maven/issues/11796">MNG-11796</a>:
108      * custom lifecycle with {@code <default-phases>} binding goals to standard lifecycle phases.
109      */
110     @Test
111     void testCustomLifecycleDefaultPhasesForStandardPhases() throws ComponentLookupException {
112         // Create a custom lifecycle with default-phases mapping to a standard lifecycle phase
113         Map<String, LifecyclePhase> defaultPhases = new HashMap<>();
114         defaultPhases.put("process-sources", new LifecyclePhase("com.example:my-plugin:touch"));
115 
116         Lifecycle customLifecycle = new Lifecycle("my-extension", Arrays.asList("my-dummy-phase"), defaultPhases);
117 
118         List<Lifecycle> allLifecycles = new ArrayList<>();
119         allLifecycles.add(customLifecycle);
120         allLifecycles.addAll(defaultLifeCycles.getLifeCycles());
121 
122         Map<String, Lifecycle> lifeCycles = allLifecycles.stream().collect(Collectors.toMap(Lifecycle::getId, l -> l));
123         PlexusContainer mockedPlexusContainer = mock(PlexusContainer.class);
124         when(mockedPlexusContainer.lookupMap(Lifecycle.class)).thenReturn(lifeCycles);
125 
126         DefaultLifecycles dl = new DefaultLifecycles(
127                 new DefaultLifecycleRegistry(
128                         List.of(new DefaultLifecycleRegistry.LifecycleWrapperProvider(mockedPlexusContainer))),
129                 new DefaultLookup(mockedPlexusContainer));
130 
131         // Find the custom lifecycle
132         Lifecycle result = dl.getLifeCycles().stream()
133                 .filter(l -> "my-extension".equals(l.getId()))
134                 .findFirst()
135                 .orElseThrow(() -> new AssertionError("Custom lifecycle not found"));
136 
137         // Verify that getPhases() only contains the custom phase (not standard phases)
138         assertTrue(result.getPhases().contains("my-dummy-phase"), "Custom lifecycle should contain its own phase");
139 
140         // Verify that defaultLifecyclePhases includes the standard phase binding
141         Map<String, LifecyclePhase> resultDefaultPhases = result.getDefaultLifecyclePhases();
142         assertNotNull(resultDefaultPhases, "Default lifecycle phases should not be null");
143         assertTrue(
144                 resultDefaultPhases.containsKey("process-sources"),
145                 "Default lifecycle phases should contain 'process-sources' binding");
146         String goalSpec = resultDefaultPhases.get("process-sources").toString();
147         assertTrue(
148                 goalSpec.contains("com.example") && goalSpec.contains("my-plugin") && goalSpec.contains("touch"),
149                 "The process-sources binding should map to the correct goal, got: " + goalSpec);
150     }
151 
152     private Lifecycle getLifeCycleById(String id) {
153         return defaultLifeCycles.getLifeCycles().stream()
154                 .filter(l -> id.equals(l.getId()))
155                 .findFirst()
156                 .orElseThrow(IllegalArgumentException::new);
157     }
158 }