Refactor JarPatcherTest to improve readability and conform to coding standards.
ci / image (42.21, public) (push) Successful in 2m30s

This commit is contained in:
Overlord committed 2026-09-30 16:28:31 +02:00
1 parent d0512eb480
commit bf96980fc1
1 file changed
+59 -19
@@ -36,72 +36,105 @@ import static org.objectweb.asm.Opcodes.NEW;
import static org.objectweb.asm.Opcodes.RETURN; import static org.objectweb.asm.Opcodes.RETURN;
import static org.objectweb.asm.Opcodes.V25; import static org.objectweb.asm.Opcodes.V25;
class JarPatcherTest { class JarPatcherTest
{
@TempDir @TempDir
Path dir; 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. */ /** 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 @Override
public String target() { public String target() {
return "t/Target"; return "t/Target";
} }
@Override @Override
public void apply(ClassNode node) { public void apply(ClassNode node)
{
MethodNode m = new MethodNode(ACC_PUBLIC | ACC_STATIC, "pick", "(Z)Ljava/lang/Object;", null, null); MethodNode m = new MethodNode(ACC_PUBLIC | ACC_STATIC, "pick", "(Z)Ljava/lang/Object;", null, null);
Label elseBranch = new Label(); Label elseBranch = new Label();
Label end = new Label(); Label end = new Label();
m.visitVarInsn(ILOAD, 0); m.visitVarInsn(ILOAD, 0);
m.visitJumpInsn(IFEQ, elseBranch); m.visitJumpInsn(IFEQ, elseBranch);
newInstance(m, "t/A"); newInstance(m, "t/A");
m.visitJumpInsn(GOTO, end); m.visitJumpInsn(GOTO, end);
m.visitLabel(elseBranch); m.visitLabel(elseBranch);
newInstance(m, "t/B"); newInstance(m, "t/B");
m.visitLabel(end); m.visitLabel(end);
m.visitInsn(ARETURN); m.visitInsn(ARETURN);
node.methods.add(m); node.methods.add(m);
} }
}; };
//
@Test @Test
void patchesTargetAndCopiesEverythingElse() throws Exception { void patchesTargetAndCopiesEverythingElse() throws Exception
{
Path in = jar(Map.of( Path in = jar(Map.of(
"t/Base.class", classBytes("t/Base", "java/lang/Object"), "t/Base.class", classBytes("t/Base", "java/lang/Object"),
"t/A.class", classBytes("t/A", "t/Base"), "t/A.class", classBytes("t/A", "t/Base"),
"t/B.class", classBytes("t/B", "t/Base"), "t/B.class", classBytes("t/B", "t/Base"),
"t/Target.class", classBytes("t/Target", "java/lang/Object"), "t/Target.class", classBytes("t/Target", "java/lang/Object"),
"media/readme.txt", "untouched".getBytes())); "media/readme.txt", "untouched".getBytes()
));
Path out = dir.resolve("out/patched.jar"); Path out = dir.resolve("out/patched.jar");
assertEquals(1, new JarPatcher(List.of(ADD_PICK)).patch(in, out)); assertEquals(1, new JarPatcher(List.of(ADD_PICK)).patch(in, out));
try (JarFile original = new JarFile(in.toFile()); JarFile patched = new JarFile(out.toFile())) { 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); 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"); 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()); assertEquals("t.A", picked.getClass().getName());
} }
} }
@Test @Test
void failsWhenTargetIsMissing() throws IOException { void failsWhenTargetIsMissing() throws IOException
{
Path in = jar(Map.of("t/Other.class", classBytes("t/Other", "java/lang/Object"))); 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"))); IllegalStateException e = assertThrows(
assertTrue(e.getMessage().contains("t/Target.class not found"), e.getMessage()); 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 @Test
void failsWhenTargetHasMultiReleaseOverride() throws IOException { void failsWhenTargetHasMultiReleaseOverride() throws IOException
{
byte[] target = classBytes("t/Target", "java/lang/Object"); 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)); 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"))); 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()); assertTrue(e.getMessage().contains("overrides a patched class"), e.getMessage());
} }
@@ -111,27 +144,34 @@ class JarPatcherTest {
m.visitMethodInsn(INVOKESPECIAL, type, "<init>", "()V", false); 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); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
cw.visit(V25, ACC_PUBLIC, name, null, superName, null); cw.visit(V25, ACC_PUBLIC, name, null, superName, null);
MethodVisitor init = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); MethodVisitor init = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
init.visitVarInsn(ALOAD, 0); init.visitVarInsn(ALOAD, 0);
init.visitMethodInsn(INVOKESPECIAL, superName, "<init>", "()V", false); init.visitMethodInsn(INVOKESPECIAL, superName, "<init>", "()V", false);
init.visitInsn(RETURN); init.visitInsn(RETURN);
init.visitMaxs(0, 0); init.visitMaxs(0, 0);
cw.visitEnd(); cw.visitEnd();
return cw.toByteArray(); 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"); 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()) { for (Map.Entry<String, byte[]> e : entries.entrySet()) {
zip.putNextEntry(new ZipEntry(e.getKey())); zip.putNextEntry(new ZipEntry(e.getKey()));
zip.write(e.getValue()); zip.write(e.getValue());
zip.closeEntry(); zip.closeEntry();
} }
} }
return jar; return jar;
} }