Files { public static File createDirectory(String absolutePath) { if (absolutePath == null) { return null; } File directory = new File(absolutePath); if (directory.exists() && directory.isDirectory() || directory.mkdirs() && directory.isDirectory()) { return directory; } return null; }  static File createDirectory(String absolutePath); static int intLength(File file);  }
@Test public void createDirectory_shouldReturnNewDirectory() throws Exception { File directory = Files.createDirectory(expectedDirectoryPath); assertThat(directory.exists()).isTrue(); assertThat(directory.isDirectory()).isTrue(); }
@Test public void createDirectory_whenDirectoryAlreadyExists_shouldReturnTheDirectory() throws Exception { expectedDirectory = new File(expectedDirectoryPath); expectedDirectory.mkdirs(); assertThat(expectedDirectory.exists()).isTrue(); assertThat(expectedDirectory.isDirectory()).isTrue(); File directory = Files.createDirectory(expectedDirectoryPath); assertThat(directory.exists()).isTrue(); assertThat(directory.isDirectory()).isTrue(); }
@Test public void createDirectory_whenFileAlreadyExistsButIsNotADirectory_shouldReturnNull() throws Exception { File file = new File(expectedFilePath); file.createNewFile(); assertThat(file.exists()).isTrue(); assertThat(file.isDirectory()).isFalse(); File directory = Files.createDirectory(expectedFilePath); assertThat(directory).isNull(); file.delete(); }
@Test public void createDirectory_whenAbsolutePathIsNull_shouldReturnNull() throws Exception { File directory = Files.createDirectory(null); assertThat(directory).isNull(); }
@Test public void createDirectory_withExternalStoragePath_withoutRelevantPermission_shouldReturnNullAndNotThrowException() throws Exception { String filePath = activity.getExternalCacheDir() + File.separator + "testFile.txt"; File file = new File(filePath); Files.createDirectory(filePath); file.delete(); }