72
72
import static org .hamcrest .Matchers .contains ;
73
73
import static org .hamcrest .Matchers .containsInAnyOrder ;
74
74
import static org .mockito .ArgumentMatchers .any ;
75
- import static org .mockito .ArgumentMatchers .isA ;
76
75
import static org .mockito .Mockito .doAnswer ;
77
76
import static org .mockito .Mockito .doNothing ;
78
77
import static org .mockito .Mockito .doThrow ;
@@ -959,6 +958,7 @@ public void copyDirectory() throws IOException {
959
958
when (physicalFsProv .newFileChannel (Mockito .eq (ciphertextDestinationDirFile ), Mockito .any (), any (FileAttribute [].class ))).thenReturn (ciphertextTargetDirDirFileFileChannel );
960
959
when (cryptoPathMapper .getCiphertextFileType (cleartextSource )).thenReturn (CiphertextFileType .DIRECTORY );
961
960
when (cryptoPathMapper .getCiphertextFileType (cleartextDestination )).thenThrow (NoSuchFileException .class );
961
+ when (physicalFsProv .exists (ciphertextTargetParent )).thenReturn (true );
962
962
Mockito .doThrow (new NoSuchFileException ("ciphertextDestinationDirFile" )).when (physicalFsProv ).checkAccess (ciphertextDestinationFile );
963
963
964
964
inTest .copy (cleartextSource , cleartextDestination );
@@ -1007,6 +1007,7 @@ public void moveDirectoryCopyBasicAttributes() throws IOException {
1007
1007
when (physicalFsProv .readAttributes (Mockito .same (ciphertextSourceDir ), Mockito .same (BasicFileAttributes .class ), any (LinkOption [].class ))).thenReturn (srcAttrs );
1008
1008
when (physicalFsProv .getFileAttributeView (Mockito .same (ciphertextDestinationDir ), Mockito .same (BasicFileAttributeView .class ), any (LinkOption [].class ))).thenReturn (dstAttrView );
1009
1009
when (physicalFsProv .newFileChannel (Mockito .same (ciphertextDestinationDirFile ), Mockito .anySet (), any (FileAttribute [].class ))).thenReturn (ciphertextTargetDirDirFileFileChannel );
1010
+ when (physicalFsProv .exists (ciphertextTargetParent )).thenReturn (true );
1010
1011
1011
1012
inTest .copy (cleartextSource , cleartextDestination , StandardCopyOption .COPY_ATTRIBUTES );
1012
1013
@@ -1027,6 +1028,7 @@ public void moveDirectoryCopyFileOwnerAttributes() throws IOException {
1027
1028
when (physicalFsProv .getFileAttributeView (Mockito .same (ciphertextSourceDir ), Mockito .same (FileOwnerAttributeView .class ), any (LinkOption [].class ))).thenReturn (srcAttrsView );
1028
1029
when (physicalFsProv .getFileAttributeView (Mockito .same (ciphertextDestinationDir ), Mockito .same (FileOwnerAttributeView .class ), any (LinkOption [].class ))).thenReturn (dstAttrView );
1029
1030
when (physicalFsProv .newFileChannel (Mockito .same (ciphertextDestinationDirFile ), Mockito .anySet (), any (FileAttribute [].class ))).thenReturn (ciphertextTargetDirDirFileFileChannel );
1031
+ when (physicalFsProv .exists (ciphertextTargetParent )).thenReturn (true );
1030
1032
1031
1033
inTest .copy (cleartextSource , cleartextDestination , StandardCopyOption .COPY_ATTRIBUTES );
1032
1034
@@ -1050,6 +1052,7 @@ public void moveDirectoryCopyPosixAttributes() throws IOException {
1050
1052
when (physicalFsProv .readAttributes (Mockito .same (ciphertextSourceDir ), Mockito .same (PosixFileAttributes .class ), any (LinkOption [].class ))).thenReturn (srcAttrs );
1051
1053
when (physicalFsProv .getFileAttributeView (Mockito .same (ciphertextDestinationDir ), Mockito .same (PosixFileAttributeView .class ), any (LinkOption [].class ))).thenReturn (dstAttrView );
1052
1054
when (physicalFsProv .newFileChannel (Mockito .same (ciphertextDestinationDirFile ), Mockito .anySet (), any (FileAttribute [].class ))).thenReturn (ciphertextTargetDirDirFileFileChannel );
1055
+ when (physicalFsProv .exists (ciphertextTargetParent )).thenReturn (true );
1053
1056
1054
1057
inTest .copy (cleartextSource , cleartextDestination , StandardCopyOption .COPY_ATTRIBUTES );
1055
1058
@@ -1073,6 +1076,7 @@ public void moveDirectoryCopyDosAttributes() throws IOException {
1073
1076
when (physicalFsProv .readAttributes (Mockito .same (ciphertextSourceDir ), Mockito .same (DosFileAttributes .class ), any (LinkOption [].class ))).thenReturn (srcAttrs );
1074
1077
when (physicalFsProv .getFileAttributeView (Mockito .same (ciphertextDestinationDir ), Mockito .same (DosFileAttributeView .class ), any (LinkOption [].class ))).thenReturn (dstAttrView );
1075
1078
when (physicalFsProv .newFileChannel (Mockito .same (ciphertextDestinationDirFile ), Mockito .anySet (), any (FileAttribute [].class ))).thenReturn (ciphertextTargetDirDirFileFileChannel );
1079
+ when (physicalFsProv .exists (ciphertextTargetParent )).thenReturn (true );
1076
1080
1077
1081
inTest .copy (cleartextSource , cleartextDestination , StandardCopyOption .COPY_ATTRIBUTES );
1078
1082
@@ -1168,6 +1172,7 @@ public void createDirectoryIfPathCiphertextFileDoesExistThrowsFileAlreadyExcepti
1168
1172
when (cryptoPathMapper .getCiphertextDir (parent )).thenReturn (new CiphertextDirectory ("foo" , ciphertextParent ));
1169
1173
when (ciphertextParent .getFileSystem ()).thenReturn (fileSystem );
1170
1174
doThrow (new FileAlreadyExistsException (path .toString ())).when (cryptoPathMapper ).assertNonExisting (path );
1175
+ when (provider .exists (ciphertextParent )).thenReturn (true );
1171
1176
1172
1177
FileAlreadyExistsException e = Assertions .assertThrows (FileAlreadyExistsException .class , () -> {
1173
1178
inTest .createDirectory (path );
@@ -1177,7 +1182,7 @@ public void createDirectoryIfPathCiphertextFileDoesExistThrowsFileAlreadyExcepti
1177
1182
1178
1183
@ Test
1179
1184
public void createDirectoryCreatesDirectoryIfConditonsAreMet () throws IOException {
1180
- Path ciphertextParent = mock (Path .class , "ciphertextParent " );
1185
+ Path ciphertextParent = mock (Path .class , "d/00/00 " );
1181
1186
Path ciphertextRawPath = mock (Path .class , "d/00/00/path.c9r" );
1182
1187
Path ciphertextDirFile = mock (Path .class , "d/00/00/path.c9r/dir.c9r" );
1183
1188
Path ciphertextDirPath = mock (Path .class , "d/FF/FF/" );
@@ -1187,7 +1192,7 @@ public void createDirectoryCreatesDirectoryIfConditonsAreMet() throws IOExceptio
1187
1192
when (ciphertextRawPath .resolve ("dir.c9r" )).thenReturn (ciphertextDirFile );
1188
1193
when (cryptoPathMapper .getCiphertextFilePath (path )).thenReturn (ciphertextPath );
1189
1194
when (cryptoPathMapper .getCiphertextDir (path )).thenReturn (new CiphertextDirectory (dirId , ciphertextDirPath ));
1190
- when (cryptoPathMapper .getCiphertextDir (parent )).thenReturn (new CiphertextDirectory ("parentDirId" , ciphertextDirPath ));
1195
+ when (cryptoPathMapper .getCiphertextDir (parent )).thenReturn (new CiphertextDirectory ("parentDirId" , ciphertextParent ));
1191
1196
when (cryptoPathMapper .getCiphertextFileType (path )).thenThrow (NoSuchFileException .class );
1192
1197
when (ciphertextPath .getRawPath ()).thenReturn (ciphertextRawPath );
1193
1198
when (ciphertextPath .getDirFilePath ()).thenReturn (ciphertextDirFile );
@@ -1197,6 +1202,7 @@ public void createDirectoryCreatesDirectoryIfConditonsAreMet() throws IOExceptio
1197
1202
when (ciphertextDirPath .getFileSystem ()).thenReturn (fileSystem );
1198
1203
when (ciphertextDirFile .getName (3 )).thenReturn (mock (Path .class , "path.c9r" ));
1199
1204
when (provider .newFileChannel (ciphertextDirFile , EnumSet .of (StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE ))).thenReturn (channel );
1205
+ when (provider .exists (ciphertextParent )).thenReturn (true );
1200
1206
1201
1207
inTest .createDirectory (path );
1202
1208
@@ -1206,7 +1212,7 @@ public void createDirectoryCreatesDirectoryIfConditonsAreMet() throws IOExceptio
1206
1212
1207
1213
@ Test
1208
1214
public void createDirectoryClearsDirIdAndDeletesDirFileIfCreatingDirFails () throws IOException {
1209
- Path ciphertextParent = mock (Path .class , "ciphertextParent " );
1215
+ Path ciphertextParent = mock (Path .class , "d/00/00 " );
1210
1216
Path ciphertextRawPath = mock (Path .class , "d/00/00/path.c9r" );
1211
1217
Path ciphertextDirFile = mock (Path .class , "d/00/00/path.c9r/dir.c9r" );
1212
1218
Path ciphertextDirPath = mock (Path .class , "d/FF/FF/" );
@@ -1216,7 +1222,7 @@ public void createDirectoryClearsDirIdAndDeletesDirFileIfCreatingDirFails() thro
1216
1222
when (ciphertextRawPath .resolve ("dir.c9r" )).thenReturn (ciphertextDirFile );
1217
1223
when (cryptoPathMapper .getCiphertextFilePath (path )).thenReturn (ciphertextPath );
1218
1224
when (cryptoPathMapper .getCiphertextDir (path )).thenReturn (new CiphertextDirectory (dirId , ciphertextDirPath ));
1219
- when (cryptoPathMapper .getCiphertextDir (parent )).thenReturn (new CiphertextDirectory ("parentDirId" , ciphertextDirPath ));
1225
+ when (cryptoPathMapper .getCiphertextDir (parent )).thenReturn (new CiphertextDirectory ("parentDirId" , ciphertextParent ));
1220
1226
when (cryptoPathMapper .getCiphertextFileType (path )).thenThrow (NoSuchFileException .class );
1221
1227
when (ciphertextPath .getRawPath ()).thenReturn (ciphertextRawPath );
1222
1228
when (ciphertextPath .getDirFilePath ()).thenReturn (ciphertextDirFile );
@@ -1226,15 +1232,18 @@ public void createDirectoryClearsDirIdAndDeletesDirFileIfCreatingDirFails() thro
1226
1232
when (ciphertextDirPath .getFileSystem ()).thenReturn (fileSystem );
1227
1233
when (ciphertextDirFile .getName (3 )).thenReturn (mock (Path .class , "path.c9r" ));
1228
1234
when (provider .newFileChannel (ciphertextDirFile , EnumSet .of (StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE ))).thenReturn (channel );
1235
+ when (provider .exists (ciphertextParent )).thenReturn (true );
1229
1236
1230
- // make createDirectory with an FileSystemException during Files.createDirectories(ciphertextDirPath)
1231
- doThrow (new IOException ()).when (provider ).createDirectory (ciphertextDirPath );
1237
+ // make createDirectory with an FileSystemException during Files.createDirectories(ciphertextContentDir)
1238
+ doThrow (new IOException ()).when (provider ).readAttributesIfExists (ciphertextDirPath , BasicFileAttributes .class );
1239
+ doThrow (new FileAlreadyExistsException ("very specific" )).when (provider ).createDirectory (ciphertextDirPath );
1232
1240
when (ciphertextDirPath .toAbsolutePath ()).thenReturn (ciphertextDirPath );
1233
1241
when (ciphertextDirPath .getParent ()).thenReturn (null );
1234
1242
1235
- Assertions .assertThrows (IOException .class , () -> {
1243
+ var exception = Assertions .assertThrows (FileAlreadyExistsException .class , () -> {
1236
1244
inTest .createDirectory (path );
1237
1245
});
1246
+ Assertions .assertEquals ("very specific" , exception .getMessage ());
1238
1247
verify (readonlyFlag ).assertWritable ();
1239
1248
verify (provider ).delete (ciphertextDirFile );
1240
1249
verify (dirIdProvider ).delete (ciphertextDirFile );
@@ -1243,7 +1252,7 @@ public void createDirectoryClearsDirIdAndDeletesDirFileIfCreatingDirFails() thro
1243
1252
1244
1253
@ Test
1245
1254
public void createDirectoryBackupsDirIdInCiphertextDirPath () throws IOException {
1246
- Path ciphertextParent = mock (Path .class , "ciphertextParent " );
1255
+ Path ciphertextParent = mock (Path .class , "d/00/00 " );
1247
1256
Path ciphertextRawPath = mock (Path .class , "d/00/00/path.c9r" );
1248
1257
Path ciphertextDirFile = mock (Path .class , "d/00/00/path.c9r/dir.c9r" );
1249
1258
Path ciphertextDirPath = mock (Path .class , "d/FF/FF/" );
@@ -1254,7 +1263,7 @@ public void createDirectoryBackupsDirIdInCiphertextDirPath() throws IOException
1254
1263
when (ciphertextRawPath .resolve ("dir.c9r" )).thenReturn (ciphertextDirFile );
1255
1264
when (cryptoPathMapper .getCiphertextFilePath (path )).thenReturn (ciphertextPath );
1256
1265
when (cryptoPathMapper .getCiphertextDir (path )).thenReturn (cipherDirObject );
1257
- when (cryptoPathMapper .getCiphertextDir (parent )).thenReturn (new CiphertextDirectory ("parentDirId" , ciphertextDirPath ));
1266
+ when (cryptoPathMapper .getCiphertextDir (parent )).thenReturn (new CiphertextDirectory ("parentDirId" , ciphertextParent ));
1258
1267
when (cryptoPathMapper .getCiphertextFileType (path )).thenThrow (NoSuchFileException .class );
1259
1268
when (ciphertextPath .getRawPath ()).thenReturn (ciphertextRawPath );
1260
1269
when (ciphertextPath .getDirFilePath ()).thenReturn (ciphertextDirFile );
@@ -1264,6 +1273,7 @@ public void createDirectoryBackupsDirIdInCiphertextDirPath() throws IOException
1264
1273
when (ciphertextDirPath .getFileSystem ()).thenReturn (fileSystem );
1265
1274
when (ciphertextDirFile .getName (3 )).thenReturn (mock (Path .class , "path.c9r" ));
1266
1275
when (provider .newFileChannel (ciphertextDirFile , EnumSet .of (StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE ))).thenReturn (channel );
1276
+ when (provider .exists (ciphertextParent )).thenReturn (true );
1267
1277
1268
1278
inTest .createDirectory (path );
1269
1279
0 commit comments