Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up TransformerTest #62

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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