### Question:
JarMerger implements Closeable { public void addDirectory(@NonNull Path directory) throws IOException { addDirectory(directory, filter, null, null); } JarMerger(@NonNull Path jarFile);  JarMerger(@NonNull Path jarFile, @Nullable Predicate<String> filter); void addDirectory(@NonNull Path directory); void addDirectory(
            @NonNull Path directory,
            @Nullable Predicate<String> filterOverride,
            @Nullable Transformer transformer,
            @Nullable Relocator relocator); void addJar(@NonNull Path file); void addJar(
            @NonNull Path file,
            @Nullable Predicate<String> filterOverride,
            @Nullable Relocator relocator); void addFile(@NonNull String entryPath, @NonNull Path file); void addEntry(@NonNull String entryPath, @NonNull InputStream input); @Override void close(); void setManifestProperties(Map<String, String> properties); static final Predicate<String> CLASSES_ONLY; static final Predicate<String> EXCLUDE_CLASSES; static final String MODULE_PATH; static final FileTime ZERO_TIME; }### Answer:
@Test public void basicDirectory() throws Exception { Path out = Jimfs.newFileSystem(Configuration.unix()).getPath("/out/output.jar"); Path dir = createDirectoryWithClassAndResource(); try (JarMerger merger = new JarMerger(out)) { merger.addDirectory(dir); } assertThat(getEntries(out)) .containsExactly( "com/example/MyClass.class", MYCLASS_CONTENT, "resource.txt", RESOURCE_CONTENT) .inOrder(); }
@Test public void basicDirectoryWindows() throws Exception { Path build = Jimfs.newFileSystem(Configuration.windows()).getPath("C:\\src\\build"); Path out = build.resolve("jar.jar"); Path dir = createDirectoryWithClassAndResource(build.resolve("inDirectory")); try (JarMerger merger = new JarMerger(out)) { merger.addDirectory(dir); } assertThat(getEntries(out)) .containsExactly( "com/example/MyClass.class", MYCLASS_CONTENT, "resource.txt", RESOURCE_CONTENT) .inOrder(); }
@Test public void transformerInstrument() throws Exception { Path out = Jimfs.newFileSystem(Configuration.unix()).getPath("/out/output.jar"); JarMerger.Transformer transformer = (path, is) -> { switch (path) { case "com/example/MyClass.class": return new ByteArrayInputStream(REMOVED_TYPEDEFS.data); default: throw new IllegalArgumentException("Unexpected path" + path); } }; try (JarMerger merger = new JarMerger(out, JarMerger.CLASSES_ONLY)) { merger.addDirectory( createDirectoryWithClassAndResource(), JarMerger.CLASSES_ONLY, transformer, null); } assertThat(getEntries(out)).containsExactly("com/example/MyClass.class", REMOVED_TYPEDEFS); }
@Test public void transformerRemove() throws Exception { Path out = Jimfs.newFileSystem(Configuration.unix()).getPath("/out/output.jar"); JarMerger.Transformer transformer = (path, is) -> { switch (path) { case "com/example/MyClass.class": return null; default: throw new IllegalArgumentException("Unexpected path" + path); } }; try (JarMerger merger = new JarMerger(out)) { merger.addDirectory( createDirectoryWithClassAndResource(), JarMerger.CLASSES_ONLY, transformer, null); } assertThat(getEntries(out)).isEmpty(); }