Refactor JarPatcherTest to improve readability and conform to coding standards.
ci / image (42.21, public) (push) Successful in 2m30s
ci / image (42.21, public) (push) Successful in 2m30s
This commit is contained in:
1 parent
d0512eb480
commit
bf96980fc1
1 file changed
+68
-28
@@ -36,72 +36,105 @@ import static org.objectweb.asm.Opcodes.NEW;
|
||||
import static org.objectweb.asm.Opcodes.RETURN;
|
||||
import static org.objectweb.asm.Opcodes.V25;
|
||||
|
||||
class JarPatcherTest {
|
||||
class JarPatcherTest
|
||||
{
|
||||
@TempDir
|
||||
Path dir;
|
||||
|
||||
/** Adds {@code static Object pick(boolean)} returning a new A or B; frame computation at the merge point requires Base as common superclass. */
|
||||
private static final ClassPatch ADD_PICK = new ClassPatch() {
|
||||
private static final ClassPatch ADD_PICK = new ClassPatch()
|
||||
{
|
||||
@Override
|
||||
public String target() {
|
||||
return "t/Target";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(ClassNode node) {
|
||||
MethodNode m = new MethodNode(ACC_PUBLIC | ACC_STATIC, "pick", "(Z)Ljava/lang/Object;", null, null);
|
||||
Label elseBranch = new Label();
|
||||
Label end = new Label();
|
||||
public void apply(ClassNode node)
|
||||
{
|
||||
MethodNode m = new MethodNode(ACC_PUBLIC | ACC_STATIC, "pick", "(Z)Ljava/lang/Object;", null, null);
|
||||
Label elseBranch = new Label();
|
||||
Label end = new Label();
|
||||
|
||||
m.visitVarInsn(ILOAD, 0);
|
||||
m.visitJumpInsn(IFEQ, elseBranch);
|
||||
|
||||
newInstance(m, "t/A");
|
||||
|
||||
m.visitJumpInsn(GOTO, end);
|
||||
m.visitLabel(elseBranch);
|
||||
|
||||
newInstance(m, "t/B");
|
||||
|
||||
m.visitLabel(end);
|
||||
m.visitInsn(ARETURN);
|
||||
|
||||
node.methods.add(m);
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
|
||||
@Test
|
||||
void patchesTargetAndCopiesEverythingElse() throws Exception {
|
||||
void patchesTargetAndCopiesEverythingElse() throws Exception
|
||||
{
|
||||
Path in = jar(Map.of(
|
||||
"t/Base.class", classBytes("t/Base", "java/lang/Object"),
|
||||
"t/A.class", classBytes("t/A", "t/Base"),
|
||||
"t/B.class", classBytes("t/B", "t/Base"),
|
||||
"t/Target.class", classBytes("t/Target", "java/lang/Object"),
|
||||
"media/readme.txt", "untouched".getBytes()));
|
||||
"t/Base.class", classBytes("t/Base", "java/lang/Object"),
|
||||
"t/A.class", classBytes("t/A", "t/Base"),
|
||||
"t/B.class", classBytes("t/B", "t/Base"),
|
||||
"t/Target.class", classBytes("t/Target", "java/lang/Object"),
|
||||
"media/readme.txt", "untouched".getBytes()
|
||||
));
|
||||
|
||||
Path out = dir.resolve("out/patched.jar");
|
||||
|
||||
assertEquals(1, new JarPatcher(List.of(ADD_PICK)).patch(in, out));
|
||||
|
||||
try (JarFile original = new JarFile(in.toFile()); JarFile patched = new JarFile(out.toFile())) {
|
||||
for (String name : List.of("t/Base.class", "t/A.class", "t/B.class", "media/readme.txt")) {
|
||||
assertArrayEquals(read(original, name), read(patched, name), name);
|
||||
try (JarFile original = new JarFile(in.toFile()); JarFile patched = new JarFile(out.toFile()))
|
||||
{
|
||||
for (String name : List.of("t/Base.class", "t/A.class", "t/B.class", "media/readme.txt"))
|
||||
{
|
||||
assertArrayEquals(
|
||||
read(original, name),
|
||||
read(patched, name),
|
||||
name
|
||||
);
|
||||
}
|
||||
}
|
||||
try (URLClassLoader loader = new URLClassLoader(new URL[]{out.toUri().toURL()}, null)) {
|
||||
|
||||
try (URLClassLoader loader = new URLClassLoader(new URL[]{out.toUri().toURL()}, null))
|
||||
{
|
||||
Class<?> target = loader.loadClass("t.Target");
|
||||
Object picked = target.getMethod("pick", boolean.class).invoke(null, true);
|
||||
Object picked = target.getMethod("pick", boolean.class).invoke(null, true);
|
||||
|
||||
assertEquals("t.A", picked.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsWhenTargetIsMissing() throws IOException {
|
||||
void failsWhenTargetIsMissing() throws IOException
|
||||
{
|
||||
Path in = jar(Map.of("t/Other.class", classBytes("t/Other", "java/lang/Object")));
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class,
|
||||
() -> new JarPatcher(List.of(ADD_PICK)).patch(in, dir.resolve("out.jar")));
|
||||
assertTrue(e.getMessage().contains("t/Target.class not found"), e.getMessage());
|
||||
|
||||
IllegalStateException e = assertThrows(
|
||||
IllegalStateException.class,
|
||||
() -> new JarPatcher(List.of(ADD_PICK)).patch(in, dir.resolve("out.jar"))
|
||||
);
|
||||
|
||||
assertTrue(e.getMessage().contains("'t/Target.class' not found"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsWhenTargetHasMultiReleaseOverride() throws IOException {
|
||||
void failsWhenTargetHasMultiReleaseOverride() throws IOException
|
||||
{
|
||||
byte[] target = classBytes("t/Target", "java/lang/Object");
|
||||
Path in = jar(Map.of("t/Target.class", target, "META-INF/versions/21/t/Target.class", target));
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class,
|
||||
() -> new JarPatcher(List.of(ADD_PICK)).patch(in, dir.resolve("out.jar")));
|
||||
Path in = jar(Map.of("t/Target.class", target, "META-INF/versions/21/t/Target.class", target));
|
||||
|
||||
IllegalStateException e = assertThrows(
|
||||
IllegalStateException.class,
|
||||
() -> new JarPatcher(List.of(ADD_PICK)).patch(in, dir.resolve("out.jar"))
|
||||
);
|
||||
|
||||
assertTrue(e.getMessage().contains("overrides a patched class"), e.getMessage());
|
||||
}
|
||||
|
||||
@@ -111,27 +144,34 @@ class JarPatcherTest {
|
||||
m.visitMethodInsn(INVOKESPECIAL, type, "<init>", "()V", false);
|
||||
}
|
||||
|
||||
private static byte[] classBytes(String name, String superName) {
|
||||
private static byte[] classBytes(String name, String superName)
|
||||
{
|
||||
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
|
||||
cw.visit(V25, ACC_PUBLIC, name, null, superName, null);
|
||||
|
||||
MethodVisitor init = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
|
||||
init.visitVarInsn(ALOAD, 0);
|
||||
init.visitMethodInsn(INVOKESPECIAL, superName, "<init>", "()V", false);
|
||||
init.visitInsn(RETURN);
|
||||
init.visitMaxs(0, 0);
|
||||
|
||||
cw.visitEnd();
|
||||
return cw.toByteArray();
|
||||
}
|
||||
|
||||
private Path jar(Map<String, byte[]> entries) throws IOException {
|
||||
private Path jar(Map<String, byte[]> entries) throws IOException
|
||||
{
|
||||
Path jar = Files.createTempFile(dir, "in", ".jar");
|
||||
try (ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(jar))) {
|
||||
|
||||
try (ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(jar)))
|
||||
{
|
||||
for (Map.Entry<String, byte[]> e : entries.entrySet()) {
|
||||
zip.putNextEntry(new ZipEntry(e.getKey()));
|
||||
zip.write(e.getValue());
|
||||
zip.closeEntry();
|
||||
}
|
||||
}
|
||||
|
||||
return jar;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user