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.assertTrue;
33 import static org.junit.jupiter.api.Assertions.fail;
34
35
36
37
38
39
40 class TestRequireFilesDontExist {
41 @TempDir
42 public File temporaryFolder;
43
44 private final RequireFilesDontExist rule = new RequireFilesDontExist();
45
46 @Test
47 void testFileExists() throws IOException {
48 File f = File.createTempFile("junit", null, temporaryFolder);
49
50 rule.setFilesList(Collections.singletonList(f));
51
52 try {
53 rule.execute();
54 fail("Expected an Exception.");
55 } catch (EnforcerRuleException e) {
56 assertNotNull(e.getMessage());
57 }
58 f.delete();
59 }
60
61 @Test
62 void testEmptyFile() {
63 rule.setFilesList(Collections.singletonList(null));
64 try {
65 rule.execute();
66 fail("Should get exception");
67 } catch (EnforcerRuleException e) {
68 assertNotNull(e.getMessage());
69 }
70 }
71
72 @Test
73 void testEmptyFileAllowNull() {
74 rule.setFilesList(Collections.singletonList(null));
75 rule.setAllowNulls(true);
76 try {
77 rule.execute();
78 } catch (EnforcerRuleException e) {
79 fail("Unexpected Exception:" + e.getLocalizedMessage());
80 }
81 }
82
83 @Test
84 void testEmptyFileList() {
85 rule.setFilesList(Collections.emptyList());
86 assertTrue(rule.getFiles().isEmpty());
87 try {
88 rule.execute();
89 fail("Should get exception");
90 } catch (EnforcerRuleException e) {
91 assertNotNull(e.getMessage());
92 }
93 }
94
95 @Test
96 void testEmptyFileListAllowNull() {
97 rule.setFilesList(Collections.emptyList());
98 assertTrue(rule.getFiles().isEmpty());
99 rule.setAllowNulls(true);
100 try {
101 rule.execute();
102 } catch (EnforcerRuleException e) {
103 fail("Unexpected Exception:" + e.getLocalizedMessage());
104 }
105 }
106
107 @Test
108 void testFileDoesNotExist() throws EnforcerRuleException, IOException {
109 File f = File.createTempFile("junit", null, temporaryFolder);
110 f.delete();
111
112 assertFalse(f.exists());
113
114 rule.setFilesList(Collections.singletonList(f));
115
116 rule.execute();
117 }
118
119 @Test
120 void testFileDoesNotExistSatisfyAny() throws EnforcerRuleException, IOException {
121 File f = File.createTempFile("junit", null, temporaryFolder);
122 f.delete();
123
124 assertFalse(f.exists());
125
126 File g = File.createTempFile("junit", null, temporaryFolder);
127
128 assertTrue(g.exists());
129
130 rule.setFilesList(Arrays.asList(f, g.getCanonicalFile()));
131 rule.setSatisfyAny(true);
132
133 rule.execute();
134 }
135
136
137
138
139 @Test
140 void testId() {
141 assertNotNull(rule.getCacheId());
142 }
143 }