Skip to content

Commit

Permalink
feat(objectionary#226): add DummyStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed May 16, 2024
1 parent 1c879ca commit 108a5d9
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 7 deletions.
27 changes: 24 additions & 3 deletions src/main/java/org/eolang/opeo/SelectiveDecompiler.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.eolang.opeo;


import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.eolang.opeo.ast.OpcodeName;
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;

Expand All @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/eolang/opeo/storage/DummyStorage.java
Original file line number Diff line number Diff line change
@@ -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<XmirEntry> all() {
return Stream.empty();
}

@Override
public void save(final XmirEntry xmir) {
Logger.debug(this, "Dummy storage: skip %s", xmir);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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)
);
}

}

0 comments on commit 108a5d9

Please sign in to comment.