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.File;
22 import java.io.IOException;
23 import java.util.Arrays;
24 import java.util.Collections;
25
26 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.io.TempDir;
29
30 import static org.junit.jupiter.api.Assertions.assertFalse;
31 import static org.junit.jupiter.api.Assertions.assertNotNull;
32 import static org.junit.jupiter.api.Assertions.assertThrows;
33 import static org.junit.jupiter.api.Assertions.assertTrue;
34
35
36
37
38
39
40 class TestRequireFilesExist {
41 @TempDir
42 private File temporaryFolder;
43
44 private final RequireFilesExist rule = new RequireFilesExist();
45
46 @Test
47 void testFileExists() throws Exception {
48 File f = File.createTempFile("junit", null, temporaryFolder);
49
50 rule.setFilesList(Collections.singletonList(f.getCanonicalFile()));
51
52 rule.execute();
53 }
54
55 @Test
56 void testFileOsIndependentExists() {
57 rule.setFilesList(Collections.singletonList(new File("POM.xml")));
58
59 EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> rule.execute());
60
61 assertNotNull(e.getMessage());
62 }
63
64 @Test
65 void testEmptyFile() {
66 rule.setFilesList(Collections.singletonList(null));
67
68 EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> rule.execute());
69
70 assertNotNull(e.getMessage());
71 }
72
73 @Test
74 void testEmptyFileAllowNull() throws Exception {
75 rule.setFilesList(Collections.singletonList(null));
76 rule.setAllowNulls(true);
77 rule.execute();
78 }
79
80 @Test
81 void testEmptyFileList() {
82 rule.setFilesList(Collections.emptyList());
83 assertTrue(rule.getFiles().isEmpty());
84
85 EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> rule.execute());
86
87 assertNotNull(e.getMessage());
88 }
89
90 @Test
91 void testEmptyFileListAllowNull() throws Exception {
92 rule.setFilesList(Collections.emptyList());
93 assertTrue(rule.getFiles().isEmpty());
94 rule.setAllowNulls(true);
95 rule.execute();
96 }
97
98 @Test
99 void testFileDoesNotExist() throws Exception {
100 File f = File.createTempFile("junit", null, temporaryFolder);
101 f.delete();
102
103 assertFalse(f.exists());
104 rule.setFilesList(Collections.singletonList(f));
105
106 EnforcerRuleException e = assertThrows(EnforcerRuleException.class, () -> rule.execute());
107
108 assertNotNull(e.getMessage());
109 }
110
111 @Test
112 void testFileExistsSatisfyAny() throws EnforcerRuleException, IOException {
113 File f = File.createTempFile("junit", null, temporaryFolder);
114 f.delete();
115
116 assertFalse(f.exists());
117
118 File g = File.createTempFile("junit", null, temporaryFolder);
119
120 assertTrue(g.exists());
121
122 rule.setFilesList(Arrays.asList(f, g.getCanonicalFile()));
123 rule.setSatisfyAny(true);
124
125 rule.execute();
126 }
127
128
129
130
131 @Test
132 void testId() {
133 assertNotNull(rule.getCacheId());
134 }
135 }