JarContentsManager { public List<String> findFiles(File jar, String baseDirectoryName, String fileName) { requireFileExistence(jar); Objects.requireNonNull(baseDirectoryName); Objects.requireNonNull(fileName); try (JarFile jarFile = new JarFile(jar, false)) { return jarFile.stream().filter(entry -> !entry.isDirectory()) .map(ZipEntry::getName) .filter(path -> path.startsWith(baseDirectoryName)) .filter(path -> path .endsWith(JAR_PATH_SEPARATOR + fileName)) .collect(Collectors.toList()); } catch (IOException e) { throw new UncheckedIOException(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 findFiles_directoryInsteadOfJar() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(String.format( "Expect '%s' to be an existing file", testDirectory.getRoot())); jarContentsManager.findFiles(testDirectory.getRoot(), "test", "test"); }
@Test public void findFiles_notAJarFile() throws IOException { File testFile = testDirectory.newFile("test"); expectedException.expect(UncheckedIOException.class); expectedException .expectMessage("java.util.zip.ZipException: zip file is empty"); jarContentsManager.findFiles(testFile, "test", "test"); }
@Test public void findFiles_nonExistingJarFile() { File test = new File("test"); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage( String.format("Expect '%s' to be an existing file", test)); jarContentsManager.findFiles(test, "test", "test"); }
@Test public void findFiles_nonExistingFile() { List<String> result = jarContentsManager.findFiles(testJar, "blah", "nope"); assertTrue("Expect to have empty results for non-existing file", result.isEmpty()); }
@Test public void findFiles_existingFiles() { String resourceName = "vaadin-charts-webjar-6.0.0-alpha3.jar"; String searchName = "bower.json"; List<String> bowerJsons = jarContentsManager .findFiles(TestUtils.getTestJar(resourceName), "", searchName); assertEquals( String.format("Expect '%s' WebJar to contain two '%s' files", resourceName, searchName), 2, bowerJsons.size()); assertTrue(String.format( "Expect all found paths to end with the file name searched for: '%s'", searchName), bowerJsons.stream() .allMatch(path -> path.endsWith('/' + searchName))); }
@Test public void findFiles_existingFiles_baseDirectoryMatters() { String resourceName = "vaadin-charts-webjar-6.0.0-alpha3.jar"; String testPath = "META-INF/resources/webjars/highcharts/5.0.14/"; String searchName = "bower.json"; List<String> bowerJson = jarContentsManager.findFiles( TestUtils.getTestJar(resourceName), testPath, searchName); assertEquals(String.format( "Expect '%s' WebJar to contain one '%s' file in directory '%s'", resourceName, searchName, testPath), 1, bowerJson.size()); }