diff --git a/src/main/java/org/eolang/opeo/SelectiveDecompiler.java b/src/main/java/org/eolang/opeo/SelectiveDecompiler.java index 35f8c865..f88b177e 100644 --- a/src/main/java/org/eolang/opeo/SelectiveDecompiler.java +++ b/src/main/java/org/eolang/opeo/SelectiveDecompiler.java @@ -1,6 +1,7 @@ package org.eolang.opeo; +import java.nio.file.Path; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -8,6 +9,8 @@ import org.eolang.opeo.decompilation.Decompiler; import org.eolang.opeo.decompilation.handlers.RouterHandler; import org.eolang.opeo.jeo.JeoDecompiler; +import org.eolang.opeo.storage.DummyStorage; +import org.eolang.opeo.storage.FileStorage; import org.eolang.opeo.storage.Storage; import org.eolang.opeo.storage.XmirEntry; @@ -34,12 +37,30 @@ public final class SelectiveDecompiler implements Decompiler { private final String[] supported; - public SelectiveDecompiler(final Storage storage) { - this(storage, new RouterHandler(true).supportedOpcodes()); + + public SelectiveDecompiler(final Path input, final Path output, final Path copy) { + this(input, output, copy, new RouterHandler(false).supportedOpcodes()); + } + + public SelectiveDecompiler(final Path input, final Path output) { + this(input, output, new RouterHandler(false).supportedOpcodes()); + } + + + public SelectiveDecompiler( + final Path input, final Path output, final Path copy, String... supported + ) { + this(new FileStorage(input, output), new FileStorage(copy, copy), supported); + } + + public SelectiveDecompiler( + final Path input, final Path output, String... supported + ) { + this(new FileStorage(input, output), supported); } public SelectiveDecompiler(final Storage storage, String... supported) { - this(storage, storage, supported); + this(storage, new DummyStorage(), supported); } public SelectiveDecompiler( diff --git a/src/main/java/org/eolang/opeo/storage/DummyStorage.java b/src/main/java/org/eolang/opeo/storage/DummyStorage.java new file mode 100644 index 00000000..232e5ea0 --- /dev/null +++ b/src/main/java/org/eolang/opeo/storage/DummyStorage.java @@ -0,0 +1,16 @@ +package org.eolang.opeo.storage; + +import com.jcabi.log.Logger; +import java.util.stream.Stream; + +public final class DummyStorage implements Storage { + @Override + public Stream all() { + return Stream.empty(); + } + + @Override + public void save(final XmirEntry xmir) { + Logger.debug(this, "Dummy storage: skip %s", xmir); + } +} diff --git a/src/test/java/org/eolang/opeo/decompilation/SelectiveDecompilerTest.java b/src/test/java/org/eolang/opeo/decompilation/SelectiveDecompilerTest.java index 9afc2e5e..9ad65735 100644 --- a/src/test/java/org/eolang/opeo/decompilation/SelectiveDecompilerTest.java +++ b/src/test/java/org/eolang/opeo/decompilation/SelectiveDecompilerTest.java @@ -31,7 +31,6 @@ import org.cactoos.io.ResourceOf; import org.eolang.opeo.SelectiveDecompiler; import org.eolang.opeo.decompilation.handlers.RouterHandler; -import org.eolang.opeo.storage.FileStorage; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.hamcrest.io.FileMatchers; @@ -58,7 +57,7 @@ void decompiles(@TempDir final Path temp) throws Exception { Files.createDirectories(input); Files.createDirectories(output); Files.write(input.resolve("Bar.xmir"), known); - new SelectiveDecompiler(new FileStorage(input, output), this.supported).decompile(); + new SelectiveDecompiler(input, output, this.supported).decompile(); MatcherAssert.assertThat( "We expect that decompiler will decompile the input file and store the result into the output folder.", output.resolve("Bar.xmir").toFile(), @@ -79,7 +78,7 @@ void decompilesNothing(@TempDir final Path temp) throws Exception { Files.createDirectories(input); Files.createDirectories(output); Files.write(input.resolve("Bar.xmir"), known); - new SelectiveDecompiler(new FileStorage(input, output)).decompile(); + new SelectiveDecompiler(input, output).decompile(); MatcherAssert.assertThat( "We expect that decompiler will copy the input file and store the result into the output folder.", output.resolve("Bar.xmir").toFile(), @@ -102,7 +101,7 @@ void decompilesOnlySomeFiles(@TempDir final Path temp) throws Exception { Files.createDirectories(output); Files.write(input.resolve("Known.xmir"), known); Files.write(input.resolve("Unknown.xmir"), unknown); - new SelectiveDecompiler(new FileStorage(input, output)).decompile(); + new SelectiveDecompiler(input, output).decompile(); MatcherAssert.assertThat( "We expect that the decompiled file will be changed since the decompiler knows all instructions.", new BytesOf(output.resolve("Known.xmir")).asBytes(), @@ -115,4 +114,48 @@ void decompilesOnlySomeFiles(@TempDir final Path temp) throws Exception { ); } + @Test + void copiesDecompiledFiles(@TempDir final Path temp) throws Exception { + final byte[] known = new BytesOf(new ResourceOf("xmir/Known.xmir")).asBytes(); + final Path input = temp.resolve("input"); + final Path output = temp.resolve("output"); + final Path copy = temp.resolve("copy"); + Files.createDirectories(input); + Files.createDirectories(output); + Files.createDirectories(copy); + Files.write(input.resolve("Known.xmir"), known); + new SelectiveDecompiler(input, output, copy, this.supported).decompile(); + MatcherAssert.assertThat( + "We expect that the decompiled file will be stored in the output folder.", + output.resolve("Known.xmir").toFile(), + FileMatchers.anExistingFile() + ); + MatcherAssert.assertThat( + "We expect that the decompiled file will be stored in the copy folder.", + copy.resolve("Known.xmir").toFile(), + FileMatchers.anExistingFile() + ); + MatcherAssert.assertThat( + "We expect that the decompiled file will be the same in the output and copy folders.", + new BytesOf(output.resolve("Known.xmir")).asBytes(), + Matchers.equalTo(new BytesOf(copy.resolve("Known.xmir")).asBytes()) + ); + } + + @Test + void doesNotCopyDecompiledFiles(@TempDir final Path temp) throws Exception { + final byte[] known = new BytesOf(new ResourceOf("xmir/Known.xmir")).asBytes(); + final Path input = temp.resolve("input"); + final Path output = temp.resolve("output"); + Files.createDirectories(input); + Files.createDirectories(output); + Files.write(input.resolve("Known.xmir"), known); + new SelectiveDecompiler(input, output, this.supported).decompile(); + MatcherAssert.assertThat( + "We expect that nothing additional will be stored in the folder instead of 'input' and 'output' folders.", + temp.toFile().listFiles(), + Matchers.arrayWithSize(2) + ); + } + }