I am trying to analyse JUnit tests with ByteBuddy (1.7.5) dynamically, so I don't know them in advance. I am loading them through the run() method below, and intercepting with the Agent built with the Agent() method below.
The unit tests work as expected when I run them directly in Eclipse or when I run them without installing the agent. But as soon as the Agent gets installed, the tests throw exceptions and fail.
For example: java.io.FileNotFoundException
are thrown when the test creates documents (see stack trace below). I am using opensource unit tests (in this case from terrier-core, the tests in the test class org.terrier.indexing.TestSimpleXMLCollection
).
Does someone know why this happens? Is it something like a race condition happening with the original test and the interceptor?
My code is the following:
public void run(@SuppressWarnings("rawtypes") Class classWithName, String testMethodName) {
interceptor.traversed = false;
Request request = Request.method(classWithName, testMethodName);
junit.run(request);
}
And:
public Agent(String pn) {
this.projectName = pn;
this.junit = new JUnitCore();
this.interceptor = new InterceptorX2();
new AgentBuilder.Default().type(
ElementMatchers.not(ElementMatchers.named("junit.framework.testcase"))
.and(ElementMatchers.nameStartsWithIgnoreCase(projectName)
.or(ElementMatchers.nameEndsWithIgnoreCase("test"))
.or(ElementMatchers.nameEndsWithIgnoreCase("tests")))
,
ElementMatchers.not(ElementMatchers.isBootstrapClassLoader()))
.transform(new AgentBuilder.Transformer() {
@Override
public Builder<?> transform(Builder<?> builder, TypeDescription arg1, ClassLoader arg2, JavaModule arg3) {
return builder
.method(
ElementMatchers.isPublic()
.and(ElementMatchers.not(ElementMatchers.isDeclaredBy(Object.class))
.and(ElementMatchers.not(ElementMatchers.isDeclaredBy(junit.framework.TestCase.class))))
).intercept(MethodDelegation.to(interceptor));
}
}).installOn(ByteBuddyAgent.install());
}
EDIT:
And my interceptor is doing nothing special:
@RuntimeType
public Object intercept(@Origin Method m, @SuperCall Callable<?> zuper, @AllArguments Object[] args, @This Object thiz) throws Exception {
return zuper.call();
}
Stacktrace of error:
java.io.FileNotFoundException: C:\Users\jackt\AppData\Local\Temp\junit6014103019704928063\terrier.properties (Das System kann die angegebene Datei nicht finden)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at org.terrier.tests.ApplicationSetupBasedTest.makeEnvironment$original$m3joH5OH(ApplicationSetupBasedTest.java:81)
at org.terrier.tests.ApplicationSetupBasedTest.makeEnvironment$original$m3joH5OH$accessor$c8PeV6zd(ApplicationSetupBasedTest.java)
at org.terrier.tests.ApplicationSetupBasedTest$auxiliary$fr9IrFTX.call(Unknown Source)
at myproject.bytebuddy.introspect.InterceptorX2.intercept(InterceptorX2.java:79)
at org.terrier.tests.ApplicationSetupBasedTest.makeEnvironment(ApplicationSetupBasedTest.java)
at org.terrier.indexing.TestSimpleXMLCollection.makeEnvironment$accessor$BjbiedTZ(TestSimpleXMLCollection.java)
at org.terrier.indexing.TestSimpleXMLCollection$auxiliary$BcvkPOor.call(Unknown Source)
at myproject.bytebuddy.introspect.InterceptorX2.intercept(InterceptorX2.java:79)
at org.terrier.indexing.TestSimpleXMLCollection.makeEnvironment(TestSimpleXMLCollection.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at myproject.bytebuddy.introspect.Agent.run(Agent.java:71)
at myproject.analysis.MutatorAnalysis.runAgent(MutatorAnalysis.java:43)
at myproject.Analysis.getResultsForTestCase(Analysis.java:196)
at myproject.Analysis.startPrivateAnalysis(Analysis.java:163)
at myproject.Analysis.start(Analysis.java:147)