Skip to content

Commit

Permalink
Clean up TransformerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
centic9 authored and basil committed Mar 8, 2022
1 parent 3608b22 commit 72733b3
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/test/java/org/kohsuke/file_leak_detector/TransformerTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package org.kohsuke.file_leak_detector;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -10,23 +19,15 @@
import org.kohsuke.file_leak_detector.transform.ClassTransformSpec;
import org.kohsuke.file_leak_detector.transform.TransformerImpl;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertTrue;

/**
* @author Kohsuke Kawaguchi
*/
@RunWith(Parameterized.class)
public class TransformerTest {
List<ClassTransformSpec> specs = AgentMain.createSpec();
private static final List<ClassTransformSpec> specs = AgentMain.createSpec();

private final Class<?> c;

Class<?> c;

public TransformerTest(Class<?> c) {
this.c = c;
}
Expand All @@ -36,30 +37,32 @@ public void testInstrumentations() throws Exception {
TransformerImpl t = new TransformerImpl(specs);

String name = c.getName().replace('.', '/');
byte[] data = IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream(name + ".class"));
byte[] data2 = t.transform(name,data);
InputStream resource = getClass().getClassLoader().getResourceAsStream(name + ".class");
assertNotNull("Could not load " + name + ".class", resource);
byte[] data = IOUtils.toByteArray(resource);
byte[] data2 = t.transform(name, data);

// File classFile = new File("/tmp/" + name + ".class");
// classFile.getParentFile().mkdirs();
// FileOutputStream o = new FileOutputStream(classFile);
// o.write(data2);
// o.close();

String errors;
final String errors;
ClassReader classReader = new ClassReader(data2);
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
CheckClassAdapter.verify(classReader, false, new PrintWriter(baos));
errors = new String(baos.toByteArray(), UTF_8);
}
assertTrue("Verification failed for " + c + "\n" + errors, errors.isEmpty());
}
@Parameters

@Parameters(name = "{index} - {0}")
public static List<Object[]> specs() throws Exception {
List<Object[]> r = new ArrayList<Object[]>();
for (ClassTransformSpec s : AgentMain.createSpec()) {
for (ClassTransformSpec s : specs) {
Class<?> c = TransformerTest.class.getClassLoader().loadClass(s.name.replace('/', '.'));
r.add(new Object[]{c});
r.add(new Object[] {c});
}
return r;
}
Expand Down

0 comments on commit 72733b3

Please sign in to comment.