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.files;
20  
21  import java.io.BufferedWriter;
22  import java.io.File;
23  import java.io.FileWriter;
24  import java.io.IOException;
25  import java.util.Arrays;
26  import java.util.Collections;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.enforcer.rule.api.EnforcerLogger;
30  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
31  import org.apache.maven.plugin.testing.ArtifactStubFactory;
32  import org.apache.maven.project.MavenProject;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  import org.junit.jupiter.api.extension.ExtendWith;
36  import org.junit.jupiter.api.io.TempDir;
37  import org.mockito.InjectMocks;
38  import org.mockito.Mock;
39  import org.mockito.junit.jupiter.MockitoExtension;
40  
41  import static org.junit.jupiter.api.Assertions.assertFalse;
42  import static org.junit.jupiter.api.Assertions.assertNotNull;
43  import static org.junit.jupiter.api.Assertions.assertNull;
44  import static org.junit.jupiter.api.Assertions.assertSame;
45  import static org.junit.jupiter.api.Assertions.assertTrue;
46  import static org.junit.jupiter.api.Assertions.fail;
47  import static org.mockito.Mockito.when;
48  
49  /**
50   * Test the "require files exist" rule.
51   *
52   * @author <a href="brianf@apache.org">Brian Fox</a>
53   */
54  @ExtendWith(MockitoExtension.class)
55  class TestRequireFilesSize {
56      @TempDir
57      public File temporaryFolder;
58  
59      @Mock
60      private MavenProject project;
61  
62      @Mock
63      private EnforcerLogger log;
64  
65      @InjectMocks
66      private RequireFilesSize rule;
67  
68      @BeforeEach
69      void setup() {
70          rule.setLog(log);
71      }
72  
73      @Test
74      void testFileExists() throws EnforcerRuleException, IOException {
75          File f = File.createTempFile("junit", null, temporaryFolder);
76  
77          rule.setFilesList(Collections.singletonList(f));
78  
79          rule.execute();
80      }
81  
82      @Test
83      void testEmptyFile() {
84          rule.setFilesList(Collections.singletonList(null));
85          try {
86              rule.execute();
87              fail("Should get exception");
88          } catch (EnforcerRuleException e) {
89              assertNotNull(e.getMessage());
90          }
91      }
92  
93      @Test
94      void testEmptyFileAllowNull() throws EnforcerRuleException {
95          rule.setFilesList(Collections.singletonList(null));
96          rule.setAllowNulls(true);
97          rule.execute();
98      }
99  
100     @Test
101     void testEmptyFileList() throws EnforcerRuleException, IOException {
102         rule.setFilesList(Collections.emptyList());
103 
104         assertTrue(rule.getFiles().isEmpty());
105 
106         File f = File.createTempFile("junit", null, temporaryFolder);
107 
108         ArtifactStubFactory factory = new ArtifactStubFactory();
109         Artifact a = factory.getReleaseArtifact();
110         a.setFile(f);
111 
112         when(project.getArtifact()).thenReturn(a);
113 
114         // sanity check the mockProject
115         assertSame(f, project.getArtifact().getFile());
116 
117         rule.execute();
118     }
119 
120     @Test
121     void testFileDoesNotExist() throws IOException {
122         File f = File.createTempFile("junit", null, temporaryFolder);
123         f.delete();
124         assertFalse(f.exists());
125         rule.setFilesList(Collections.singletonList(f));
126 
127         try {
128             rule.execute();
129             fail("Should get exception");
130         } catch (EnforcerRuleException e) {
131             assertNotNull(e.getMessage());
132         }
133     }
134 
135     @Test
136     void testFileTooSmall() throws IOException {
137         File f = File.createTempFile("junit", null, temporaryFolder);
138         rule.setFilesList(Collections.singletonList(f));
139         rule.setMinsize(10);
140         try {
141             rule.execute();
142             fail("Should get exception");
143         } catch (EnforcerRuleException e) {
144             assertNotNull(e.getMessage());
145         }
146     }
147 
148     @Test
149     void testFileTooBig() throws IOException {
150         File f = File.createTempFile("junit", null, temporaryFolder);
151         try (BufferedWriter out = new BufferedWriter(new FileWriter(f))) {
152             out.write("123456789101112131415");
153         }
154 
155         rule.setFilesList(Collections.singletonList(f));
156         rule.setMaxsize(10);
157         assertTrue(f.length() > 10);
158         try {
159             rule.execute();
160             fail("Should get exception");
161         } catch (EnforcerRuleException e) {
162             assertNotNull(e.getMessage());
163         }
164     }
165 
166     @Test
167     void testRequireFilesSizeSatisfyAny() throws EnforcerRuleException, IOException {
168         File f = File.createTempFile("junit", null, temporaryFolder);
169         try (BufferedWriter out = new BufferedWriter(new FileWriter(f))) {
170             out.write("123456789101112131415");
171         }
172         assertTrue(f.length() > 10);
173 
174         File g = File.createTempFile("junit", null, temporaryFolder);
175 
176         rule.setFilesList(Arrays.asList(f, g));
177         rule.setMaxsize(10);
178         rule.setSatisfyAny(true);
179 
180         rule.execute();
181     }
182 
183     /**
184      * Test id.
185      */
186     @Test
187     void testId() {
188         assertNull(rule.getCacheId());
189     }
190 }