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 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   * This class is intended to test the {@link BanDistributionManagement} rule.
40   *
41   * @author <a href="mailto:khmarbaise@apache.org">Karl Heinz Marbaise</a>
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          // intentionally no assert cause in case of an exception the test will be red.
65      }
66  
67      /**
68       * <pre>
69       * &lt;distributionManagement&gt;
70       *   &lt;repository&gt;
71       *    ...
72       *   &lt;/repository&gt;
73       * &lt;/distributionManagement&gt;
74       * </pre>
75       */
76      @Test
77      void shouldThrowExceptionIfDistributionManagementIsDefinedWithRepository() {
78          setupProjectWithDistributionManagement(new DeploymentRepository(), null);
79          assertThrows(EnforcerRuleException.class, () -> rule.execute());
80      }
81  
82      /**
83       * <pre>
84       * &lt;distributionManagement&gt;
85       *   &lt;snapshotRepository&gt;
86       *    ...
87       *   &lt;/snapshotRepository&gt;
88       * &lt;/distributionManagement&gt;
89       * </pre>
90       */
91      @Test
92      void shouldThrowExceptionIfDistributionManagementIsDefinedWithRepositorySnapshotRepository() {
93          setupProjectWithDistributionManagement(null, new DeploymentRepository());
94  
95          assertThrows(EnforcerRuleException.class, () -> rule.execute());
96      }
97  
98      /**
99       * <pre>
100      * &lt;distributionManagement&gt;
101      *   &lt;repository&gt;
102      *    ...
103      *   &lt;/repository&gt;
104      *   &lt;snapshotRepository&gt;
105      *    ...
106      *   &lt;/snapshotRepository&gt;
107      *   &lt;site&gt;
108      *    ...
109      *   &lt;/site&gt;
110      * &lt;/distributionManagement&gt;
111      * </pre>
112      */
113     @Test
114     void shouldThrowExceptionIfDistributionManagementIsDefinedWithRepositorySnapshotRepositorySite() {
115         setupProjectWithDistributionManagement(new DeploymentRepository(), null);
116 
117         assertThrows(EnforcerRuleException.class, () -> rule.execute());
118     }
119 
120     /**
121      * <pre>
122      * &lt;distributionManagement&gt;
123      *   &lt;repository&gt;
124      *    ...
125      *   &lt;/repository&gt;
126      * &lt;/distributionManagement&gt;
127      * </pre>
128      *
129      * @throws Exception if any occurs
130      */
131     @Test
132     void shouldAllowDistributionManagementHavingRepository() throws Exception {
133         setupProjectWithDistributionManagement(null, null);
134         rule.setAllowRepository(true);
135         rule.execute();
136         // intentionally no assert cause in case of an exception the test will be red.
137     }
138 
139     /**
140      * <pre>
141      * &lt;distributionManagement&gt;
142      *   &lt;repository&gt;
143      *    ...
144      *   &lt;/repository&gt;
145      *   &lt;snapshotRepository&gt;
146      *    ...
147      *   &lt;/snapshotRepository&gt;
148      * &lt;/distributionManagement&gt;
149      * </pre>
150      *
151      * @throws Exception if any occurs
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         // intentionally no assert cause in case of an exception the test will be red.
161     }
162 
163     /**
164      * <pre>
165      * &lt;distributionManagement&gt;
166      *   &lt;repository&gt;
167      *    ...
168      *   &lt;/repository&gt;
169      *   &lt;snapshotRepository&gt;
170      *    ...
171      *   &lt;/snapshotRepository&gt;
172      *   &lt;site&gt;
173      *    ...
174      *   &lt;/site&gt;
175      * &lt;/distributionManagement&gt;
176      * </pre>
177      *
178      * @throws Exception if any occurs
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         // intentionally no assert cause in case of an exception the test will be red.
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         //        when(project.getPackaging()).thenReturn("jar");
211         Model mavenModel = mock(Model.class);
212         when(project.getOriginalModel()).thenReturn(mavenModel);
213         when(mavenModel.getDistributionManagement()).thenReturn(distributionManagement);
214     }
215 }