Skip to content

Commit 18a3504

Browse files
committed
Add basic test for the AllocationTrackerClassFileTransformer
1 parent 954c0a6 commit 18a3504

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

pom.xml

+18
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@
6464
</build>
6565

6666
<dependencies>
67+
<dependency>
68+
<groupId>junit</groupId>
69+
<artifactId>junit</artifactId>
70+
<version>4.11</version>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.hamcrest</groupId>
75+
<artifactId>hamcrest-all</artifactId>
76+
<version>1.3</version>
77+
<scope>test</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>commons-io</groupId>
81+
<artifactId>commons-io</artifactId>
82+
<version>2.4</version>
83+
<scope>test</scope>
84+
</dependency>
6785
<dependency>
6886
<groupId>org.ow2.asm</groupId>
6987
<artifactId>asm</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package de.codecentric.test;
2+
3+
/**
4+
* Test class for testing byte code manipulation
5+
*/
6+
public class TestBean {
7+
8+
}

0 commit comments

Comments
 (0)