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