Skip to content

Commit 2cf7c98

Browse files
committed
Add test for map-specialized-methods
1 parent 277e073 commit 2cf7c98

File tree

5 files changed

+157
-30
lines changed

5 files changed

+157
-30
lines changed

enigma-cli/src/test/java/cuchaz/enigma/command/CheckMappingsCommandTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;
55

6-
import java.io.File;
6+
import java.nio.file.Path;
77

8-
public class CheckMappingsCommandTest {
9-
private static final String PACKAGE_ACCESS = "../enigma/build/test-obf/packageAccess.jar";
8+
public class CheckMappingsCommandTest extends CommandTest {
9+
private static final Path JAR = obfJar("packageAccess");
10+
private static final Path WRONG_MAPPINGS = getResource("/packageAccess/wrongMappings");
11+
private static final Path CORRECT_MAPPINGS = getResource("/packageAccess/correctMappings");
1012

1113
@Test
1214
public void testWrong() {
1315
Assertions.assertThrows(IllegalStateException.class, () ->
14-
new CheckMappingsCommand().run(new File(PACKAGE_ACCESS).getAbsolutePath(), new File("src/test/resources" +
15-
"/packageAccess/wrongMappings").getAbsolutePath())
16+
CheckMappingsCommand.run(JAR, WRONG_MAPPINGS)
1617
);
1718
}
1819

1920
@Test
20-
public void testRight() throws Exception {
21-
new CheckMappingsCommand().run(new File(PACKAGE_ACCESS).getAbsolutePath(), new File("src/test/resources" +
22-
"/packageAccess/correctMappings").getAbsolutePath());
21+
public void testRight() {
22+
Assertions.assertDoesNotThrow(() -> CheckMappingsCommand.run(JAR, CORRECT_MAPPINGS));
2323
}
2424
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cuchaz.enigma.command;
2+
3+
import cuchaz.enigma.translation.mapping.EntryMapping;
4+
import cuchaz.enigma.translation.mapping.tree.EntryTree;
5+
import cuchaz.enigma.translation.representation.entry.Entry;
6+
7+
import java.net.URISyntaxException;
8+
import java.nio.file.Path;
9+
10+
public abstract class CommandTest {
11+
public static Path obfJar(String name) {
12+
return Path.of("../enigma/build/test-obf/%s.jar".formatted(name)).toAbsolutePath();
13+
}
14+
15+
public static Path getResource(String name) {
16+
try {
17+
return Path.of(CommandTest.class.getResource(name).toURI()).toAbsolutePath();
18+
} catch (URISyntaxException e) {
19+
throw new RuntimeException(e);
20+
}
21+
}
22+
23+
protected static String getName(EntryTree<EntryMapping> mappings, Entry<?> entry) {
24+
if (!mappings.contains(entry)) {
25+
return null;
26+
}
27+
28+
EntryMapping mapping = mappings.get(entry);
29+
return mapping != null ? mapping.targetName() : null;
30+
}
31+
}

enigma-cli/src/test/java/cuchaz/enigma/command/FillClassMappingsCommandTest.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,20 @@
77
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
88
import cuchaz.enigma.translation.mapping.tree.EntryTree;
99
import cuchaz.enigma.translation.representation.entry.ClassEntry;
10-
import cuchaz.enigma.translation.representation.entry.Entry;
1110
import cuchaz.enigma.translation.representation.entry.FieldEntry;
1211
import cuchaz.enigma.translation.representation.entry.MethodEntry;
1312
import org.junit.jupiter.api.Test;
1413

15-
import java.net.URISyntaxException;
1614
import java.nio.file.Files;
1715
import java.nio.file.Path;
1816

1917
import static org.junit.jupiter.api.Assertions.assertEquals;
2018
import static org.junit.jupiter.api.Assertions.assertNotNull;
2119
import static org.junit.jupiter.api.Assertions.assertNull;
2220

23-
public class FillClassMappingsCommandTest {
24-
private static final Path JAR = Path.of("../enigma/build/test-obf/innerClasses.jar");
25-
private static final Path MAPPINGS;
21+
public class FillClassMappingsCommandTest extends CommandTest {
22+
private static final Path JAR = obfJar("innerClasses");
23+
private static final Path MAPPINGS = getResource("/fillClassMappings/");
2624

2725
private static final ClassEntry A = new ClassEntry("a");
2826
private static final MethodEntry A_METHOD = MethodEntry.parse("a", "a", "()V");
@@ -94,21 +92,4 @@ public void test() throws Exception {
9492
assertNull(getName(result, F_LEVEL_3));
9593
assertNull(getName(result, F_LEVEL_3_FIELD));
9694
}
97-
98-
private static String getName(EntryTree<EntryMapping> mappings, Entry<?> entry) {
99-
if (!mappings.contains(entry)) {
100-
return null;
101-
}
102-
103-
EntryMapping mapping = mappings.get(entry);
104-
return mapping != null ? mapping.targetName() : null;
105-
}
106-
107-
static {
108-
try {
109-
MAPPINGS = Path.of(FillClassMappingsCommandTest.class.getResource("/fillClassMappings/").toURI());
110-
} catch (URISyntaxException e) {
111-
throw new RuntimeException(e);
112-
}
113-
}
11495
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package cuchaz.enigma.command;
2+
3+
import cuchaz.enigma.ProgressListener;
4+
import cuchaz.enigma.translation.mapping.EntryMapping;
5+
import cuchaz.enigma.translation.mapping.serde.MappingFileNameFormat;
6+
import cuchaz.enigma.translation.mapping.serde.MappingFormat;
7+
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
8+
import cuchaz.enigma.translation.mapping.tree.EntryTree;
9+
import cuchaz.enigma.translation.representation.entry.ClassEntry;
10+
import cuchaz.enigma.translation.representation.entry.MethodEntry;
11+
import org.junit.jupiter.api.Test;
12+
13+
import java.nio.file.Files;
14+
import java.nio.file.Path;
15+
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertNotNull;
18+
import static org.junit.jupiter.api.Assertions.assertNull;
19+
20+
public class MapSpecializedMethodsCommandTest extends CommandTest {
21+
private static final Path JAR = obfJar("bridge");
22+
private static final Path MAPPINGS = getResource("/mapSpecializedMethods/");
23+
24+
private static final ClassEntry BASE_CLASS = new ClassEntry("a");
25+
private static final MethodEntry BASE_FOO_1 = MethodEntry.parse("a", "d", "()La;");
26+
private static final MethodEntry BASE_FOO_2 = MethodEntry.parse("a", "a", "(I)La;");
27+
private static final MethodEntry BASE_FOO_3 = MethodEntry.parse("a", "a", "(II)La;");
28+
private static final MethodEntry BASE_BAR_1 = MethodEntry.parse("a", "e", "()La;");
29+
private static final MethodEntry BASE_BAR_2 = MethodEntry.parse("a", "b", "(I)La;");
30+
private static final MethodEntry BASE_BAZ_1 = MethodEntry.parse("a", "c", "(I)La;");
31+
private static final MethodEntry BASE_BAZ_2 = MethodEntry.parse("a", "b", "(II)La;");
32+
private static final ClassEntry OTHER_CLASS = new ClassEntry("b");
33+
private static final MethodEntry OTHER_GET = MethodEntry.parse("b", "a", "()Ljava/lang/Integer;");
34+
private static final MethodEntry OTHER_GET_BRIDGE = MethodEntry.parse("b", "get", "()Ljava/lang/Object;");
35+
private static final MethodEntry OTHER_APPLY = MethodEntry.parse("b", "a", "(Ljava/lang/String;)Ljava/lang/Integer;");
36+
private static final MethodEntry OTHER_APPLY_BRIDGE = MethodEntry.parse("b", "apply", "(Ljava/lang/Object;)Ljava/lang/Object;");
37+
private static final ClassEntry SUB_CLASS = new ClassEntry("c");
38+
private static final MethodEntry SUB_FOO_1 = MethodEntry.parse("c", "f", "()Lc;");
39+
private static final MethodEntry SUB_FOO_1_BRIDGE = MethodEntry.parse("c", "d", "()La;");
40+
private static final MethodEntry SUB_FOO_2 = MethodEntry.parse("c", "d", "(I)Lc;");
41+
private static final MethodEntry SUB_FOO_2_BRIDGE = MethodEntry.parse("c", "a", "(I)La;");
42+
private static final MethodEntry SUB_FOO_3 = MethodEntry.parse("c", "c", "(II)Lc;");
43+
private static final MethodEntry SUB_FOO_3_BRIDGE = MethodEntry.parse("c", "a", "(II)La;");
44+
private static final MethodEntry SUB_BAR_1 = MethodEntry.parse("c", "g", "()Lc;");
45+
private static final MethodEntry SUB_BAR_1_BRIDGE = MethodEntry.parse("c", "e", "()La;");
46+
private static final MethodEntry SUB_BAR_2 = MethodEntry.parse("c", "e", "(I)Lc;");
47+
private static final MethodEntry SUB_BAR_2_BRIDGE = MethodEntry.parse("c", "b", "(I)La;");
48+
private static final MethodEntry SUB_BAZ_1 = MethodEntry.parse("c", "f", "(I)Lc;");
49+
private static final MethodEntry SUB_BAZ_1_BRIDGE = MethodEntry.parse("c", "c", "(I)La;");
50+
private static final MethodEntry SUB_BAZ_2 = MethodEntry.parse("c", "d", "(II)Lc;");
51+
private static final MethodEntry SUB_BAZ_2_BRIDGE = MethodEntry.parse("c", "b", "(II)La;");
52+
private static final ClassEntry INNER_SUB_CLASS = new ClassEntry("c$a");
53+
private static final MethodEntry INNER_SUB_FOO_1_BRIDGE = MethodEntry.parse("c$a", "d", "()La;");
54+
private static final MethodEntry INNER_SUB_FOO_2_BRIDGE = MethodEntry.parse("c$a", "a", "(I)La;");
55+
private static final MethodEntry INNER_SUB_FOO_3_BRIDGE = MethodEntry.parse("c$a", "a", "(II)La;");
56+
private static final MethodEntry INNER_SUB_BAR_1_BRIDGE = MethodEntry.parse("c$a", "e", "()La;");
57+
private static final MethodEntry INNER_SUB_BAR_2_BRIDGE = MethodEntry.parse("c$a", "b", "(I)La;");
58+
private static final MethodEntry INNER_SUB_BAZ_1_BRIDGE = MethodEntry.parse("c$a", "c", "(I)La;");
59+
private static final MethodEntry INNER_SUB_BAZ_2_BRIDGE = MethodEntry.parse("c$a", "b", "(II)La;");
60+
61+
@Test
62+
public void test() throws Exception {
63+
Path resultFile = Files.createTempFile("mapSpecializedMethods", ".mappings");
64+
MapSpecializedMethodsCommand.run(JAR, MappingFormat.ENIGMA_DIRECTORY.name(), MAPPINGS, MappingFormat.ENIGMA_FILE.name(), resultFile);
65+
66+
EntryTree<EntryMapping> result = MappingFormat.ENIGMA_FILE.read(resultFile, ProgressListener.none(), new MappingSaveParameters(MappingFileNameFormat.BY_DEOBF));
67+
68+
assertNotNull(result.findNode(BASE_CLASS));
69+
assertEquals("foo", getName(result, BASE_FOO_1));
70+
assertEquals("foo", getName(result, BASE_FOO_2));
71+
assertEquals("foo", getName(result, BASE_FOO_3));
72+
assertEquals("bar", getName(result, BASE_BAR_1));
73+
assertEquals("bar", getName(result, BASE_BAR_2));
74+
assertEquals("baz", getName(result, BASE_BAZ_1));
75+
assertEquals("baz", getName(result, BASE_BAZ_2));
76+
77+
assertNotNull(result.findNode(OTHER_CLASS));
78+
assertEquals("get", getName(result, OTHER_GET));
79+
assertNull(getName(result, OTHER_GET_BRIDGE));
80+
assertEquals("apply", getName(result, OTHER_APPLY));
81+
assertNull(getName(result, OTHER_APPLY_BRIDGE));
82+
83+
assertNotNull(result.findNode(SUB_CLASS));
84+
assertEquals("foo", getName(result, SUB_FOO_1));
85+
assertNull(getName(result, SUB_FOO_1_BRIDGE));
86+
assertEquals("foo", getName(result, SUB_FOO_2));
87+
assertNull(getName(result, SUB_FOO_2_BRIDGE));
88+
assertEquals("foo", getName(result, SUB_FOO_3));
89+
assertNull(getName(result, SUB_FOO_3_BRIDGE));
90+
assertEquals("bar", getName(result, SUB_BAR_1));
91+
assertNull(getName(result, SUB_BAR_1_BRIDGE));
92+
assertEquals("bar", getName(result, SUB_BAR_2));
93+
assertNull(getName(result, SUB_BAR_2_BRIDGE));
94+
assertEquals("baz", getName(result, SUB_BAZ_1));
95+
assertNull(getName(result, SUB_BAZ_1_BRIDGE));
96+
assertEquals("baz", getName(result, SUB_BAZ_2));
97+
assertNull(getName(result, SUB_BAZ_2_BRIDGE));
98+
99+
assertNull(getName(result, INNER_SUB_FOO_1_BRIDGE));
100+
assertNull(getName(result, INNER_SUB_FOO_2_BRIDGE));
101+
assertNull(getName(result, INNER_SUB_FOO_3_BRIDGE));
102+
assertNull(getName(result, INNER_SUB_BAR_1_BRIDGE));
103+
assertNull(getName(result, INNER_SUB_BAR_2_BRIDGE));
104+
assertNull(getName(result, INNER_SUB_BAZ_1_BRIDGE));
105+
assertNull(getName(result, INNER_SUB_BAZ_2_BRIDGE));
106+
}
107+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CLASS a
2+
METHOD a foo (I)La;
3+
METHOD a foo (II)La;
4+
METHOD b bar (I)La;
5+
METHOD b baz (II)La;
6+
METHOD c baz (I)La;
7+
METHOD d foo ()La;
8+
METHOD e bar ()La;

0 commit comments

Comments
 (0)