-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAgentFileVisitorTest.java
More file actions
238 lines (198 loc) · 10.6 KB
/
Copy pathAgentFileVisitorTest.java
File metadata and controls
238 lines (198 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package io.jenkins.plugins.util;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import edu.hm.hafner.util.FilteredLog;
import edu.hm.hafner.util.SerializableTest;
import edu.hm.hafner.util.VisibleForTesting;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.File;
import java.io.Serial;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Optional;
import io.jenkins.plugins.util.AgentFileVisitor.FileSystemFacade;
import io.jenkins.plugins.util.AgentFileVisitor.FileVisitorResult;
import io.jenkins.plugins.util.AgentFileVisitorTest.StringScanner;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
/**
* Tests the class {@link AgentFileVisitor}.
*
* @author Ullrich Hafner
*/
class AgentFileVisitorTest extends SerializableTest<StringScanner> {
private static final String CONTENT = "Hello World!";
private static final String PATTERN = "**/*.txt";
private static final String ENCODING = "UTF-8";
@TempDir
private File workspace;
@DisplayName("Should report error on empty results")
@CsvSource({"true, enabled", "false, disabled"})
@ParameterizedTest(name = "{index} => followSymbolicLinks={0}, message={1}")
void shouldReportErrorOnEmptyResults(final boolean followLinks, final String message) {
var scanner = new StringScanner(PATTERN, ENCODING, followLinks, true,
createFileSystemFacade(followLinks));
FileVisitorResult<String> actualResult = scanner.invoke(workspace, null);
assertThat(actualResult.getResults()).isEmpty();
assertThat(actualResult.getLog().getInfoMessages()).containsExactly(
"Searching for all files in '/absolute/path' that match the pattern '" + PATTERN + "'",
"Traversing of symbolic links: " + message);
assertThat(actualResult.getLog().getErrorMessages()).containsExactly(
"Errors during parsing",
"No files found for pattern '**/*.txt'. Configuration error?");
assertThat(actualResult.hasErrors()).isTrue();
}
@DisplayName("Should report error on single result")
@CsvSource({"true, enabled", "false, disabled"})
@ParameterizedTest(name = "{index} => followSymbolicLinks={0}, message={1}")
void shouldReturnSingleResult(final boolean followLinks, final String message) {
var scanner = new StringScanner(PATTERN, ENCODING, followLinks, true,
createFileSystemFacade(followLinks, "/one.txt"));
FileVisitorResult<String> actualResult = scanner.invoke(workspace, null);
assertThat(actualResult.getResults()).containsExactly(CONTENT + 1);
assertThat(actualResult.getLog().getInfoMessages()).containsExactly(
"Searching for all files in '/absolute/path' that match the pattern '**/*.txt'",
"Traversing of symbolic links: " + message,
"-> found 1 file",
"Successfully processed file '/one.txt'");
assertThat(actualResult.getLog().getErrorMessages()).isEmpty();
assertThat(actualResult.hasErrors()).isFalse();
}
@DisplayName("Should report error on single result")
@CsvSource({"true, enabled", "false, disabled"})
@ParameterizedTest(name = "{index} => followSymbolicLinks={0}, message={1}")
void shouldReturnMultipleResults(final boolean followLinks, final String message) {
var scanner = new StringScanner(PATTERN, ENCODING, followLinks, true,
createFileSystemFacade(followLinks, "/one.txt", "/two.txt"));
FileVisitorResult<String> actualResult = scanner.invoke(workspace, null);
assertThat(actualResult.getResults()).containsExactly(CONTENT + 1, CONTENT + 2);
assertThat(actualResult.getLog().getInfoMessages()).containsExactly(
"Searching for all files in '/absolute/path' that match the pattern '**/*.txt'",
"Traversing of symbolic links: " + message,
"-> found 2 files",
"Successfully processed file '/one.txt'",
"Successfully processed file '/two.txt'");
assertThat(actualResult.getLog().getErrorMessages()).isEmpty();
assertThat(actualResult.hasErrors()).isFalse();
}
@Test
@DisplayName("Should log error for empty or forbidden files")
void shouldLogErrorForEmptyAndForbiddenFiles() {
var fileSystemFacade = createFileSystemFacade(true,
"/one.txt", "/two.txt", "empty.txt", "not-readable.txt");
var empty = workspace.toPath().resolve("empty.txt");
when(fileSystemFacade.resolve(workspace, "empty.txt")).thenReturn(empty);
when(fileSystemFacade.isEmpty(empty)).thenReturn(true);
var notReadable = workspace.toPath().resolve("not-readable.txt");
when(fileSystemFacade.resolve(workspace, "not-readable.txt")).thenReturn(notReadable);
when(fileSystemFacade.isNotReadable(notReadable)).thenReturn(true);
var scanner = new StringScanner(PATTERN, ENCODING, true, true,
fileSystemFacade);
FileVisitorResult<String> actualResult = scanner.invoke(workspace, null);
assertThat(actualResult.getResults()).containsExactly(CONTENT + 1, CONTENT + 2);
assertThat(actualResult.getLog().getInfoMessages()).contains(
"Searching for all files in '/absolute/path' that match the pattern '**/*.txt'",
"-> found 4 files",
"Successfully processed file '/one.txt'",
"Successfully processed file '/two.txt'");
assertThat(actualResult.hasErrors()).isTrue();
assertThat(actualResult.getLog().getErrorMessages()).containsExactly("Errors during parsing",
"Skipping file 'empty.txt' because it's empty",
"Skipping file 'not-readable.txt' because Jenkins has no permission to read the file");
}
@Test
@DisplayName("Should skip logging of errors when parsing empty files")
void shouldSkipLoggingOfErrorsForEmptyFiles() {
var fileSystemFacade = createFileSystemFacade(true,
"/one.txt", "/two.txt", "empty.txt");
var empty = workspace.toPath().resolve("empty.txt");
when(fileSystemFacade.resolve(workspace, "empty.txt")).thenReturn(empty);
when(fileSystemFacade.isEmpty(empty)).thenReturn(true);
var scanner = new StringScanner(PATTERN, ENCODING, true, false,
fileSystemFacade);
FileVisitorResult<String> actualResult = scanner.invoke(workspace, null);
assertThat(actualResult.getResults()).containsExactly(CONTENT + 1, CONTENT + 2);
assertThat(actualResult.getLog().getInfoMessages()).contains(
"Searching for all files in '/absolute/path' that match the pattern '**/*.txt'",
"-> found 3 files",
"Successfully processed file '/one.txt'",
"Successfully processed file '/two.txt'",
"Skipping file 'empty.txt' because it's empty");
assertThat(actualResult.hasErrors()).isFalse();
assertThat(actualResult.getLog().getErrorMessages()).isEmpty();
}
@Test
@DisplayName("Should log error if no results are returned")
void shouldLogErrorIfNoResultIsAvailable() {
var fileSystemFacade = createFileSystemFacade(false, "/one.txt");
var scanner = new EmptyScanner(fileSystemFacade);
FileVisitorResult<String> actualResult = scanner.invoke(workspace, null);
assertThat(actualResult.getResults()).isEmpty();
assertThat(actualResult.getLog().getInfoMessages()).containsExactly(
"Searching for all files in '/absolute/path' that match the pattern '**/*.txt'",
"Traversing of symbolic links: disabled",
"-> found 1 file");
assertThat(actualResult.hasErrors()).isTrue();
assertThat(actualResult.getLog().getErrorMessages()).containsExactly("Errors during parsing",
"No result created for file '/one.txt' due to some errors");
}
private FileSystemFacade createFileSystemFacade(final boolean followLinks, final String... files) {
FileSystemFacade fileSystem = mock(FileSystemFacade.class);
when(fileSystem.getAbsolutePath(any())).thenReturn("/absolute/path");
when(fileSystem.find(PATTERN, followLinks, workspace)).thenReturn(files);
return fileSystem;
}
@Override
protected StringScanner createSerializable() {
return new StringScanner(PATTERN, ENCODING, true, true, createFileSystemFacade(true));
}
static class StringScanner extends AgentFileVisitor<String> {
@Serial
private static final long serialVersionUID = -6902473746775046311L;
private int counter = 1;
@VisibleForTesting
protected StringScanner(final String filePattern, final String encoding, final boolean followSymbolicLinks, final boolean errorOnEmptyFiles, final FileSystemFacade fileSystemFacade) {
super(filePattern, encoding, followSymbolicLinks, errorOnEmptyFiles, fileSystemFacade);
}
@Override
@SuppressWarnings("PMD.AssignmentInOperand")
protected Optional<String> processFile(final Path file, final Charset charset, final FilteredLog log) {
return Optional.of(CONTENT + counter++);
}
@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
@SuppressFBWarnings(value = "EQ_ALWAYS_TRUE", justification = "Required for serializable test")
@Override
public boolean equals(final Object o) {
return true;
}
@Override
public int hashCode() {
return 0;
}
}
private static class EmptyScanner extends AgentFileVisitor<String> {
@Serial
private static final long serialVersionUID = 3700448215163706213L;
@VisibleForTesting
protected EmptyScanner(final FileSystemFacade fileSystemFacade) {
super(PATTERN, ENCODING, false, false, fileSystemFacade);
}
@Override
protected Optional<String> processFile(final Path file, final Charset charset, final FilteredLog log) {
return Optional.empty();
}
@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
@SuppressFBWarnings(value = "EQ_ALWAYS_TRUE", justification = "Required for serializable test")
@Override
public boolean equals(final Object o) {
return true;
}
@Override
public int hashCode() {
return 0;
}
}
}