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.List;
27  import java.util.Map;
28  import java.util.stream.Collectors;
29  
30  import org.codehaus.plexus.PlexusContainer;
31  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
32  import org.codehaus.plexus.testing.PlexusTest;
33  import org.junit.jupiter.api.Test;
34  
35  import static org.hamcrest.MatcherAssert.assertThat;
36  import static org.hamcrest.Matchers.arrayWithSize;
37  import static org.hamcrest.Matchers.hasSize;
38  import static org.hamcrest.Matchers.is;
39  import static org.mockito.Mockito.mock;
40  import static org.mockito.Mockito.when;
41  
42  /**
43   */
44  @PlexusTest
45  class DefaultLifecyclesTest {
46      @Inject
47      private DefaultLifecycles defaultLifeCycles;
48  
49      @Test
50      void testDefaultLifecycles() {
51          final List<Lifecycle> lifecycles = defaultLifeCycles.getLifeCycles();
52          assertThat(lifecycles, hasSize(4));
53          assertThat(DefaultLifecycles.STANDARD_LIFECYCLES, arrayWithSize(4));
54      }
55  
56      @Test
57      void testDefaultLifecycle() {
58          final Lifecycle lifecycle = getLifeCycleById("default");
59          assertThat(lifecycle.getId(), is("default"));
60          assertThat(lifecycle.getPhases(), hasSize(23));
61      }
62  
63      @Test
64      void testCleanLifecycle() {
65          final Lifecycle lifecycle = getLifeCycleById("clean");
66          assertThat(lifecycle.getId(), is("clean"));
67          assertThat(lifecycle.getPhases(), hasSize(3));
68      }
69  
70      @Test
71      void testSiteLifecycle() {
72          final Lifecycle lifecycle = getLifeCycleById("site");
73          assertThat(lifecycle.getId(), is("site"));
74          assertThat(lifecycle.getPhases(), hasSize(4));
75      }
76  
77      @Test
78      void testWrapperLifecycle() {
79          final Lifecycle lifecycle = getLifeCycleById("wrapper");
80          assertThat(lifecycle.getId(), is("wrapper"));
81          assertThat(lifecycle.getPhases(), hasSize(1));
82      }
83  
84      @Test
85      void testCustomLifecycle() throws ComponentLookupException {
86          List<Lifecycle> myLifecycles = new ArrayList<>();
87          Lifecycle myLifecycle =
88                  new Lifecycle("etl", Arrays.asList("extract", "transform", "load"), Collections.emptyMap());
89          myLifecycles.add(myLifecycle);
90          myLifecycles.addAll(defaultLifeCycles.getLifeCycles());
91  
92          Map<String, Lifecycle> lifeCycles = myLifecycles.stream().collect(Collectors.toMap(Lifecycle::getId, l -> l));
93          PlexusContainer mockedPlexusContainer = mock(PlexusContainer.class);
94          when(mockedPlexusContainer.lookupMap(Lifecycle.class)).thenReturn(lifeCycles);
95  
96          DefaultLifecycles dl = new DefaultLifecycles(mockedPlexusContainer);
97  
98          assertThat(dl.getLifeCycles().get(0).getId(), is("clean"));
99          assertThat(dl.getLifeCycles().get(1).getId(), is("default"));
100         assertThat(dl.getLifeCycles().get(2).getId(), is("site"));
101         assertThat(dl.getLifeCycles().get(3).getId(), is("wrapper"));
102         assertThat(dl.getLifeCycles().get(4).getId(), is("etl"));
103     }
104 
105     private Lifecycle getLifeCycleById(String id) {
106         return defaultLifeCycles.getLifeCycles().stream()
107                 .filter(l -> id.equals(l.getId()))
108                 .findFirst()
109                 .orElseThrow(IllegalArgumentException::new);
110     }
111 }