1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
39
40
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
77 });
78
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
91 });
92
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
131 });
132
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
147 });
148
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
162 }
163 }