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 org.apache.maven.enforcer.rule.api.EnforcerLogger;
22 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
23 import org.apache.maven.model.DeploymentRepository;
24 import org.apache.maven.model.DistributionManagement;
25 import org.apache.maven.model.Model;
26 import org.apache.maven.model.Site;
27 import org.apache.maven.project.MavenProject;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.junit.jupiter.MockitoExtension;
34
35 import static org.junit.jupiter.api.Assertions.assertThrows;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
38
39
40
41
42
43
44 @ExtendWith(MockitoExtension.class)
45 class BanDistributionManagementTest {
46
47 @Mock
48 private MavenProject project;
49
50 @Mock
51 private EnforcerLogger log;
52
53 @InjectMocks
54 private BanDistributionManagement rule;
55
56 @BeforeEach
57 void setup() {
58 rule.setLog(log);
59 }
60
61 @Test
62 void shouldNotFailWithoutDistributionManagement() throws Exception {
63 setupProjectWithoutDistributionManagement();
64 rule.execute();
65
66 }
67
68
69
70
71
72
73
74
75
76
77 @Test
78 void shouldThrowExceptionIfDistributionManagementIsDefinedWithRepository() {
79 setupProjectWithDistributionManagement(new DeploymentRepository(), null, null);
80 assertThrows(EnforcerRuleException.class, () -> rule.execute());
81 }
82
83
84
85
86
87
88
89
90
91
92 @Test
93 void shouldThrowExceptionIfDistributionManagementIsDefinedWithRepositorySnapshotRepository() {
94 setupProjectWithDistributionManagement(null, new DeploymentRepository(), null);
95
96 assertThrows(EnforcerRuleException.class, () -> rule.execute());
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 @Test
115 void shouldThrowExceptionIfDistributionManagementIsDefinedWithRepositorySnapshotRepositorySite() {
116 setupProjectWithDistributionManagement(new DeploymentRepository(), null, null);
117
118 assertThrows(EnforcerRuleException.class, () -> rule.execute());
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132 @Test
133 void shouldAllowDistributionManagementHavingRepository() throws Exception {
134 setupProjectWithDistributionManagement(null, null, null);
135 rule.setAllowRepository(true);
136 rule.execute();
137
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 @Test
155 void shouldAllowDistributionManagementHavingRepositorySnapshotRepository() throws Exception {
156 setupProjectWithDistributionManagement(null, null, null);
157
158 rule.setAllowRepository(true);
159 rule.setAllowSnapshotRepository(true);
160 rule.execute();
161
162 }
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181 @Test
182 void shouldAllowDistributionManagementHavingRepositorySnapshotRepositorySite() throws Exception {
183 setupProjectWithDistributionManagement(null, null, null);
184 rule.setAllowRepository(true);
185 rule.setAllowSnapshotRepository(true);
186 rule.setAllowSite(true);
187 rule.execute();
188
189 }
190
191 private void setupProjectWithoutDistributionManagement() {
192 setupProject(null);
193 }
194
195 private void setupProjectWithDistributionManagement(
196 DeploymentRepository repository, DeploymentRepository snapshotRepository, Site site) {
197 DistributionManagement dm = mock(DistributionManagement.class);
198 if (repository != null) {
199 when(dm.getRepository()).thenReturn(repository);
200 }
201 if (snapshotRepository != null) {
202 when(dm.getSnapshotRepository()).thenReturn(snapshotRepository);
203 }
204 if (site != null) {
205 when(dm.getSite()).thenReturn(site);
206 }
207 setupProject(dm);
208
209 when(project.getParent()).thenReturn(mock(MavenProject.class));
210 when(project.isExecutionRoot()).thenReturn(true);
211 }
212
213 private void setupProject(DistributionManagement distributionManagement) {
214
215 Model mavenModel = mock(Model.class);
216 when(project.getOriginalModel()).thenReturn(mavenModel);
217 when(mavenModel.getDistributionManagement()).thenReturn(distributionManagement);
218 }
219 }