FileManager { public static byte[] readBytes(@NonNull File file) { byte[] fileContents = new byte[(int) file.length()]; try { FileInputStream fileStream = new FileInputStream(file); try { DataInputStream dataInputStream = new DataInputStream(fileStream); dataInputStream.readFully(fileContents); return fileContents; } finally { fileStream.close(); } } catch (IOException e) { AppCenterLog.error(AppCenter.LOG_TAG, "Could not read file " + file.getAbsolutePath(), e); } return null; }  static synchronized void initialize(Context context); @SuppressWarnings("SameParameterValue") static String read(@NonNull String path); static String read(@NonNull File file); static byte[] readBytes(@NonNull File file); static void write(@NonNull String path, @NonNull String contents); static void write(@NonNull File file, @NonNull String contents); @SuppressWarnings("WeakerAccess") @NonNull static String[] getFilenames(@NonNull String path, @Nullable FilenameFilter filter); @SuppressWarnings("WeakerAccess") @Nullable static File lastModifiedFile(@NonNull String path, @Nullable FilenameFilter filter); @Nullable static File lastModifiedFile(@NonNull File dir, @Nullable FilenameFilter filter); static boolean delete(@NonNull String path); static boolean delete(@NonNull File file); static boolean deleteDirectory(@NonNull File file); static void cleanDirectory(@NonNull File directory); @SuppressWarnings("ResultOfMethodCallIgnored") static void mkdir(@NonNull String path);  }
@Test public void readBytesError() throws Exception { mockStatic(AppCenterLog.class); FileInputStream fileInputStream = mock(FileInputStream.class); whenNew(FileInputStream.class).withAnyArguments().thenReturn(fileInputStream); DataInputStream dataInputStream = mock(DataInputStream.class); whenNew(DataInputStream.class).withAnyArguments().thenReturn(dataInputStream); doThrow(new IOException("mock")).when(dataInputStream).readFully(any(byte[].class)); assertNull(FileManager.readBytes(new File(""))); verify(fileInputStream).close(); verifyStatic(); AppCenterLog.error(anyString(), anyString(), any(IOException.class)); }
@Test public void readBytesErrorAndCloseError() throws Exception { mockStatic(AppCenterLog.class); FileInputStream fileInputStream = mock(FileInputStream.class); whenNew(FileInputStream.class).withAnyArguments().thenReturn(fileInputStream); doThrow(new IOException("mock close")).when(fileInputStream).close(); DataInputStream dataInputStream = mock(DataInputStream.class); whenNew(DataInputStream.class).withAnyArguments().thenReturn(dataInputStream); doThrow(new IOException("mock")).when(dataInputStream).readFully(any(byte[].class)); assertNull(FileManager.readBytes(new File(""))); verify(fileInputStream).close(); verifyStatic(); AppCenterLog.error(anyString(), anyString(), any(IOException.class)); }