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.enforcer.rules;
20  
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
27  import org.apache.maven.project.MavenProject;
28  import org.junit.jupiter.api.Test;
29  import org.junit.jupiter.api.extension.ExtendWith;
30  import org.mockito.InjectMocks;
31  import org.mockito.Mock;
32  import org.mockito.junit.jupiter.MockitoExtension;
33  
34  import static org.junit.jupiter.api.Assertions.assertThrows;
35  import static org.mockito.Mockito.when;
36  
37  /**
38   * Check the profile rule.
39   *
40   * @author <a href="mailto:khmarbaise@apache.org">Karl Heinz Marbaise</a>
41   */
42  @ExtendWith(MockitoExtension.class)
43  class RequireActiveProfileTest {
44  
45      @Mock
46      private MavenProject project;
47  
48      @InjectMocks
49      private RequireActiveProfile rule;
50  
51      @Test
52      void testNoActiveProfilesInProjectAndNoProfilesExpectedToBeActivated() throws EnforcerRuleException {
53  
54          rule.execute();
55      }
56  
57      @Test
58      void testActiveProfileAndExpectedActiveProfile() throws EnforcerRuleException {
59          Map<String, List<String>> profiles = Collections.singletonMap("pom", Arrays.asList("profile-2"));
60  
61          when(project.getInjectedProfileIds()).thenReturn(profiles);
62  
63          rule.setProfiles("profile-2");
64  
65          rule.execute();
66      }
67  
68      @Test
69      void testNoActiveProfileButTheRuleRequestedAnActiveProfile() {
70          assertThrows(EnforcerRuleException.class, () -> {
71              when(project.getInjectedProfileIds()).thenReturn(Collections.emptyMap());
72  
73              rule.setProfiles("profile-2");
74  
75              rule.execute();
76              // intentionally no assertTrue(...)
77          });
78          // intentionally no assertTrue(...)
79      }
80  
81      @Test
82      void testNoActiveProfileButWeExpectToGetAnExceptionWithAll() {
83          assertThrows(EnforcerRuleException.class, () -> {
84              when(project.getInjectedProfileIds()).thenReturn(Collections.emptyMap());
85  
86              rule.setProfiles("profile-2");
87              rule.setAll(true);
88  
89              rule.execute();
90              // intentionally no assertTrue(...)
91          });
92          // intentionally no assertTrue(...)
93      }
94  
95      @Test
96      void testTwoActiveProfilesWithOneRequiredProfile() throws EnforcerRuleException {
97          Map<String, List<String>> profiles = Collections.singletonMap("pom", Arrays.asList("profile-1", "profile-2"));
98  
99          when(project.getInjectedProfileIds()).thenReturn(profiles);
100 
101         rule.setProfiles("profile-2");
102 
103         rule.execute();
104     }
105 
106     @Test
107     void testTwoActiveProfilesWhereOneProfileIsRequiredToBeActivated() throws EnforcerRuleException {
108         Map<String, List<String>> profiles = Collections.singletonMap("pom", Arrays.asList("profile-1", "profile-2"));
109 
110         when(project.getInjectedProfileIds()).thenReturn(profiles);
111 
112         rule.setProfiles("profile-2");
113         rule.setAll(true);
114 
115         rule.execute();
116     }
117 
118     @Test
119     void testTwoActiveProfilesWithTwoRequiredProfilesWhereOneOfThemIsNotPartOfTheActiveProfiles() {
120         assertThrows(EnforcerRuleException.class, () -> {
121             Map<String, List<String>> profiles =
122                     Collections.singletonMap("pom", Arrays.asList("profile-X", "profile-Y"));
123 
124             when(project.getInjectedProfileIds()).thenReturn(profiles);
125 
126             rule.setProfiles("profile-Z,profile-X");
127             rule.setAll(true);
128 
129             rule.execute();
130             // intentionally no assertTrue(..)
131         });
132         // intentionally no assertTrue(..)
133     }
134 
135     @Test
136     void testOneActiveProfilesWithTwoRequiredProfiles() {
137         assertThrows(EnforcerRuleException.class, () -> {
138             Map<String, List<String>> profiles = Collections.singletonMap("pom", Arrays.asList("profile-X"));
139 
140             when(project.getInjectedProfileIds()).thenReturn(profiles);
141 
142             rule.setProfiles("profile-X,profile-Y");
143             rule.setAll(true);
144 
145             rule.execute();
146             // intentionally no assertTrue(..)
147         });
148         // intentionally no assertTrue(..)
149     }
150 
151     @Test
152     void testOneActiveProfileWithTwoProfilesButNotAll() throws EnforcerRuleException {
153         Map<String, List<String>> profiles = Collections.singletonMap("pom", Arrays.asList("profile-X"));
154 
155         when(project.getInjectedProfileIds()).thenReturn(profiles);
156 
157         rule.setProfiles("profile-X,profile-Y");
158         rule.setAll(false);
159 
160         rule.execute();
161         // intentionally no assertTrue(..)
162     }
163 }