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.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   * Test class for the RequireSnapshotVersion rule.
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 }