1
1
/*
2
- * Copyright 2012-2020 the original author or authors.
2
+ * Copyright 2012-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
23
23
import java .util .ArrayList ;
24
24
import java .util .Collections ;
25
25
import java .util .HashSet ;
26
- import java .util .LinkedHashSet ;
27
26
import java .util .List ;
28
27
import java .util .Set ;
29
28
import java .util .UUID ;
29
+ import java .util .stream .Collectors ;
30
30
31
31
import org .junit .jupiter .api .BeforeEach ;
32
32
import org .junit .jupiter .api .Test ;
44
44
* Tests for {@link FileSystemWatcher}.
45
45
*
46
46
* @author Phillip Webb
47
+ * @author Andy Wilkinson
47
48
*/
48
49
class FileSystemWatcherTests {
49
50
@@ -120,19 +121,17 @@ void addFile() throws Exception {
120
121
File directory = startWithNewDirectory ();
121
122
File file = touch (new File (directory , "test.txt" ));
122
123
this .watcher .stopAfter (1 );
123
- ChangedFiles changedFiles = getSingleChangedFiles ();
124
124
ChangedFile expected = new ChangedFile (directory , file , Type .ADD );
125
- assertThat (changedFiles . getFiles ()).contains (expected );
125
+ assertThat (getAllFileChanges ()).containsExactly (expected );
126
126
}
127
127
128
128
@ Test
129
129
void addNestedFile () throws Exception {
130
130
File directory = startWithNewDirectory ();
131
131
File file = touch (new File (new File (directory , "sub" ), "text.txt" ));
132
132
this .watcher .stopAfter (1 );
133
- ChangedFiles changedFiles = getSingleChangedFiles ();
134
133
ChangedFile expected = new ChangedFile (directory , file , Type .ADD );
135
- assertThat (changedFiles . getFiles ()).contains (expected );
134
+ assertThat (getAllFileChanges ()).containsExactly (expected );
136
135
}
137
136
138
137
@ Test
@@ -144,9 +143,8 @@ void createSourceDirectoryAndAddFile() throws IOException {
144
143
directory .mkdirs ();
145
144
File file = touch (new File (directory , "text.txt" ));
146
145
this .watcher .stopAfter (1 );
147
- ChangedFiles changedFiles = getSingleChangedFiles ();
148
146
ChangedFile expected = new ChangedFile (directory , file , Type .ADD );
149
- assertThat (changedFiles . getFiles ()).contains (expected );
147
+ assertThat (getAllFileChanges ()).containsExactly (expected );
150
148
}
151
149
152
150
@ Test
@@ -171,8 +169,7 @@ void waitsForQuietPeriod() throws Exception {
171
169
Thread .sleep (10 );
172
170
}
173
171
this .watcher .stopAfter (1 );
174
- ChangedFiles changedFiles = getSingleChangedFiles ();
175
- assertThat (changedFiles .getFiles ()).hasSize (100 );
172
+ assertThat (getAllFileChanges ()).hasSize (100 );
176
173
}
177
174
178
175
@ Test
@@ -184,9 +181,8 @@ void withExistingFiles() throws Exception {
184
181
this .watcher .start ();
185
182
File file = touch (new File (directory , "test2.txt" ));
186
183
this .watcher .stopAfter (1 );
187
- ChangedFiles changedFiles = getSingleChangedFiles ();
188
184
ChangedFile expected = new ChangedFile (directory , file , Type .ADD );
189
- assertThat (changedFiles . getFiles ()).contains (expected );
185
+ assertThat (getAllFileChanges ()).contains (expected );
190
186
}
191
187
192
188
@ Test
@@ -201,7 +197,7 @@ void multipleSources() throws Exception {
201
197
File file1 = touch (new File (directory1 , "test.txt" ));
202
198
File file2 = touch (new File (directory2 , "test.txt" ));
203
199
this .watcher .stopAfter (1 );
204
- Set <ChangedFiles > change = getSingleOnChange ( );
200
+ Set <ChangedFiles > change = this . changes . stream (). flatMap ( Set < ChangedFiles >:: stream ). collect ( Collectors . toSet () );
205
201
assertThat (change .size ()).isEqualTo (2 );
206
202
for (ChangedFiles changedFiles : change ) {
207
203
if (changedFiles .getSourceDirectory ().equals (directory1 )) {
@@ -219,16 +215,16 @@ void multipleSources() throws Exception {
219
215
void multipleListeners () throws Exception {
220
216
File directory = new File (this .tempDir , UUID .randomUUID ().toString ());
221
217
directory .mkdir ();
222
- final Set <ChangedFiles > listener2Changes = new LinkedHashSet <>();
218
+ final List < Set <ChangedFiles >> listener2Changes = new ArrayList <>();
223
219
this .watcher .addSourceDirectory (directory );
224
- this .watcher .addListener (listener2Changes ::addAll );
220
+ this .watcher .addListener (listener2Changes ::add );
225
221
this .watcher .start ();
226
222
File file = touch (new File (directory , "test.txt" ));
227
223
this .watcher .stopAfter (1 );
228
- ChangedFiles changedFiles = getSingleChangedFiles ();
229
224
ChangedFile expected = new ChangedFile (directory , file , Type .ADD );
230
- assertThat (changedFiles .getFiles ()).contains (expected );
231
- assertThat (listener2Changes ).isEqualTo (this .changes .get (0 ));
225
+ Set <ChangedFile > changeSet = getAllFileChanges ();
226
+ assertThat (changeSet ).contains (expected );
227
+ assertThat (getAllFileChanges (listener2Changes )).isEqualTo (changeSet );
232
228
}
233
229
234
230
@ Test
@@ -243,8 +239,7 @@ void modifyDeleteAndAdd() throws Exception {
243
239
delete .delete ();
244
240
File add = touch (new File (directory , "add.txt" ));
245
241
this .watcher .stopAfter (1 );
246
- ChangedFiles changedFiles = getSingleChangedFiles ();
247
- Set <ChangedFile > actual = changedFiles .getFiles ();
242
+ Set <ChangedFile > actual = getAllFileChanges ();
248
243
Set <ChangedFile > expected = new HashSet <>();
249
244
expected .add (new ChangedFile (directory , modify , Type .MODIFY ));
250
245
expected .add (new ChangedFile (directory , delete , Type .DELETE ));
@@ -266,8 +261,7 @@ void withTriggerFilter() throws Exception {
266
261
assertThat (this .changes ).isEmpty ();
267
262
FileCopyUtils .copy ("abc" .getBytes (), trigger );
268
263
this .watcher .stopAfter (1 );
269
- ChangedFiles changedFiles = getSingleChangedFiles ();
270
- Set <ChangedFile > actual = changedFiles .getFiles ();
264
+ Set <ChangedFile > actual = getAllFileChanges ();
271
265
Set <ChangedFile > expected = new HashSet <>();
272
266
expected .add (new ChangedFile (directory , file , Type .MODIFY ));
273
267
assertThat (actual ).isEqualTo (expected );
@@ -286,15 +280,13 @@ private File startWithNewDirectory() throws IOException {
286
280
return directory ;
287
281
}
288
282
289
- private ChangedFiles getSingleChangedFiles () {
290
- Set <ChangedFiles > singleChange = getSingleOnChange ();
291
- assertThat (singleChange ).hasSize (1 );
292
- return singleChange .iterator ().next ();
283
+ private Set <ChangedFile > getAllFileChanges () {
284
+ return getAllFileChanges (this .changes );
293
285
}
294
286
295
- private Set <ChangedFiles > getSingleOnChange ( ) {
296
- assertThat ( this . changes ). hasSize ( 1 );
297
- return this . changes . get ( 0 );
287
+ private Set <ChangedFile > getAllFileChanges ( List < Set < ChangedFiles >> changes ) {
288
+ return changes . stream (). flatMap ( Set < ChangedFiles >:: stream )
289
+ . flatMap (( changedFiles ) -> changedFiles . getFiles (). stream ()). collect ( Collectors . toSet () );
298
290
}
299
291
300
292
private File touch (File file ) throws IOException {
0 commit comments