Skip to content

Commit

Permalink
Add test for updating class file version while retransforming
Browse files Browse the repository at this point in the history
  • Loading branch information
felixbarny committed Jun 12, 2020
1 parent 4b3aa01 commit 98d5fb0
Showing 1 changed file with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.math.util.MathUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -50,6 +51,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -266,6 +268,25 @@ String exceptionPlease() {
throw null;
}

@Test
void testPatchClassFileVersionToJava7() {
// loading classes compiled with bytecode level 50 (Java 6)
assertThat(StringUtils.startsWithIgnoreCase("APM", "apm")).isTrue();

// retransforming classes and patch to bytecode level 51 (Java 7)
ElasticApmAgent.initInstrumentation(tracer,
ByteBuddyAgent.install(),
Collections.singletonList(new CommonsLangInstrumentation()));

assertThat(CommonsLangInstrumentation.enterCount).hasValue(0);
assertThat(CommonsLangInstrumentation.exitCount).hasValue(0);

assertThat(StringUtils.startsWithIgnoreCase("APM", "apm")).isTrue();

assertThat(CommonsLangInstrumentation.enterCount).hasPositiveValue();
assertThat(CommonsLangInstrumentation.exitCount).hasPositiveValue();
}

private void init(ConfigurationRegistry config, List<ElasticApmInstrumentation> instrumentations) {
ElasticApmAgent.initInstrumentation(new ElasticApmTracerBuilder()
.configurationRegistry(config)
Expand Down Expand Up @@ -496,4 +517,40 @@ public Collection<String> getInstrumentationGroupNames() {
return List.of("test", "experimental");
}
}

public static class CommonsLangInstrumentation extends ElasticApmInstrumentation {

static AtomicInteger enterCount = new AtomicInteger();
static AtomicInteger exitCount = new AtomicInteger();

@Advice.OnMethodEnter(inline = false)
public static void onEnter() {
enterCount.incrementAndGet();
}

@Advice.OnMethodExit(inline = false)
public static void onExit() {
exitCount.incrementAndGet();
}

@Override
public ElementMatcher<? super TypeDescription> getTypeMatcher() {
return ElementMatchers.nameStartsWith(StringUtils.class.getPackageName());
}

@Override
public ElementMatcher<? super MethodDescription> getMethodMatcher() {
return ElementMatchers.any();
}

@Override
public Collection<String> getInstrumentationGroupNames() {
return Collections.singletonList("test");
}

@Override
public boolean indyDispatch() {
return true;
}
}
}

0 comments on commit 98d5fb0

Please sign in to comment.