|
| 1 | +package de.codecentric.performance.agent.allocation; |
| 2 | + |
| 3 | +import static org.hamcrest.Matchers.greaterThan; |
| 4 | +import static org.hamcrest.Matchers.is; |
| 5 | +import static org.junit.Assert.assertSame; |
| 6 | +import static org.junit.Assert.assertThat; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | + |
| 11 | +import org.apache.commons.io.IOUtils; |
| 12 | +import org.junit.Assert; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +import de.codecentric.test.TestBean; |
| 17 | + |
| 18 | +public class AllocationTrackerClassFileTransformerTest { |
| 19 | + |
| 20 | + /** |
| 21 | + * The object under test |
| 22 | + */ |
| 23 | + private AllocationTrackerClassFileTransformer transformer; |
| 24 | + |
| 25 | + @Before |
| 26 | + public void setUp() { |
| 27 | + transformer = new AllocationTrackerClassFileTransformer("de.codecentric.test"); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void allocationTrackerClassesAreIgnored() throws Exception { |
| 32 | + byte[] classBytes = getClassBytes(ConstructorVisitor.class); |
| 33 | + byte[] actual = transformer.transform(null, ConstructorVisitor.class.getName(), ConstructorVisitor.class, null, |
| 34 | + classBytes); |
| 35 | + |
| 36 | + assertSame(classBytes, actual); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void notMatchingClassPrefixesAreIgnored() throws Exception { |
| 41 | + byte[] classBytes = getClassBytes(Assert.class); |
| 42 | + byte[] actual = transformer.transform(null, Assert.class.getName(), Assert.class, null, classBytes); |
| 43 | + |
| 44 | + assertSame(classBytes, actual); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void matchingClassesAreExtended() throws Exception { |
| 49 | + byte[] byteArray = getClassBytes(TestBean.class); |
| 50 | + byte[] actual = transformer.transform(null, TestBean.class.getName(), TestBean.class, null, byteArray); |
| 51 | + |
| 52 | + // the class has been changed. What else can we do? |
| 53 | + assertThat(actual.length, is(greaterThan(byteArray.length))); |
| 54 | + } |
| 55 | + |
| 56 | + private byte[] getClassBytes(Class<?> clazz) throws IOException { |
| 57 | + String className = clazz.getName(); |
| 58 | + String classAsPath = className.replace('.', '/') + ".class"; |
| 59 | + InputStream stream = clazz.getClassLoader().getResourceAsStream(classAsPath); |
| 60 | + |
| 61 | + return IOUtils.toByteArray(stream); |
| 62 | + } |
| 63 | +} |
0 commit comments