S3FileObject extends S3CommonFileObject { @Override protected void handleAttachException( String key, String bucket ) throws FileSystemException { SimpleEntry<String, String> newPath = fixFilePath( key, bucket ); String keyWithDelimiter = newPath.getKey() + DELIMITER; try { s3Object = getS3Object( keyWithDelimiter, newPath.getValue() ); s3ObjectMetadata = s3Object.getObjectMetadata(); injectType( FileType.FOLDER ); } catch ( AmazonS3Exception e2 ) { ListObjectsRequest listObjectsRequest = new ListObjectsRequest() .withBucketName( newPath.getValue() ) .withPrefix( keyWithDelimiter ) .withDelimiter( DELIMITER ); ObjectListing ol = fileSystem.getS3Client().listObjects( listObjectsRequest ); if ( !( ol.getCommonPrefixes().isEmpty() && ol.getObjectSummaries().isEmpty() ) ) { injectType( FileType.FOLDER ); } else { String errorCode = e2.getErrorCode(); if ( !errorCode.equals( "NoSuchKey" ) ) { logger.error( "Could not get information on " + getQualifiedName(), e2 ); throw new FileSystemException( "vfs.provider/get-type.error", getQualifiedName(), e2 ); } } } } protected  S3FileObject( final AbstractFileName name, final S3FileSystem fileSystem );   }
@Test public void testHandleAttachException() throws FileSystemException { String testKey = BUCKET_NAME + "/" + origKey; String testBucket = "badBucketName"; AmazonS3Exception exception = new AmazonS3Exception("NoSuchKey"); when( s3ServiceMock.getObject( BUCKET_NAME, origKey + "/" ) ).thenThrow( exception ); try { s3FileObjectFileSpy.handleAttachException( testKey, testBucket ); } catch ( FileSystemException e ) { fail( "Caught exception " + e.getMessage() ); } assertEquals( FileType.FOLDER, s3FileObjectFileSpy.getType() ); }
@Test public void testHandleAttachExceptionEmptyFolder() throws FileSystemException { String testKey = BUCKET_NAME + "/" + origKey; String testBucket = "badBucketName"; AmazonS3Exception exception = new AmazonS3Exception("NoSuchKey"); exception.setErrorCode("NoSuchKey"); when( s3ServiceMock.getObject( BUCKET_NAME, origKey + "/" ) ).thenThrow( exception ); childObjectListing = mock( ObjectListing.class ); when( childObjectListing.getObjectSummaries() ).thenReturn( new ArrayList<>() ); when( childObjectListing.getCommonPrefixes() ).thenReturn( new ArrayList<>() ); when( s3ServiceMock.listObjects( any( ListObjectsRequest.class ) ) ).thenReturn( childObjectListing ); try { s3FileObjectFileSpy.handleAttachException( testKey, testBucket ); } catch ( FileSystemException e ) { fail( "Caught exception " + e.getMessage() ); } assertEquals( FileType.IMAGINARY, s3FileObjectFileSpy.getType() ); }