Skip to content

Commit 695460b

Browse files
committed
add test cases
1 parent 9e80a4c commit 695460b

4 files changed

Lines changed: 95 additions & 4 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.apache.skywalking.apm.agent.bytebuddy.biz;
2+
3+
public class ChildBar extends ParentBar {
4+
public String sayHelloChild() {
5+
return "Joe";
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.apache.skywalking.apm.agent.bytebuddy.biz;
2+
3+
public class ParentBar {
4+
public String sayHelloParent() {
5+
return "Joe";
6+
}
7+
}

apm-sniffer/bytebuddy-patch/src/test/java/org/apache/skywalking/apm/agent/bytebuddy/cases/AbstractInterceptTest.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.skywalking.apm.agent.bytebuddy.SWAuxiliaryTypeNamingStrategy;
3939
import org.apache.skywalking.apm.agent.bytebuddy.SWClassFileLocator;
4040
import org.apache.skywalking.apm.agent.bytebuddy.biz.BizFoo;
41+
import org.apache.skywalking.apm.agent.bytebuddy.biz.ChildBar;
4142
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
4243
import org.junit.Assert;
4344
import org.junit.BeforeClass;
@@ -63,6 +64,8 @@ public class AbstractInterceptTest {
6364
public static final String BIZ_FOO_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.BizFoo";
6465
public static final String PROJECT_SERVICE_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ProjectService";
6566
public static final String DOC_SERVICE_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.DocService";
67+
public static final String PARENT_BAR_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ParentBar";
68+
public static final String CHILD_BAR_CLASS_NAME = "org.apache.skywalking.apm.agent.bytebuddy.biz.ChildBar";
6669
public static final String SAY_HELLO_METHOD = "sayHello";
6770
public static final int BASE_INT_VALUE = 100;
6871
public static final String CONSTRUCTOR_INTERCEPTOR_CLASS = "constructorInterceptorClass";
@@ -85,6 +88,20 @@ protected void failed(Throwable e, Description description) {
8588
}
8689
};
8790

91+
protected static void callBar(int round) {
92+
Log.info("-------------");
93+
Log.info("callChildBar: " + round);
94+
// load target class
95+
String strResultChild = new ChildBar().sayHelloChild();
96+
Log.info("result: " + strResultChild);
97+
98+
String strResultParent = new ChildBar().sayHelloParent();
99+
Log.info("result: " + strResultParent);
100+
101+
Assert.assertEquals("String value is unexpected", "John", strResultChild);
102+
Assert.assertEquals("String value is unexpected", "John", strResultParent);
103+
}
104+
88105
protected static void callBizFoo(int round) {
89106
Log.info("-------------");
90107
Log.info("callBizFoo: " + round);
@@ -115,7 +132,7 @@ protected static void checkConstructorInterceptor(String className, int round) {
115132

116133
protected static void checkInterface(Class testClass, Class interfaceCls) {
117134
Assert.assertTrue("Check interface failure, the test class: " + testClass + " does not implement the expected interface: " + interfaceCls,
118-
EnhancedInstance.class.isAssignableFrom(BizFoo.class));
135+
EnhancedInstance.class.isAssignableFrom(testClass));
119136
}
120137

121138
protected static void checkErrors() {
@@ -139,8 +156,7 @@ protected void installMethodInterceptorWithMethodDelegation(String className, St
139156
return builder
140157
.method(ElementMatchers.nameContainsIgnoreCase(methodName))
141158
.intercept(MethodDelegation.withDefaultConfiguration()
142-
.to(new InstMethodsInter(interceptorClassName, classLoader), fieldName))
143-
;
159+
.to(new InstMethodsInter(interceptorClassName, classLoader), fieldName));
144160
}
145161
)
146162
.with(getListener(interceptorClassName))
@@ -223,7 +239,7 @@ protected void installTraceClassTransformer(String msg) {
223239
ClassFileTransformer classFileTransformer = new ClassFileTransformer() {
224240
@Override
225241
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
226-
if (className.endsWith("BizFoo") || className.endsWith("ProjectService") || className.endsWith("DocService")) {
242+
if (className.endsWith("BizFoo") || className.endsWith("ProjectService") || className.endsWith("DocService") || className.endsWith("ChildBar") || className.endsWith("ParentBar")) {
227243
Log.error(msg + className);
228244
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
229245
ClassReader cr = new ClassReader(classfileBuffer);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.agent.bytebuddy.cases;
20+
21+
import net.bytebuddy.agent.ByteBuddyAgent;
22+
import org.apache.skywalking.apm.agent.bytebuddy.biz.ChildBar;
23+
import org.apache.skywalking.apm.agent.bytebuddy.biz.ParentBar;
24+
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
25+
import org.junit.Test;
26+
27+
import java.lang.instrument.Instrumentation;
28+
29+
public class ReTransform4Test extends AbstractReTransformTest {
30+
31+
@Test
32+
public void testEnhancedInstanceForMultiShotRetransform() throws Exception {
33+
Instrumentation instrumentation = ByteBuddyAgent.install();
34+
35+
// install transformer
36+
installMethodInterceptor(PARENT_BAR_CLASS_NAME, "sayHelloParent", 1);
37+
installMethodInterceptor(CHILD_BAR_CLASS_NAME, "sayHelloChild", 1);
38+
// implement EnhancedInstance
39+
installInterface(PARENT_BAR_CLASS_NAME);
40+
installInterface(CHILD_BAR_CLASS_NAME);
41+
42+
callBar(1);
43+
44+
// check interceptors
45+
checkInterface(ParentBar.class, EnhancedInstance.class);
46+
checkInterface(ChildBar.class, EnhancedInstance.class);
47+
checkErrors();
48+
49+
installTraceClassTransformer("Trace class: ");
50+
51+
// do retransform
52+
reTransform(instrumentation, ChildBar.class);
53+
54+
// check interceptors
55+
checkInterface(ParentBar.class, EnhancedInstance.class);
56+
checkInterface(ChildBar.class, EnhancedInstance.class);
57+
checkErrors();
58+
}
59+
60+
}
61+

0 commit comments

Comments
 (0)