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.Test;
25  import org.junit.jupiter.api.extension.ExtendWith;
26  import org.mockito.InjectMocks;
27  import org.mockito.Mock;
28  import org.mockito.junit.jupiter.MockitoExtension;
29  
30  import static org.assertj.core.api.Assertions.assertThat;
31  import static org.assertj.core.api.Assertions.assertThatCode;
32  import static org.mockito.Mockito.never;
33  import static org.mockito.Mockito.verify;
34  import static org.mockito.Mockito.when;
35  
36  /**
37   * The Class TestRequireReleaseVersion.
38   *
39   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
40   */
41  @ExtendWith(MockitoExtension.class)
42  class TestRequireReleaseVersion {
43  
44      @Mock
45      private MavenProject project;
46  
47      @InjectMocks
48      private RequireReleaseVersion rule;
49  
50      @Test
51      void testProjectWithReleaseVersion() throws Exception {
52          ArtifactStubFactory factory = new ArtifactStubFactory();
53  
54          when(project.getArtifact()).thenReturn(factory.getReleaseArtifact());
55  
56          assertThatCode(rule::execute).doesNotThrowAnyException();
57      }
58  
59      @Test
60      void testProjectWithSnapshotVersion() throws Exception {
61          ArtifactStubFactory factory = new ArtifactStubFactory();
62  
63          when(project.getArtifact()).thenReturn(factory.getSnapshotArtifact());
64  
65          assertThatCode(rule::execute)
66                  .isInstanceOf(EnforcerRuleException.class)
67                  .hasMessageContaining("This project cannot be a snapshot");
68      }
69  
70      @Test
71      void shouldFailWhenParentIsSnapshot() throws Exception {
72          ArtifactStubFactory factory = new ArtifactStubFactory();
73  
74          when(project.getArtifact()).thenReturn(factory.getReleaseArtifact());
75          when(project.getParentArtifact()).thenReturn(factory.getSnapshotArtifact());
76  
77          rule.setFailWhenParentIsSnapshot(true);
78  
79          assertThatCode(rule::execute)
80                  .isInstanceOf(EnforcerRuleException.class)
81                  .hasMessageContaining("Parent Cannot be a snapshot");
82      }
83  
84      @Test
85      void shouldAllowParentSnapshot() throws Exception {
86          ArtifactStubFactory factory = new ArtifactStubFactory();
87  
88          when(project.getArtifact()).thenReturn(factory.getReleaseArtifact());
89  
90          rule.setFailWhenParentIsSnapshot(false);
91  
92          assertThatCode(rule::execute).doesNotThrowAnyException();
93  
94          verify(project, never()).getParentArtifact();
95      }
96  
97      /**
98       * Test cache.
99       */
100     @Test
101     void testCache() {
102         assertThat(rule.getCacheId()).isNull();
103     }
104 }