PortablePipelineJarCreator implements PortablePipelineRunner { @VisibleForTesting protected void copyResourcesFromJar(JarFile inputJar) throws IOException { Enumeration<JarEntry> inputJarEntries = inputJar.entries(); Set<String> previousEntryNames = new HashSet<>(ImmutableList.of(JarFile.MANIFEST_NAME)); while (inputJarEntries.hasMoreElements()) { JarEntry inputJarEntry = inputJarEntries.nextElement(); InputStream inputStream = inputJar.getInputStream(inputJarEntry); String entryName = inputJarEntry.getName(); if (previousEntryNames.contains(entryName)) { LOG.debug("Skipping duplicated file {}", entryName); } else { JarEntry outputJarEntry = new JarEntry(inputJarEntry); outputStream.putNextEntry(outputJarEntry); LOG.trace("Copying jar entry {}", inputJarEntry); IOUtils.copy(inputStream, outputStream); previousEntryNames.add(entryName); } } } PortablePipelineJarCreator(Class mainClass); @Override PortablePipelineResult run(Pipeline pipeline, JobInfo jobInfo);  }
@Test public void testCopyResourcesFromJar_copiesResources() throws IOException { List<JarEntry> entries = ImmutableList.of(new JarEntry("foo"), new JarEntry("bar"), new JarEntry("baz")); when(inputJar.entries()).thenReturn(Collections.enumeration(entries)); jarCreator.copyResourcesFromJar(inputJar); verify(outputStream, times(3)).putNextEntry(any()); }
@Test public void testCopyResourcesFromJar_ignoresManifest() throws IOException { List<JarEntry> manifestEntry = ImmutableList.of(new JarEntry(JarFile.MANIFEST_NAME)); when(inputJar.entries()).thenReturn(Collections.enumeration(manifestEntry)); jarCreator.copyResourcesFromJar(inputJar); verify(outputStream, never()).putNextEntry(any()); }
@Test public void testCopyResourcesFromJar_ignoresDuplicates() throws IOException { List<JarEntry> duplicateEntries = ImmutableList.of(new JarEntry("foo"), new JarEntry("foo")); when(inputJar.entries()).thenReturn(Collections.enumeration(duplicateEntries)); jarCreator.copyResourcesFromJar(inputJar); verify(outputStream, times(1)).putNextEntry(any()); }