JarContentsManager { public void copyFilesFromJarTrimmingBasePath(File jar, String jarDirectoryToCopyFrom, File outputDirectory, String... wildcardPathExclusions) { requireFileExistence(jar); if (!Objects.requireNonNull(outputDirectory).isDirectory()) { throw new IllegalArgumentException( String.format("Expect '%s' to be an existing directory", outputDirectory)); } String basePath = normalizeJarBasePath(jarDirectoryToCopyFrom); try (JarFile jarFile = new JarFile(jar, false)) { jarFile.stream().filter(file -> !file.isDirectory()) .filter(file -> file.getName().toLowerCase(Locale.ENGLISH) .startsWith(basePath.toLowerCase(Locale.ENGLISH))) .filter(file -> isFileIncluded(file, wildcardPathExclusions)) .forEach(jarEntry -> copyJarEntryTrimmingBasePath(jarFile, jarEntry, basePath, outputDirectory)); } catch (IOException e) { throw new UncheckedIOException(String.format( "Failed to extract files from jarFile '%s' to directory '%s'", jar, outputDirectory), e); } }  boolean containsPath(File jar, String filePath); byte[] getFileContents(File jar, String filePath); List<String> findFiles(File jar, String baseDirectoryName,
            String fileName); void copyFilesFromJarTrimmingBasePath(File jar,
            String jarDirectoryToCopyFrom, File outputDirectory,
            String... wildcardPathExclusions); void copyIncludedFilesFromJarTrimmingBasePath(File jar,
            String jarDirectoryToCopyFrom, File outputDirectory,
            String... wildcardPathInclusions);  }
