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.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.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.UUID;
30
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.enforcer.rule.api.EnforcerLogger;
33 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
34 import org.apache.maven.plugin.testing.ArtifactStubFactory;
35 import org.apache.maven.project.MavenProject;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.junit.jupiter.api.io.TempDir;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.junit.jupiter.MockitoExtension;
43
44 import static org.junit.jupiter.api.Assertions.assertFalse;
45 import static org.junit.jupiter.api.Assertions.assertNotNull;
46 import static org.junit.jupiter.api.Assertions.assertNull;
47 import static org.junit.jupiter.api.Assertions.assertSame;
48 import static org.junit.jupiter.api.Assertions.assertTrue;
49 import static org.junit.jupiter.api.Assertions.fail;
50 import static org.mockito.Mockito.when;
51
52
53
54
55
56
57 @ExtendWith(MockitoExtension.class)
58 class TestRequireFilesSize {
59 @TempDir
60 private File temporaryFolder;
61
62 @Mock
63 private MavenProject project;
64
65 @Mock
66 private EnforcerLogger log;
67
68 @InjectMocks
69 private RequireFilesSize rule;
70
71 @BeforeEach
72 void setup() {
73 rule.setLog(log);
74 }
75
76 @Test
77 void testFileExists() throws EnforcerRuleException, IOException {
78 File f = File.createTempFile("junit", null, temporaryFolder);
79
80 rule.setFilesList(Collections.singletonList(f));
81
82 rule.execute();
83 }
84
85 @Test
86 void testEmptyFile() {
87 rule.setFilesList(Collections.singletonList(null));
88 try {
89 rule.execute();
90 fail("Should get exception");
91 } catch (EnforcerRuleException e) {
92 assertNotNull(e.getMessage());
93 }
94 }
95
96 @Test
97 void testEmptyFileAllowNull() throws EnforcerRuleException {
98 rule.setFilesList(Collections.singletonList(null));
99 rule.setAllowNulls(true);
100 rule.execute();
101 }
102
103 @Test
104 void testEmptyFileList() throws EnforcerRuleException, IOException {
105 rule.setFilesList(Collections.emptyList());
106
107 assertTrue(rule.getFiles().isEmpty());
108
109 File f = File.createTempFile("junit", null, temporaryFolder);
110
111 ArtifactStubFactory factory = new ArtifactStubFactory();
112 Artifact a = factory.getReleaseArtifact();
113 a.setFile(f);
114
115 when(project.getArtifact()).thenReturn(a);
116
117
118 assertSame(f, project.getArtifact().getFile());
119
120 rule.execute();
121 }
122
123 @Test
124 void testFileDoesNotExist() throws IOException {
125 File f = File.createTempFile("junit", null, temporaryFolder);
126 f.delete();
127 assertFalse(f.exists());
128 rule.setFilesList(Collections.singletonList(f));
129
130 try {
131 rule.execute();
132 fail("Should get exception");
133 } catch (EnforcerRuleException e) {
134 assertNotNull(e.getMessage());
135 }
136 }
137
138 @Test
139 void testFileTooSmall() throws IOException {
140 File f = File.createTempFile("junit", null, temporaryFolder);
141 rule.setFilesList(Collections.singletonList(f));
142 rule.setMinsize(10);
143 try {
144 rule.execute();
145 fail("Should get exception");
146 } catch (EnforcerRuleException e) {
147 assertNotNull(e.getMessage());
148 }
149 }
150
151 @Test
152 void testFileTooBig() throws IOException {
153 File f = File.createTempFile("junit", null, temporaryFolder);
154 try (BufferedWriter out = new BufferedWriter(new FileWriter(f))) {
155 out.write("123456789101112131415");
156 }
157
158 rule.setFilesList(Collections.singletonList(f));
159 rule.setMaxsize(10);
160 assertTrue(f.length() > 10);
161 try {
162 rule.execute();
163 fail("Should get exception");
164 } catch (EnforcerRuleException e) {
165 assertNotNull(e.getMessage());
166 }
167 }
168
169 @Test
170 void testRequireFilesSizeSatisfyAny() throws EnforcerRuleException, IOException {
171 File f = File.createTempFile("junit", null, temporaryFolder);
172 try (BufferedWriter out = new BufferedWriter(new FileWriter(f))) {
173 out.write("123456789101112131415");
174 }
175 assertTrue(f.length() > 10);
176
177 File g = File.createTempFile("junit", null, temporaryFolder);
178
179 rule.setFilesList(Arrays.asList(f, g));
180 rule.setMaxsize(10);
181 rule.setSatisfyAny(true);
182
183 rule.execute();
184 }
185
186 @Test
187 void testDirectoryContentOverUpperBound() throws IOException {
188 Path d = Files.createTempDirectory(temporaryFolder.toPath(), "junit");
189 long totalSize = d.toFile().length();
190 for (int i = 0; i < 3; i++) {
191 Path f = Files.createTempFile(d, "file", ".txt");
192 Files.write(f, UUID.randomUUID().toString().getBytes());
193 totalSize += f.toFile().length();
194 }
195 rule.setFilesList(Collections.singletonList(d.toFile()));
196 rule.setMaxsize(totalSize - 1);
197 rule.setRecursive(true);
198 try {
199 rule.execute();
200 fail("Should get exception");
201 } catch (EnforcerRuleException e) {
202 assertNotNull(e.getMessage());
203 }
204 }
205
206 @Test
207 void testDirectoryContentUnderLowerBound() throws IOException, EnforcerRuleException {
208 Path d = Files.createTempDirectory(temporaryFolder.toPath(), "junit");
209 long totalSize = d.toFile().length();
210 for (int i = 0; i < 3; i++) {
211 Path f = Files.createTempFile(d, "file", ".txt");
212 Files.write(f, UUID.randomUUID().toString().getBytes());
213 totalSize += f.toFile().length();
214 }
215 rule.setFilesList(Collections.singletonList(d.toFile()));
216 rule.setMinsize(totalSize);
217 rule.setRecursive(true);
218 rule.execute();
219 }
220
221
222
223
224 @Test
225 void testId() {
226 assertNull(rule.getCacheId());
227 }
228 }