7272import static org .hamcrest .Matchers .contains ;
7373import static org .hamcrest .Matchers .containsInAnyOrder ;
7474import static org .mockito .ArgumentMatchers .any ;
75- import static org .mockito .ArgumentMatchers .isA ;
7675import static org .mockito .Mockito .doAnswer ;
7776import static org .mockito .Mockito .doNothing ;
7877import static org .mockito .Mockito .doThrow ;
@@ -959,6 +958,7 @@ public void copyDirectory() throws IOException {
959958 when (physicalFsProv .newFileChannel (Mockito .eq (ciphertextDestinationDirFile ), Mockito .any (), any (FileAttribute [].class ))).thenReturn (ciphertextTargetDirDirFileFileChannel );
960959 when (cryptoPathMapper .getCiphertextFileType (cleartextSource )).thenReturn (CiphertextFileType .DIRECTORY );
961960 when (cryptoPathMapper .getCiphertextFileType (cleartextDestination )).thenThrow (NoSuchFileException .class );
961+ when (physicalFsProv .exists (ciphertextTargetParent )).thenReturn (true );
962962 Mockito .doThrow (new NoSuchFileException ("ciphertextDestinationDirFile" )).when (physicalFsProv ).checkAccess (ciphertextDestinationFile );
963963
964964 inTest .copy (cleartextSource , cleartextDestination );
@@ -1007,6 +1007,7 @@ public void moveDirectoryCopyBasicAttributes() throws IOException {
10071007 when (physicalFsProv .readAttributes (Mockito .same (ciphertextSourceDir ), Mockito .same (BasicFileAttributes .class ), any (LinkOption [].class ))).thenReturn (srcAttrs );
10081008 when (physicalFsProv .getFileAttributeView (Mockito .same (ciphertextDestinationDir ), Mockito .same (BasicFileAttributeView .class ), any (LinkOption [].class ))).thenReturn (dstAttrView );
10091009 when (physicalFsProv .newFileChannel (Mockito .same (ciphertextDestinationDirFile ), Mockito .anySet (), any (FileAttribute [].class ))).thenReturn (ciphertextTargetDirDirFileFileChannel );
1010+ when (physicalFsProv .exists (ciphertextTargetParent )).thenReturn (true );
10101011
10111012 inTest .copy (cleartextSource , cleartextDestination , StandardCopyOption .COPY_ATTRIBUTES );
10121013
@@ -1027,6 +1028,7 @@ public void moveDirectoryCopyFileOwnerAttributes() throws IOException {
10271028 when (physicalFsProv .getFileAttributeView (Mockito .same (ciphertextSourceDir ), Mockito .same (FileOwnerAttributeView .class ), any (LinkOption [].class ))).thenReturn (srcAttrsView );
10281029 when (physicalFsProv .getFileAttributeView (Mockito .same (ciphertextDestinationDir ), Mockito .same (FileOwnerAttributeView .class ), any (LinkOption [].class ))).thenReturn (dstAttrView );
10291030 when (physicalFsProv .newFileChannel (Mockito .same (ciphertextDestinationDirFile ), Mockito .anySet (), any (FileAttribute [].class ))).thenReturn (ciphertextTargetDirDirFileFileChannel );
1031+ when (physicalFsProv .exists (ciphertextTargetParent )).thenReturn (true );
10301032
10311033 inTest .copy (cleartextSource , cleartextDestination , StandardCopyOption .COPY_ATTRIBUTES );
10321034
@@ -1050,6 +1052,7 @@ public void moveDirectoryCopyPosixAttributes() throws IOException {
10501052 when (physicalFsProv .readAttributes (Mockito .same (ciphertextSourceDir ), Mockito .same (PosixFileAttributes .class ), any (LinkOption [].class ))).thenReturn (srcAttrs );
10511053 when (physicalFsProv .getFileAttributeView (Mockito .same (ciphertextDestinationDir ), Mockito .same (PosixFileAttributeView .class ), any (LinkOption [].class ))).thenReturn (dstAttrView );
10521054 when (physicalFsProv .newFileChannel (Mockito .same (ciphertextDestinationDirFile ), Mockito .anySet (), any (FileAttribute [].class ))).thenReturn (ciphertextTargetDirDirFileFileChannel );
1055+ when (physicalFsProv .exists (ciphertextTargetParent )).thenReturn (true );
10531056
10541057 inTest .copy (cleartextSource , cleartextDestination , StandardCopyOption .COPY_ATTRIBUTES );
10551058
@@ -1073,6 +1076,7 @@ public void moveDirectoryCopyDosAttributes() throws IOException {
10731076 when (physicalFsProv .readAttributes (Mockito .same (ciphertextSourceDir ), Mockito .same (DosFileAttributes .class ), any (LinkOption [].class ))).thenReturn (srcAttrs );
10741077 when (physicalFsProv .getFileAttributeView (Mockito .same (ciphertextDestinationDir ), Mockito .same (DosFileAttributeView .class ), any (LinkOption [].class ))).thenReturn (dstAttrView );
10751078 when (physicalFsProv .newFileChannel (Mockito .same (ciphertextDestinationDirFile ), Mockito .anySet (), any (FileAttribute [].class ))).thenReturn (ciphertextTargetDirDirFileFileChannel );
1079+ when (physicalFsProv .exists (ciphertextTargetParent )).thenReturn (true );
10761080
10771081 inTest .copy (cleartextSource , cleartextDestination , StandardCopyOption .COPY_ATTRIBUTES );
10781082
@@ -1168,6 +1172,7 @@ public void createDirectoryIfPathCiphertextFileDoesExistThrowsFileAlreadyExcepti
11681172 when (cryptoPathMapper .getCiphertextDir (parent )).thenReturn (new CiphertextDirectory ("foo" , ciphertextParent ));
11691173 when (ciphertextParent .getFileSystem ()).thenReturn (fileSystem );
11701174 doThrow (new FileAlreadyExistsException (path .toString ())).when (cryptoPathMapper ).assertNonExisting (path );
1175+ when (provider .exists (ciphertextParent )).thenReturn (true );
11711176
11721177 FileAlreadyExistsException e = Assertions .assertThrows (FileAlreadyExistsException .class , () -> {
11731178 inTest .createDirectory (path );
@@ -1177,7 +1182,7 @@ public void createDirectoryIfPathCiphertextFileDoesExistThrowsFileAlreadyExcepti
11771182
11781183 @ Test
11791184 public void createDirectoryCreatesDirectoryIfConditonsAreMet () throws IOException {
1180- Path ciphertextParent = mock (Path .class , "ciphertextParent " );
1185+ Path ciphertextParent = mock (Path .class , "d/00/00 " );
11811186 Path ciphertextRawPath = mock (Path .class , "d/00/00/path.c9r" );
11821187 Path ciphertextDirFile = mock (Path .class , "d/00/00/path.c9r/dir.c9r" );
11831188 Path ciphertextDirPath = mock (Path .class , "d/FF/FF/" );
@@ -1187,7 +1192,7 @@ public void createDirectoryCreatesDirectoryIfConditonsAreMet() throws IOExceptio
11871192 when (ciphertextRawPath .resolve ("dir.c9r" )).thenReturn (ciphertextDirFile );
11881193 when (cryptoPathMapper .getCiphertextFilePath (path )).thenReturn (ciphertextPath );
11891194 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 ));
11911196 when (cryptoPathMapper .getCiphertextFileType (path )).thenThrow (NoSuchFileException .class );
11921197 when (ciphertextPath .getRawPath ()).thenReturn (ciphertextRawPath );
11931198 when (ciphertextPath .getDirFilePath ()).thenReturn (ciphertextDirFile );
@@ -1197,6 +1202,7 @@ public void createDirectoryCreatesDirectoryIfConditonsAreMet() throws IOExceptio
11971202 when (ciphertextDirPath .getFileSystem ()).thenReturn (fileSystem );
11981203 when (ciphertextDirFile .getName (3 )).thenReturn (mock (Path .class , "path.c9r" ));
11991204 when (provider .newFileChannel (ciphertextDirFile , EnumSet .of (StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE ))).thenReturn (channel );
1205+ when (provider .exists (ciphertextParent )).thenReturn (true );
12001206
12011207 inTest .createDirectory (path );
12021208
@@ -1206,7 +1212,7 @@ public void createDirectoryCreatesDirectoryIfConditonsAreMet() throws IOExceptio
12061212
12071213 @ Test
12081214 public void createDirectoryClearsDirIdAndDeletesDirFileIfCreatingDirFails () throws IOException {
1209- Path ciphertextParent = mock (Path .class , "ciphertextParent " );
1215+ Path ciphertextParent = mock (Path .class , "d/00/00 " );
12101216 Path ciphertextRawPath = mock (Path .class , "d/00/00/path.c9r" );
12111217 Path ciphertextDirFile = mock (Path .class , "d/00/00/path.c9r/dir.c9r" );
12121218 Path ciphertextDirPath = mock (Path .class , "d/FF/FF/" );
@@ -1216,7 +1222,7 @@ public void createDirectoryClearsDirIdAndDeletesDirFileIfCreatingDirFails() thro
12161222 when (ciphertextRawPath .resolve ("dir.c9r" )).thenReturn (ciphertextDirFile );
12171223 when (cryptoPathMapper .getCiphertextFilePath (path )).thenReturn (ciphertextPath );
12181224 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 ));
12201226 when (cryptoPathMapper .getCiphertextFileType (path )).thenThrow (NoSuchFileException .class );
12211227 when (ciphertextPath .getRawPath ()).thenReturn (ciphertextRawPath );
12221228 when (ciphertextPath .getDirFilePath ()).thenReturn (ciphertextDirFile );
@@ -1226,15 +1232,18 @@ public void createDirectoryClearsDirIdAndDeletesDirFileIfCreatingDirFails() thro
12261232 when (ciphertextDirPath .getFileSystem ()).thenReturn (fileSystem );
12271233 when (ciphertextDirFile .getName (3 )).thenReturn (mock (Path .class , "path.c9r" ));
12281234 when (provider .newFileChannel (ciphertextDirFile , EnumSet .of (StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE ))).thenReturn (channel );
1235+ when (provider .exists (ciphertextParent )).thenReturn (true );
12291236
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 );
12321240 when (ciphertextDirPath .toAbsolutePath ()).thenReturn (ciphertextDirPath );
12331241 when (ciphertextDirPath .getParent ()).thenReturn (null );
12341242
1235- Assertions .assertThrows (IOException .class , () -> {
1243+ var exception = Assertions .assertThrows (FileAlreadyExistsException .class , () -> {
12361244 inTest .createDirectory (path );
12371245 });
1246+ Assertions .assertEquals ("very specific" , exception .getMessage ());
12381247 verify (readonlyFlag ).assertWritable ();
12391248 verify (provider ).delete (ciphertextDirFile );
12401249 verify (dirIdProvider ).delete (ciphertextDirFile );
@@ -1243,7 +1252,7 @@ public void createDirectoryClearsDirIdAndDeletesDirFileIfCreatingDirFails() thro
12431252
12441253 @ Test
12451254 public void createDirectoryBackupsDirIdInCiphertextDirPath () throws IOException {
1246- Path ciphertextParent = mock (Path .class , "ciphertextParent " );
1255+ Path ciphertextParent = mock (Path .class , "d/00/00 " );
12471256 Path ciphertextRawPath = mock (Path .class , "d/00/00/path.c9r" );
12481257 Path ciphertextDirFile = mock (Path .class , "d/00/00/path.c9r/dir.c9r" );
12491258 Path ciphertextDirPath = mock (Path .class , "d/FF/FF/" );
@@ -1254,7 +1263,7 @@ public void createDirectoryBackupsDirIdInCiphertextDirPath() throws IOException
12541263 when (ciphertextRawPath .resolve ("dir.c9r" )).thenReturn (ciphertextDirFile );
12551264 when (cryptoPathMapper .getCiphertextFilePath (path )).thenReturn (ciphertextPath );
12561265 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 ));
12581267 when (cryptoPathMapper .getCiphertextFileType (path )).thenThrow (NoSuchFileException .class );
12591268 when (ciphertextPath .getRawPath ()).thenReturn (ciphertextRawPath );
12601269 when (ciphertextPath .getDirFilePath ()).thenReturn (ciphertextDirFile );
@@ -1264,6 +1273,7 @@ public void createDirectoryBackupsDirIdInCiphertextDirPath() throws IOException
12641273 when (ciphertextDirPath .getFileSystem ()).thenReturn (fileSystem );
12651274 when (ciphertextDirFile .getName (3 )).thenReturn (mock (Path .class , "path.c9r" ));
12661275 when (provider .newFileChannel (ciphertextDirFile , EnumSet .of (StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE ))).thenReturn (channel );
1276+ when (provider .exists (ciphertextParent )).thenReturn (true );
12671277
12681278 inTest .createDirectory (path );
12691279
0 commit comments