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.version;
20  
21  import org.apache.maven.enforcer.rule.api.EnforcerLogger;
22  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
23  import org.apache.maven.rtinfo.RuntimeInformation;
24  import org.junit.jupiter.api.BeforeEach;
25  import org.junit.jupiter.api.Test;
26  import org.junit.jupiter.api.extension.ExtendWith;
27  import org.mockito.InjectMocks;
28  import org.mockito.Mock;
29  import org.mockito.junit.jupiter.MockitoExtension;
30  
31  import static org.assertj.core.api.Assertions.assertThat;
32  import static org.junit.jupiter.api.Assertions.fail;
33  import static org.mockito.Mockito.when;
34  
35  /**
36   * The Class TestMavenVersion.
37   *
38   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
39   */
40  @ExtendWith(MockitoExtension.class)
41  class TestMavenVersion {
42  
43      @Mock
44      private RuntimeInformation runtimeInformation;
45  
46      @Mock
47      private EnforcerLogger log;
48  
49      @InjectMocks
50      private RequireMavenVersion rule;
51  
52      @BeforeEach
53      void setup() {
54          rule.setLog(log);
55      }
56  
57      /**
58       * Test rule.
59       *
60       * @throws EnforcerRuleException the enforcer rule exception
61       */
62      @Test
63      void testRule() throws EnforcerRuleException {
64  
65          when(runtimeInformation.getMavenVersion()).thenReturn("3.0");
66  
67          rule.setVersion("2.0.5");
68  
69          // test the singular version
70          rule.execute();
71  
72          // exclude this version
73          rule.setVersion("(2.0.5");
74  
75          try {
76              rule.execute();
77              fail("Expected an exception.");
78          } catch (EnforcerRuleException e) {
79              // expected to catch this.
80          }
81  
82          // this shouldn't crash
83          rule.setVersion("2.0.5_01");
84          rule.execute();
85      }
86  
87      /**
88       * Test few more cases
89       *
90       * @throws EnforcerRuleException the enforcer rule exception
91       */
92      @Test
93      void checkRequireVersionMatrix() throws EnforcerRuleException {
94  
95          when(runtimeInformation.getMavenVersion()).thenReturn("3.6.1");
96          rule.setVersion("3.6.0");
97          rule.execute();
98  
99          when(runtimeInformation.getMavenVersion()).thenReturn("3.6.2");
100         rule.setVersion("3.6.0");
101         rule.execute();
102         rule.setVersion("3.6.1");
103         rule.execute();
104         rule.setVersion("3.6.2");
105         rule.execute();
106         rule.setVersion("3.6.3");
107         try {
108             rule.execute();
109             fail("Expected an exception.");
110         } catch (EnforcerRuleException e) {
111             // expected to catch this.
112         }
113     }
114 
115     /**
116      * Test id.
117      */
118     @Test
119     void testId() {
120         rule.setVersion("3.3.3");
121         assertThat(rule.getCacheId()).isNotEmpty();
122     }
123 }