@Test public void copyFilesFromJar_nullJarFile() { expectedException.expect(NullPointerException.class); jarContentsManager.copyFilesFromJarTrimmingBasePath(null, null, testDirectory.getRoot()); }
@Test public void copyFilesFromJar_notAJarFile() throws IOException { File testFile = testDirectory.newFile("test"); expectedException.expect(UncheckedIOException.class); expectedException.expectMessage(String.format( "Failed to extract files from jarFile '%s' to directory '%s'", testFile, testDirectory.getRoot())); jarContentsManager.copyFilesFromJarTrimmingBasePath(testFile, null, testDirectory.getRoot()); }
@Test public void copyFilesFromJar_nonExistingJarFile() { File test = new File("test"); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage( String.format("Expect '%s' to be an existing file", test)); jarContentsManager.copyFilesFromJarTrimmingBasePath(test, null, testDirectory.getRoot()); }
@Test public void copyFilesFromJar_directoryInsteadOfJar() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(String.format( "Expect '%s' to be an existing file", testDirectory.getRoot())); jarContentsManager.copyFilesFromJarTrimmingBasePath( testDirectory.getRoot(), null, testDirectory.getRoot()); }
@Test public void copyFilesFromJar_nullOutputDirectory() { expectedException.expect(NullPointerException.class); jarContentsManager.copyFilesFromJarTrimmingBasePath(testJar, null, null); }
@Test public void copyFilesFromJar_fileInsteadOfDirectory() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(String .format("Expect '%s' to be an existing directory", testJar)); jarContentsManager.copyFilesFromJarTrimmingBasePath(testJar, null, testJar); }
@Test public void copyFilesFromJar_noBasePath_noExclusions() { assertThat( "Do not expect any files in temporary directory before the test", TestUtils.listFilesRecursively(testDirectory.getRoot()).size(), is(0)); jarContentsManager.copyFilesFromJarTrimmingBasePath(testJar, null, testDirectory.getRoot()); assertThat("Temporary directory should have files after jar copied", TestUtils.listFilesRecursively(testDirectory.getRoot()).size(), is(not(0))); }
@Test public void copyFilesFromJar_noBasePath_excludeEverything() { jarContentsManager.copyFilesFromJarTrimmingBasePath(testJar, null, testDirectory.getRoot(), "*"); assertThat( "Do not expect any files with filter that excludes everything", TestUtils.listFilesRecursively(testDirectory.getRoot()).size(), is(0)); }
@Test public void copyFilesFromJar_withBasePath_noExclusions() throws IOException { String basePath = "META-INF/maven/"; jarContentsManager.copyFilesFromJarTrimmingBasePath(testJar, basePath, testDirectory.getRoot()); List<String> resultingPaths = TestUtils .listFilesRecursively(testDirectory.getRoot()); assertThat(String.format( "Expect jar '%s' to contain files with base path '%s'", testJar, basePath), resultingPaths.size(), is(not(0))); assertTrue("Resulting paths should not contain base path = " + basePath, resultingPaths.stream() .noneMatch(path -> path.contains(basePath))); }
@Test public void copyFilesFromJar_exclusionsWork() throws IOException { String basePath = "META-INF/maven"; File notFilteredCopyingDirectory = testDirectory .newFolder("notFiltered"); jarContentsManager.copyFilesFromJarTrimmingBasePath(testJar, basePath, notFilteredCopyingDirectory); List<String> notFilteredPaths = TestUtils .listFilesRecursively(notFilteredCopyingDirectory); File filteredCopyingDirectory = testDirectory.newFolder("filtered"); jarContentsManager.copyFilesFromJarTrimmingBasePath(testJar, basePath, filteredCopyingDirectory, "*.xml"); List<String> filteredPaths = TestUtils .listFilesRecursively(filteredCopyingDirectory); assertTrue( "Filtered paths' count should be less than non filtered paths' count", filteredPaths.size() < notFilteredPaths.size()); assertTrue("Not filtered paths should contain xml files", notFilteredPaths.stream() .anyMatch(path -> path.endsWith(".xml"))); assertTrue("Paths with '*.xml' exclusion should not contain xml files", filteredPaths.stream() .noneMatch(path -> path.endsWith(".xml"))); }
@Test public void copyFilesFromJar_basePathAppendedWithTrailingSlash() throws IOException { String basePath1 = "META-INF/maven"; File basePath1Directory = testDirectory.newFolder("basePath1"); jarContentsManager.copyFilesFromJarTrimmingBasePath(testJar, basePath1, basePath1Directory); List<String> basePath1Paths = TestUtils .listFilesRecursively(basePath1Directory); String basePath2 = basePath1 + '/'; File basePath2Directory = testDirectory.newFolder("basePath2"); jarContentsManager.copyFilesFromJarTrimmingBasePath(testJar, basePath2, basePath2Directory); List<String> basePath2Paths = TestUtils .listFilesRecursively(basePath2Directory); assertEquals( "Base path without trailing slash should be treated the same as base path with one", basePath1Paths, basePath2Paths); }
@Test public void copyFilesFromJar_copiedFromBasePathResultsAreContainedInAllPaths() throws IOException { File allFilesDirectory = testDirectory.newFolder("all"); jarContentsManager.copyFilesFromJarTrimmingBasePath(testJar, null, allFilesDirectory); List<String> allPaths = TestUtils .listFilesRecursively(allFilesDirectory); String basePath = "/META-INF/maven"; File filteredFilesDirectory = testDirectory.newFolder("filtered"); jarContentsManager.copyFilesFromJarTrimmingBasePath(testJar, basePath, filteredFilesDirectory); List<String> filteredPaths = TestUtils .listFilesRecursively(filteredFilesDirectory); List<String> filteredPathsPrefixedByBasePath = filteredPaths.stream() .map(path -> basePath + path).collect(Collectors.toList()); assertTrue("Filtered paths' count should be less than all paths' count", filteredPaths.size() < allPaths.size()); assertTrue("base path + filtered path should be contained in all paths", allPaths.containsAll(filteredPathsPrefixedByBasePath)); }
@Test public void copyFilesFromJar_casePreserved() { File outputDirectory = testDirectory.getRoot(); String jarDirectory = "META-INF/resources/webjars/paper-button/2.0.0/.github/"; File testJar = TestUtils.getTestJar("paper-button-2.0.0.jar"); List<String> originalFiles = listFilesInJar(testJar, jarDirectory); jarContentsManager.copyFilesFromJarTrimmingBasePath(testJar, jarDirectory, outputDirectory); Set<String> copiedFiles = new HashSet<>( TestUtils.listFilesRecursively(outputDirectory)); assertEquals(String.format( "Number of files in jar '%s' in jar directory '%s' and number of copied files should match.", testJar, jarDirectory), originalFiles.size(), copiedFiles.size()); copiedFiles.forEach(copiedFile -> assertTrue(String.format( "Failed to find copied file '%s' in files '%s' from jar '%s'", copiedFile, originalFiles, testJar), originalFiles.stream() .anyMatch(file -> file.endsWith(copiedFile)))); }
@Test public void copyFilesFromJar_doNotUpdateFileIfContentIsTheSame() { File outputDirectory = testDirectory.getRoot(); String jarDirectory = "META-INF/resources/webjars/paper-button"; File testJar = TestUtils.getTestJar("paper-button-2.0.0.jar"); File jsonFile = copyFilesFromJar(outputDirectory, jarDirectory, testJar); long timestamp = System.currentTimeMillis(); Assert.assertTrue(FileUtils.isFileOlder(jsonFile, timestamp)); jarContentsManager.copyFilesFromJarTrimmingBasePath(testJar, jarDirectory, outputDirectory); Assert.assertTrue(FileUtils.isFileOlder(jsonFile, timestamp)); }
@Test public void copyFilesFromJar_updateFileIfContentIsNotTheSame() throws IOException { File outputDirectory = testDirectory.getRoot(); String jarDirectory = "META-INF/resources/webjars/paper-button"; File testJar = TestUtils.getTestJar("paper-button-2.0.0.jar"); File jsonFile = copyFilesFromJar(outputDirectory, jarDirectory, testJar); String originalContent = FileUtils .readLines(jsonFile, StandardCharsets.UTF_8).stream() .collect(Collectors.joining("")); String content = "{}"; FileUtils.write(jsonFile, content, StandardCharsets.UTF_8); jarContentsManager.copyFilesFromJarTrimmingBasePath(testJar, jarDirectory, outputDirectory); Assert.assertNotEquals(content, FileUtils.readLines(jsonFile, StandardCharsets.UTF_8).stream() .collect(Collectors.joining(""))); Assert.assertEquals(originalContent, FileUtils.readLines(jsonFile, StandardCharsets.UTF_8).stream() .collect(Collectors.joining(""))); }