16
16
import java .io .IOException ;
17
17
import java .nio .ByteBuffer ;
18
18
import java .nio .file .FileAlreadyExistsException ;
19
+ import java .nio .file .FileVisitResult ;
19
20
import java .nio .file .Files ;
20
21
import java .nio .file .NoSuchFileException ;
21
22
import java .nio .file .Path ;
23
+ import java .nio .file .SimpleFileVisitor ;
22
24
import java .nio .file .StandardOpenOption ;
25
+ import java .nio .file .attribute .BasicFileAttributes ;
23
26
import java .util .Set ;
24
27
import java .util .concurrent .TimeoutException ;
25
28
import java .util .stream .Stream ;
@@ -43,12 +46,13 @@ class S3WritableByteChannelTest {
43
46
44
47
@ Test
45
48
@ DisplayName ("when file exists and constructor is invoked with option `CREATE_NEW` should throw FileAlreadyExistsException" )
46
- void whenFileExistsAndCreateNewShouldThrowFileAlreadyExistsException () throws InterruptedException , TimeoutException {
49
+ void whenFileExistsAndCreateNewShouldThrowFileAlreadyExistsException () throws InterruptedException , TimeoutException , IOException {
47
50
S3FileSystemProvider provider = mock ();
48
51
when (provider .exists (any (S3AsyncClient .class ), any ())).thenReturn (true );
49
52
50
53
S3FileSystem fs = mock ();
51
54
when (fs .provider ()).thenReturn (provider );
55
+ when (fs .createTempFile (any (S3Path .class ))).thenReturn (Files .createTempFile ("" , "" ));
52
56
53
57
var file = S3Path .getPath (fs , "somefile" );
54
58
assertThatThrownBy (() -> new S3WritableByteChannel (file , mock (), mock (), Set .of (CREATE_NEW )))
@@ -77,6 +81,7 @@ void shouldBeSeekable() throws InterruptedException, TimeoutException, IOExcepti
77
81
78
82
S3FileSystem fs = mock ();
79
83
when (fs .provider ()).thenReturn (provider );
84
+ when (fs .createTempFile (any (S3Path .class ))).thenReturn (Files .createTempFile ("" , "" ));
80
85
81
86
var file = S3Path .getPath (fs , "somefile" );
82
87
var channel = new S3WritableByteChannel (file , mock (), mock (), Set .of (READ , WRITE ));
@@ -103,6 +108,7 @@ void shouldNotThrowWhen(boolean fileExists, Set<StandardOpenOption> openOptions)
103
108
104
109
S3FileSystem fs = mock ();
105
110
when (fs .provider ()).thenReturn (provider );
111
+ when (fs .createTempFile (any (S3Path .class ))).thenReturn (Files .createTempFile ("" , "" ));
106
112
107
113
var file = S3Path .getPath (fs , "somefile" );
108
114
new S3WritableByteChannel (file , mock (), mock (), openOptions ).close ();
@@ -116,6 +122,7 @@ void shouldBeOpenBeforeClose() throws InterruptedException, TimeoutException, IO
116
122
117
123
S3FileSystem fs = mock ();
118
124
when (fs .provider ()).thenReturn (provider );
125
+ when (fs .createTempFile (any (S3Path .class ))).thenReturn (Files .createTempFile ("" , "" ));
119
126
120
127
var file = S3Path .getPath (fs , "somefile" );
121
128
try (var channel = new S3WritableByteChannel (file , mock (), mock (), Set .of (CREATE ))){
@@ -131,6 +138,7 @@ void shouldBeNotOpenAfterClose() throws InterruptedException, TimeoutException,
131
138
132
139
S3FileSystem fs = mock ();
133
140
when (fs .provider ()).thenReturn (provider );
141
+ when (fs .createTempFile (any (S3Path .class ))).thenReturn (Files .createTempFile ("" , "" ));
134
142
135
143
var file = S3Path .getPath (fs , "somefile" );
136
144
var channel = new S3WritableByteChannel (file , mock (), mock (), Set .of (CREATE ));
@@ -143,16 +151,20 @@ void shouldBeNotOpenAfterClose() throws InterruptedException, TimeoutException,
143
151
void tmpFileIsCleanedUpAfterClose (@ TempDir Path tempDir ) throws InterruptedException , TimeoutException , IOException {
144
152
S3FileSystemProvider provider = mock ();
145
153
when (provider .exists (any (S3AsyncClient .class ), any ())).thenReturn (false );
146
- S3FileSystem fs = mock ();
147
- when (fs .provider ()).thenReturn (provider );
148
- var file = S3Path .getPath (fs , "somefile" );
149
-
150
- var channel = new S3WritableByteChannel (file , mock (), mock (), Set .of (CREATE ));
151
-
152
- var countAfterOpening = countTemporaryFiles (tempDir );
153
- channel .close ();
154
- var countAfterClosing = countTemporaryFiles (tempDir );
155
- assertThat (countAfterClosing ).isLessThan (countAfterOpening );
154
+ var fs = new S3FileSystem (provider , null , tempDir );
155
+ var file1 = S3Path .getPath (fs , "file1" );
156
+ var file2 = S3Path .getPath (fs , "dir1/file2" );
157
+ var file3 = S3Path .getPath (fs , "dir1/dir2/file3" );
158
+
159
+ var channel1 = new S3WritableByteChannel (file1 , mock (), mock (), Set .of (CREATE ));
160
+ var channel2 = new S3WritableByteChannel (file2 , mock (), mock (), Set .of (CREATE ));
161
+ var channel3 = new S3WritableByteChannel (file3 , mock (), mock (), Set .of (CREATE ));
162
+
163
+ assertThat (countTemporaryFiles (tempDir )).isEqualTo (3 );
164
+ channel1 .close ();
165
+ channel2 .close ();
166
+ channel3 .close ();
167
+ assertThat (countTemporaryFiles (tempDir )).isZero ();
156
168
}
157
169
158
170
@ Test
@@ -162,6 +174,7 @@ void secondCloseIsNoOp() throws InterruptedException, TimeoutException, IOExcept
162
174
when (provider .exists (any (S3AsyncClient .class ), any ())).thenReturn (false );
163
175
S3FileSystem fs = mock ();
164
176
when (fs .provider ()).thenReturn (provider );
177
+ when (fs .createTempFile (any (S3Path .class ))).thenReturn (Files .createTempFile ("" , "" ));
165
178
var file = S3Path .getPath (fs , "somefile" );
166
179
167
180
S3TransferUtil utilMock = mock ();
@@ -174,11 +187,17 @@ void secondCloseIsNoOp() throws InterruptedException, TimeoutException, IOExcept
174
187
}
175
188
176
189
private long countTemporaryFiles (Path tempDir ) throws IOException {
177
- try (var list = Files .list (tempDir .getParent ())) {
178
- return list
179
- .filter ((path ) -> path .getFileName ().toString ().contains ("aws-s3-nio-" ))
180
- .count ();
181
- }
190
+ var visitor = new SimpleFileVisitor <Path >() {
191
+ int fileCount = 0 ;
192
+
193
+ @ Override
194
+ public FileVisitResult visitFile (Path file , BasicFileAttributes attrs ) throws IOException {
195
+ fileCount ++;
196
+ return super .visitFile (file , attrs );
197
+ }
198
+ };
199
+ Files .walkFileTree (tempDir , visitor );
200
+ return visitor .fileCount ;
182
201
}
183
202
184
203
private Stream <Arguments > acceptedFileExistsAndOpenOptions () {
0 commit comments