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 java.util.Collections;
22  
23  import org.apache.maven.enforcer.rule.api.EnforcerLogger;
24  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
25  import org.apache.maven.model.Prerequisites;
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.verify;
36  import static org.mockito.Mockito.when;
37  
38  @ExtendWith(MockitoExtension.class)
39  class RequirePrerequisiteTest {
40  
41      @Mock
42      private MavenProject project;
43  
44      @Mock
45      private EnforcerLogger log;
46  
47      @InjectMocks
48      private RequirePrerequisite rule;
49  
50      @BeforeEach
51      void before() {
52          rule.setLog(log);
53          when(project.getPackaging()).thenReturn("maven-plugin");
54      }
55  
56      @Test
57      void testNoPrerequisite() {
58          assertThrows(EnforcerRuleException.class, () -> rule.execute());
59      }
60  
61      @Test
62      void testNoSpecifiedPrerequisite() throws Exception {
63          when(project.getPrerequisites()).thenReturn(new Prerequisites());
64          rule.execute();
65      }
66  
67      @Test
68      void testLowerMavenPrerequisite() {
69          when(project.getPrerequisites()).thenReturn(new Prerequisites());
70  
71          assertThrows(EnforcerRuleException.class, () -> {
72              rule.setMavenVersion("3.0");
73              rule.execute();
74          });
75      }
76  
77      @Test
78      void testLowerMavenRangePrerequisite() {
79          when(project.getPrerequisites()).thenReturn(new Prerequisites());
80  
81          assertThrows(EnforcerRuleException.class, () -> {
82              rule.setMavenVersion("[3.0,)");
83              rule.execute();
84          });
85      }
86  
87      @Test
88      void testMavenRangesPrerequisite() {
89          assertThrows(EnforcerRuleException.class, () -> {
90              Prerequisites prerequisites = new Prerequisites();
91              prerequisites.setMaven("2.2.0");
92              when(project.getPrerequisites()).thenReturn(prerequisites);
93  
94              rule.setMavenVersion("[2.0.6,2.1.0),(2.1.0,2.2.0),(2.2.0,)");
95              rule.execute();
96          });
97      }
98  
99      @Test
100     void testValidPrerequisite() throws Exception {
101         Prerequisites prerequisites = new Prerequisites();
102         prerequisites.setMaven("3.0");
103         when(project.getPrerequisites()).thenReturn(prerequisites);
104 
105         rule.setMavenVersion("2.2.1");
106         rule.execute();
107     }
108 
109     @Test
110     void testPomPackaging() throws Exception {
111         when(project.getPackaging()).thenReturn("pom");
112 
113         rule.execute();
114 
115         verify(log).debug("Packaging is pom, skipping requirePrerequisite rule");
116     }
117 
118     @Test
119     void testMatchingPackagings() {
120         assertThrows(EnforcerRuleException.class, () -> {
121             when(project.getPackaging()).thenReturn("maven-plugin");
122 
123             rule.setPackagings(Collections.singletonList("maven-plugin"));
124             rule.execute();
125         });
126     }
127 
128     @Test
129     void testNotMatchingPackagings() throws Exception {
130         when(project.getPackaging()).thenReturn("jar");
131 
132         rule.setPackagings(Collections.singletonList("maven-plugin"));
133         rule.execute();
134 
135         verify(log).debug("Packaging is jar, skipping requirePrerequisite rule");
136     }
137 }