1515import java .io .IOException ;
1616import java .nio .file .Files ;
1717import java .nio .file .Path ;
18+ import java .nio .file .StandardOpenOption ;
1819import java .nio .file .StandardWatchEventKinds ;
1920import java .nio .file .WatchService ;
2021import java .time .Duration ;
@@ -62,7 +63,7 @@ public void afterEach() {
6263 @ DisplayName ("After 5 seconds of token creation, a new file is created" )
6364 public void testFileCreation () {
6465 var filePath = tmpDir .resolve ("inUse.file" );
65- try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , RealUseToken . ActivationType . CREATE , encWrapper )) {
66+ try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , StandardOpenOption . CREATE_NEW , encWrapper )) {
6667 Awaitility .await ().atLeast (FILE_OPERATION_DELAY ).atMost (FILE_OPERATION_MAX ).until (() -> Files .exists (filePath ));
6768 Assertions .assertTrue (Files .exists (filePath ));
6869 }
@@ -77,7 +78,7 @@ public void testFileSteal() throws IOException {
7778 var watchKey = tmpDir .register (watchService , StandardWatchEventKinds .ENTRY_MODIFY );
7879 var fileTime = Files .getLastModifiedTime (filePath );
7980
80- try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , RealUseToken . ActivationType . STEAL , encWrapper )) {
81+ try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , StandardOpenOption . TRUNCATE_EXISTING , encWrapper )) {
8182 Awaitility .await ().atLeast (FILE_OPERATION_DELAY ).atMost (FILE_OPERATION_MAX ).until (() -> fileTime .compareTo (Files .getLastModifiedTime (filePath )) < 0 );
8283 var events = watchKey .pollEvents ();
8384 var createEvent = events .stream ().filter (e -> e .kind ().equals (StandardWatchEventKinds .ENTRY_MODIFY )).findAny ();
@@ -90,28 +91,13 @@ public void testFileSteal() throws IOException {
9091 Assertions .assertNull (useTokens .get (filePath ));
9192 }
9293
93- @ Test
94- @ DisplayName ("Invalid token creation does nothing" )
95- public void testInvalid () throws IOException {
96- var filePath = tmpDir .resolve ("inUse.file" );
97- var watchKey = tmpDir .register (watchService , StandardWatchEventKinds .ENTRY_MODIFY );
98-
99- try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , RealUseToken .ActivationType .NONE , encWrapper )) {
100- Awaitility .await ().pollDelay (FILE_OPERATION_MAX ).timeout (FILE_OPERATION_MAX .multipliedBy (2 )).until (() -> true );
101- Assertions .assertTrue (Files .notExists (filePath ));
102- Assertions .assertTrue (token .isClosed ());
103- Assertions .assertNull (useTokens .get (filePath ));
104- MatcherAssert .assertThat (watchKey .pollEvents (), Matchers .empty ());
105- }
106- }
107-
10894 @ Test
10995 @ DisplayName ("After 5 seconds of token creation, failed steal closes the token " )
11096 public void testFileStealFails () throws IOException {
11197 var filePath = tmpDir .resolve ("inUse.file" ); //file does not exist
11298 var watchKey = tmpDir .register (watchService , StandardWatchEventKinds .ENTRY_CREATE , StandardWatchEventKinds .ENTRY_MODIFY , StandardWatchEventKinds .ENTRY_DELETE );
11399
114- try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , RealUseToken . ActivationType . STEAL , encWrapper )) {
100+ try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , StandardOpenOption . TRUNCATE_EXISTING , encWrapper )) {
115101 Awaitility .await ().atLeast (FILE_OPERATION_DELAY ).atMost (FILE_OPERATION_MAX ).until (token ::isClosed );
116102 Assertions .assertTrue (Files .notExists (filePath ));
117103 Assertions .assertTrue (token .isClosed ());
@@ -126,7 +112,7 @@ public void testTokenCloseBeforeFileOperation() throws IOException {
126112 var filePath = tmpDir .resolve ("inUse.file" );
127113 var watchKey = tmpDir .register (watchService , StandardWatchEventKinds .ENTRY_CREATE , StandardWatchEventKinds .ENTRY_MODIFY , StandardWatchEventKinds .ENTRY_DELETE );
128114
129- try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , RealUseToken . ActivationType . CREATE , encWrapper )) {
115+ try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , StandardOpenOption . CREATE_NEW , encWrapper )) {
130116 Assertions .assertTrue (Files .notExists (filePath ));
131117 }
132118 Awaitility .await ().pollDelay (FILE_OPERATION_MAX ).timeout (FILE_OPERATION_MAX .multipliedBy (2 )).until (() -> true );
@@ -142,7 +128,7 @@ public void testMoveToBefore() throws IOException {
142128 var targetPath = tmpDir .resolve ("inUse2.file" );
143129 var watchKey = tmpDir .register (watchService , StandardWatchEventKinds .ENTRY_CREATE , StandardWatchEventKinds .ENTRY_MODIFY , StandardWatchEventKinds .ENTRY_DELETE );
144130
145- try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , RealUseToken . ActivationType . CREATE , encWrapper )) {
131+ try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , StandardOpenOption . CREATE_NEW , encWrapper )) {
146132 token .moveToInternal (targetPath );
147133
148134 //no file operation after move
@@ -172,7 +158,7 @@ public void testMoveToAfter() {
172158 var filePath = tmpDir .resolve ("inUse.file" );
173159 var targetPath = tmpDir .resolve ("inUse2.file" );
174160
175- try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , RealUseToken . ActivationType . CREATE , encWrapper )) {
161+ try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , StandardOpenOption . CREATE_NEW , encWrapper )) {
176162 Awaitility .await ().atLeast (FILE_OPERATION_DELAY ).atMost (FILE_OPERATION_MAX ).until (() -> Files .exists (filePath ));
177163
178164 token .moveToInternal (targetPath );
@@ -195,7 +181,7 @@ public void testMoveToClosed() throws IOException {
195181 var targetPath = tmpDir .resolve ("inUse2.file" );
196182 var watchKey = tmpDir .register (watchService , StandardWatchEventKinds .ENTRY_CREATE , StandardWatchEventKinds .ENTRY_MODIFY , StandardWatchEventKinds .ENTRY_DELETE );
197183
198- try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , RealUseToken . ActivationType . CREATE , encWrapper )) {
184+ try (var token = new RealUseToken (filePath , "test3000" , cryptor , useTokens , StandardOpenOption . CREATE_NEW , encWrapper )) {
199185 token .close ();
200186 Awaitility .await ().pollDelay (FILE_OPERATION_MAX ).timeout (FILE_OPERATION_MAX .multipliedBy (2 )).until (() -> true );
201187
0 commit comments