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.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
37
38
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
59
60
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
70 rule.execute();
71
72
73 rule.setVersion("(2.0.5");
74
75 try {
76 rule.execute();
77 fail("Expected an exception.");
78 } catch (EnforcerRuleException e) {
79
80 }
81
82
83 rule.setVersion("2.0.5_01");
84 rule.execute();
85 }
86
87
88
89
90
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
112 }
113 }
114
115
116
117
118 @Test
119 void testId() {
120 rule.setVersion("3.3.3");
121 assertThat(rule.getCacheId()).isNotEmpty();
122 }
123 }