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;
20
21 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
22 import org.apache.maven.plugin.testing.ArtifactStubFactory;
23 import org.apache.maven.project.MavenProject;
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.assertThatCode;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.verifyNoMoreInteractions;
35 import static org.mockito.Mockito.when;
36
37
38
39
40 @ExtendWith(MockitoExtension.class)
41 class TestRequireSnapshotVersion {
42
43 @Mock
44 private MavenProject project;
45
46 private ArtifactStubFactory factory;
47
48 @InjectMocks
49 private RequireSnapshotVersion rule;
50
51 @BeforeEach
52 public void before() {
53 factory = new ArtifactStubFactory();
54 }
55
56 @Test
57 void shouldFailForRelease() throws Exception {
58 when(project.getArtifact()).thenReturn(factory.getReleaseArtifact());
59
60 assertThatCode(rule::execute).isInstanceOf(EnforcerRuleException.class);
61 }
62
63 @Test
64 void shouldPassForSnapshot() throws Exception {
65 when(project.getArtifact()).thenReturn(factory.getSnapshotArtifact());
66
67 assertThatCode(rule::execute).doesNotThrowAnyException();
68 }
69
70 @Test
71 void shouldFailForReleaseParent() throws Exception {
72 when(project.getArtifact()).thenReturn(factory.getSnapshotArtifact());
73
74 MavenProject parent = mock(MavenProject.class);
75 when(parent.getArtifact()).thenReturn(factory.getReleaseArtifact());
76
77 when(project.getParent()).thenReturn(parent);
78 when(project.hasParent()).thenReturn(true);
79
80 rule.setFailWhenParentIsRelease(true);
81
82 assertThatCode(rule::execute).isInstanceOf(EnforcerRuleException.class);
83 }
84
85 @Test
86 void shouldPassForSnapshotParent() throws Exception {
87 when(project.getArtifact()).thenReturn(factory.getSnapshotArtifact());
88
89 MavenProject parent = mock(MavenProject.class);
90 when(parent.getArtifact()).thenReturn(factory.getSnapshotArtifact());
91
92 when(project.getParent()).thenReturn(parent);
93 when(project.hasParent()).thenReturn(true);
94
95 rule.setFailWhenParentIsRelease(true);
96
97 assertThatCode(rule::execute).doesNotThrowAnyException();
98 }
99
100 @Test
101 void parentShouldNotBeChecked() throws Exception {
102 when(project.getArtifact()).thenReturn(factory.getSnapshotArtifact());
103
104 rule.setFailWhenParentIsRelease(false);
105
106 assertThatCode(rule::execute).doesNotThrowAnyException();
107
108 verify(project).getArtifact();
109 verifyNoMoreInteractions(project);
110 }
111 }