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