3434
3535import java .io .IOException ;
3636import java .nio .file .Files ;
37- import java .nio .file .Path ;
3837import java .nio .file .attribute .FileTime ;
3938import java .time .Instant ;
40- import java .util .ArrayList ;
41- import java .util .Arrays ;
42- import java .util .Collections ;
43- import java .util .List ;
4439import java .util .concurrent .atomic .AtomicBoolean ;
45- import java .util .function .Consumer ;
46- import java .util .function .Predicate ;
47- import java .util .stream .Stream ;
4840
4941import org .awaitility .Awaitility ;
5042import org .junit .jupiter .api .AfterEach ;
@@ -134,40 +126,38 @@ void singleFileThatMonitorsOnlyADirectory() throws IOException, InterruptedExcep
134126
135127 @ Test
136128 void noRescanOnOverflow () throws IOException , InterruptedException {
137- var bookkeeper = new Bookkeeper ();
129+ var bookkeeper = new TestHelper . Bookkeeper ();
138130 try (var watch = startWatchAndTriggerOverflow (Approximation .NONE , bookkeeper )) {
139131 Thread .sleep (TestHelper .SHORT_WAIT .toMillis ());
140132
141133 await ("Overflow shouldn't trigger created, modified, or deleted events" )
142- .until (() -> bookkeeper .fullPaths ( CREATED , MODIFIED , DELETED ).count () == 0 );
134+ .until (() -> bookkeeper .events (). kind ( CREATED , MODIFIED , DELETED ).none () );
143135 await ("Overflow should be visible to user-defined event handler" )
144- .until (() -> bookkeeper .fullPaths ( OVERFLOW ).count () == 1 );
136+ .until (() -> bookkeeper .events (). kind ( OVERFLOW ).any () );
145137 }
146138 }
147139
148140 @ Test
149141 void memorylessRescanOnOverflow () throws IOException , InterruptedException {
150- var bookkeeper = new Bookkeeper ();
142+ var bookkeeper = new TestHelper . Bookkeeper ();
151143 try (var watch = startWatchAndTriggerOverflow (Approximation .ALL , bookkeeper )) {
152144 Thread .sleep (TestHelper .SHORT_WAIT .toMillis ());
153145
154- var isFile = Predicate . isEqual ( watch .getPath ());
155- var isNotFile = Predicate . not ( isFile );
146+ var fileName = watch .getPath (). getFileName ( );
147+ var parent = watch . getPath (). getParent ( );
156148
157- await ("Overflow should trigger created event for `file `" )
158- .until (() -> bookkeeper .fullPaths ( CREATED ).filter ( isFile ). count () == 1 );
149+ await ("Overflow should trigger created event for `" + fileName + " `" )
150+ .until (() -> bookkeeper .events (). kind ( CREATED ).rootPath ( parent ). relativePath ( fileName ). any () );
159151 await ("Overflow shouldn't trigger created events for other files" )
160- .until (() -> bookkeeper .fullPaths (CREATED ).filter (isNotFile ).count () == 0 );
161- await ("Overflow shouldn't trigger modified events (`file` is empty)" )
162- .until (() -> bookkeeper .fullPaths (MODIFIED ).count () == 0 );
163- await ("Overflow shouldn't trigger deleted events" )
164- .until (() -> bookkeeper .fullPaths (DELETED ).count () == 0 );
152+ .until (() -> bookkeeper .events ().kind (CREATED ).rootPath (parent ).relativePathNot (fileName ).none ());
153+ await ("Overflow shouldn't trigger modified or deleted events" )
154+ .until (() -> bookkeeper .events ().kind (MODIFIED , DELETED ).none ());
165155 await ("Overflow should be visible to user-defined event handler" )
166- .until (() -> bookkeeper .fullPaths ( OVERFLOW ).count () == 1 );
156+ .until (() -> bookkeeper .events (). kind ( OVERFLOW ).any () );
167157 }
168158 }
169159
170- private ActiveWatch startWatchAndTriggerOverflow (Approximation whichFiles , Bookkeeper bookkeeper ) throws IOException {
160+ private ActiveWatch startWatchAndTriggerOverflow (Approximation whichFiles , TestHelper . Bookkeeper bookkeeper ) throws IOException {
171161 var parent = testDir .getTestDirectory ();
172162 var file = parent .resolve ("a.txt" );
173163
@@ -181,22 +171,4 @@ private ActiveWatch startWatchAndTriggerOverflow(Approximation whichFiles, Bookk
181171 ((EventHandlingWatch ) watch ).handleEvent (overflow );
182172 return watch ;
183173 }
184-
185- private static class Bookkeeper implements Consumer <WatchEvent > {
186- private final List <WatchEvent > events = Collections .synchronizedList (new ArrayList <>());
187-
188- public Stream <WatchEvent > events (WatchEvent .Kind ... kinds ) {
189- var list = Arrays .asList (kinds .length == 0 ? WatchEvent .Kind .values () : kinds );
190- return events .stream ().filter (e -> list .contains (e .getKind ()));
191- }
192-
193- public Stream <Path > fullPaths (WatchEvent .Kind ... kinds ) {
194- return events (kinds ).map (WatchEvent ::calculateFullPath );
195- }
196-
197- @ Override
198- public void accept (WatchEvent e ) {
199- events .add (e );
200- }
201- }
202174}
0 commit comments