-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test for
AgentMain
which verifies that all classes from the s…
…pec are actually instrumented
- Loading branch information
Showing
2 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/test/java/org/kohsuke/file_leak_detector/AgentMainTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.kohsuke.file_leak_detector; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyBoolean; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.lang.instrument.ClassFileTransformer; | ||
import java.lang.instrument.Instrumentation; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.junit.Test; | ||
import org.kohsuke.file_leak_detector.transform.ClassTransformSpec; | ||
import org.mockito.Mockito; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.stubbing.Answer; | ||
|
||
public class AgentMainTest { | ||
@Test | ||
public void noDuplicateSpecs() { | ||
final List<ClassTransformSpec> specs = AgentMain.createSpec(); | ||
|
||
Set<String> seenClasses = new HashSet<>(); | ||
for (ClassTransformSpec spec : specs) { | ||
assertTrue("Did have duplicate spec for class " + spec.name, seenClasses.add(spec.name)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testPreMain() throws Exception { | ||
final List<ClassTransformSpec> specs = AgentMain.createSpec(); | ||
|
||
final Set<String> seenClasses = new HashSet<>(); | ||
for (ClassTransformSpec spec : specs) { | ||
seenClasses.add(spec.name); | ||
} | ||
|
||
Instrumentation instrumentation = mock(Instrumentation.class); | ||
Mockito.doAnswer(new Answer<Object>() { | ||
@Override | ||
public Object answer(InvocationOnMock invocationOnMock) throws Throwable { | ||
for (Object obj : invocationOnMock.getArguments()) { | ||
Class<?> clazz = (Class<?>) obj; | ||
String name = clazz.getName().replace(".", "/"); | ||
assertTrue( | ||
"Tried to transform a class wihch is not contained in the specs: " | ||
+ name | ||
+ " (" | ||
+ clazz | ||
+ "), having remaining classes: " | ||
+ seenClasses, | ||
seenClasses.remove(name)); | ||
} | ||
return null; | ||
} | ||
}).when(instrumentation).retransformClasses((Class<?>) any()); | ||
|
||
AgentMain.premain(null, instrumentation); | ||
|
||
verify(instrumentation, times(1)).addTransformer((ClassFileTransformer) any(), anyBoolean()); | ||
verify(instrumentation, times(1)).retransformClasses((Class<?>) any()); | ||
|
||
// the following are not available in all JVMs | ||
seenClasses.remove("sun/nio/ch/SocketChannelImpl"); | ||
seenClasses.remove("java/net/AbstractPlainSocketImpl"); | ||
|
||
assertTrue( | ||
"Had classes in the spec which were not instrumented: " + seenClasses, | ||
seenClasses.isEmpty()); | ||
} | ||
} |