WidgetFileHelper { public static void deleteOldConcatenateFiles(Path path, String suffix) { String regex = "^" + FILENAME_PREFIX +"(?:(?!"+ suffix + ").)*\\.min\\.js$"; try (Stream<Path> files = Files.list(path)) { files.filter(p -> p.getFileName().toString().matches(regex) && !Files.isDirectory(p)) .forEach(p ->deleteFile(p)); } catch (IOException e) { throw new GenerationException("Error while filter file in folder " + path.toString(), e); } }  static Path writeFile(byte[] content, Path folderPath, String suffix); static void deleteOldConcatenateFiles(Path path, String suffix); static void deleteConcatenateFile(Path folder);  }
@Test(expected = GenerationException.class) public void should_throw_generation_exception_if_not_exist_folder_path_dont_exist_when_delete_a_file() throws Exception { Path folderNotFound = temporaryFolder.toPath().resolve("folderNotFound"); WidgetFileHelper.deleteOldConcatenateFiles(folderNotFound, "notUsedForThisTest"); }
@Test public void should_delete_files_if_old_files_exists() throws Exception { File assetsFolder = temporaryFolder.newFolder("maPage", "assets"); File expectFileDeleted = temporaryFolder.newFile("maPage/assets/widgets-4576.min.js"); File expectFileAlreadyExist = temporaryFolder.newFile("maPage/assets/widgets-1z2a3456.min.js"); WidgetFileHelper.deleteOldConcatenateFiles(assetsFolder.toPath(), "1z2a3456"); assertThat(expectFileDeleted).doesNotExist(); assertThat(expectFileAlreadyExist).exists(); }
@Test public void should_delete_on_folder_all_old_widgets_directives_file() throws Exception { File assetsFolder = temporaryFolder.newFolder("maPage", "assets"); File expectToBeDeletedFile = temporaryFolder.newFile("maPage/assets/widgets-fdsf45741sf.min.js"); File fragment = temporaryFolder.newFile("maPage/assets/123456.js"); File expectExistFile = temporaryFolder.newFile("maPage/assets/12345654.json"); WidgetFileHelper.deleteOldConcatenateFiles(assetsFolder.toPath(), "aa"); assertThat(expectToBeDeletedFile).doesNotExist(); assertThat(expectExistFile).exists(); assertThat(fragment).exists(); }
@Test public void should_delete_on_root_folder_all_old_widgets_directives_file() throws Exception { File assetsFolder = temporaryFolder.newFolder("myFragmentId"); File fragmentJS = temporaryFolder.newFile("myFragmentId/myFragmentId.js"); File oldConcatDirectiveFile = temporaryFolder.newFile("myFragmentId/widgets-11111.min.js"); File descriptorFile = temporaryFolder.newFile("myFragmentId/myFragmentId.json"); WidgetFileHelper.deleteOldConcatenateFiles(assetsFolder.toPath(), "123"); assertThat(fragmentJS).exists(); assertThat(descriptorFile).exists(); assertThat(oldConcatDirectiveFile).doesNotExist(); }