Skip to content

Commit

Permalink
Refactored tools-jar rule to rather assure that there is a default at…
Browse files Browse the repository at this point in the history
…tachment provider available that allows to install an instrumentation. This way, the agent-related tests can be run on additional VMs, most importantly on Java 9 VMs.
  • Loading branch information
Rafael Winterhalter committed Sep 16, 2015
1 parent a5c9f4d commit 75b51ce
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 56 deletions.
@@ -1,6 +1,6 @@
package net.bytebuddy.agent;

import net.bytebuddy.test.utility.ToolsJarRule;
import net.bytebuddy.test.utility.AgentAttachmentRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
Expand All @@ -13,10 +13,10 @@
public class ByteBuddyAgentInstallationTest {

@Rule
public MethodRule toolsJarRule = new ToolsJarRule();
public MethodRule agentAttachmentRule = new AgentAttachmentRule();

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testAgentInstallation() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
}
Expand Down
@@ -1,35 +1,31 @@
package net.bytebuddy.test.utility;

import net.bytebuddy.agent.ByteBuddyAgent;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

import java.io.File;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.logging.Logger;

/**
* This rules assures that the running JVM is a JDK JVM with an existing
* <a href="https://blogs.oracle.com/CoreJavaTechTips/entry/the_attach_api">'tools.jar'</a>.
* This rules assures that the running JVM is a JDK JVM with an available
* <a href="https://blogs.oracle.com/CoreJavaTechTips/entry/the_attach_api">attach API</a>.
*/
public class ToolsJarRule implements MethodRule {
public class AgentAttachmentRule implements MethodRule {

public static final String JAVA_HOME_PROPERTY = "java.home";
private final boolean available;

public static final String TOOLS_JAR_LOCATION = "/../lib/tools.jar";

private final boolean toolsJarExists;

public ToolsJarRule() {
toolsJarExists = new File(System.getProperty(JAVA_HOME_PROPERTY).replace('\\', '/') + TOOLS_JAR_LOCATION).isFile();
public AgentAttachmentRule() {
available = ByteBuddyAgent.AttachmentProvider.DEFAULT.attempt().isAvailable();
}

@Override
public Statement apply(Statement base, FrameworkMethod method, Object target) {
return toolsJarExists || method.getAnnotation(Enforce.class) == null
return available || method.getAnnotation(Enforce.class) == null
? base
: new NoOpStatement();
}
Expand Down
Expand Up @@ -25,8 +25,8 @@
import net.bytebuddy.implementation.bytecode.member.MethodInvocation;
import net.bytebuddy.implementation.bytecode.member.MethodReturn;
import net.bytebuddy.pool.TypePool;
import net.bytebuddy.test.utility.AgentAttachmentRule;
import net.bytebuddy.test.utility.JavaVersionRule;
import net.bytebuddy.test.utility.ToolsJarRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
Expand Down Expand Up @@ -64,7 +64,7 @@ public class ByteBuddyTutorialExamplesTest {
public MethodRule javaVersionRule = new JavaVersionRule();

@Rule
public MethodRule toolsJarRule = new ToolsJarRule();
public MethodRule agentAttachmentRule = new AgentAttachmentRule();

@SuppressWarnings("unused")
private static void println(String s) {
Expand Down Expand Up @@ -133,7 +133,7 @@ public void testTutorialGettingStartedClassLoading() throws Exception {
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testTutorialGettingStartedClassReloading() throws Exception {
ByteBuddyAgent.install();
FooReloading foo = new FooReloading();
Expand Down
Expand Up @@ -9,8 +9,8 @@
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.test.utility.AgentAttachmentRule;
import net.bytebuddy.test.utility.ClassFileExtraction;
import net.bytebuddy.test.utility.ToolsJarRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -37,7 +37,7 @@ public class AgentBuilderDefaultApplicationTest {
private static final String FOO = "foo", BAR = "bar", QUX = "qux";

@Rule
public MethodRule toolsJarRule = new ToolsJarRule();
public MethodRule agentAttachmentRule = new AgentAttachmentRule();

private ClassLoader classLoader;

Expand All @@ -52,7 +52,7 @@ public void setUp() throws Exception {
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testAgentWithoutSelfInitialization() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
ClassFileTransformer classFileTransformer = new AgentBuilder.Default()
Expand All @@ -68,7 +68,7 @@ public void testAgentWithoutSelfInitialization() throws Exception {
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testAgentSelfInitialization() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
ClassFileTransformer classFileTransformer = new AgentBuilder.Default()
Expand All @@ -83,7 +83,7 @@ public void testAgentSelfInitialization() throws Exception {
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testAgentSelfInitializationAuxiliaryTypes() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
ClassFileTransformer classFileTransformer = new AgentBuilder.Default()
Expand All @@ -98,7 +98,7 @@ public void testAgentSelfInitializationAuxiliaryTypes() throws Exception {
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testAgentWithoutSelfInitializationWithNativeMethodPrefix() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
ClassFileTransformer classFileTransformer = new AgentBuilder.Default()
Expand Down
Expand Up @@ -2,9 +2,9 @@

import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.dynamic.loading.ClassReloadingStrategy;
import net.bytebuddy.test.utility.AgentAttachmentRule;
import net.bytebuddy.test.utility.JavaVersionRule;
import net.bytebuddy.test.utility.ObjectPropertyAssertion;
import net.bytebuddy.test.utility.ToolsJarRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
Expand All @@ -27,19 +27,19 @@
public class ClassFileLocatorAgentBasedTest {

@Rule
public MethodRule toolsJarRule = new ToolsJarRule();
public MethodRule agentAttachmentRule = new AgentAttachmentRule();

public MethodRule javaVersionRule = new JavaVersionRule();

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testStrategyCreation() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
assertThat(ClassReloadingStrategy.fromInstalledAgent(), notNullValue());
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testExtraction() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
ClassFileLocator classFileLocator = ClassFileLocator.AgentBased.fromInstalledAgent(getClass().getClassLoader());
Expand All @@ -49,7 +49,7 @@ public void testExtraction() throws Exception {
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testExtractionOfInflatedMethodAccessor() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
Method bar = Foo.class.getDeclaredMethod("bar");
Expand Down
Expand Up @@ -4,8 +4,8 @@
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.test.utility.AgentAttachmentRule;
import net.bytebuddy.test.utility.ObjectPropertyAssertion;
import net.bytebuddy.test.utility.ToolsJarRule;
import net.bytebuddy.utility.RandomString;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -25,7 +25,7 @@ public class ClassInjectorUsingImplementationTest {
private static final String FOO = "foo", BAR = "bar";

@Rule
public MethodRule toolsJarRule = new ToolsJarRule();
public MethodRule agentAttachmentRule = new AgentAttachmentRule();

private File folder;

Expand All @@ -38,7 +38,7 @@ public void setUp() throws Exception {
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testBootstrapInjection() throws Exception {
ClassInjector classInjector = new ClassInjector.UsingInstrumentation(folder,
ClassInjector.UsingInstrumentation.Target.BOOTSTRAP,
Expand All @@ -52,7 +52,7 @@ public void testBootstrapInjection() throws Exception {
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testSystemInjection() throws Exception {
ClassInjector classInjector = new ClassInjector.UsingInstrumentation(folder,
ClassInjector.UsingInstrumentation.Target.SYSTEM,
Expand Down
Expand Up @@ -4,8 +4,8 @@
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.test.utility.AgentAttachmentRule;
import net.bytebuddy.test.utility.ObjectPropertyAssertion;
import net.bytebuddy.test.utility.ToolsJarRule;
import net.bytebuddy.utility.RandomString;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -27,7 +27,7 @@ public class ClassLoadingStrategyForBootstrapInjectionTest {
private static final String FOO = "foo", BAR = "bar";

@Rule
public MethodRule toolsJarRule = new ToolsJarRule();
public MethodRule agentAttachmentRule = new AgentAttachmentRule();

private File file;

Expand All @@ -40,7 +40,7 @@ public void setUp() throws Exception {
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testBootstrapInjection() throws Exception {
ClassLoadingStrategy bootstrapStrategy = new ClassLoadingStrategy.ForBootstrapInjection(ByteBuddyAgent.install(), file);
String name = FOO + RandomString.make();
Expand All @@ -52,7 +52,7 @@ public void testBootstrapInjection() throws Exception {
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testClassLoaderInjection() throws Exception {
ClassLoadingStrategy bootstrapStrategy = new ClassLoadingStrategy.ForBootstrapInjection(ByteBuddyAgent.install(), file);
String name = BAR + RandomString.make();
Expand Down
Expand Up @@ -3,8 +3,8 @@
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.test.utility.AgentAttachmentRule;
import net.bytebuddy.test.utility.ObjectPropertyAssertion;
import net.bytebuddy.test.utility.ToolsJarRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
Expand All @@ -21,17 +21,17 @@ public class ClassReloadingStrategyTest {
private static final String FOO = "foo", BAR = "bar";

@Rule
public MethodRule toolsJarRule = new ToolsJarRule();
public MethodRule agentAttachmentRule = new AgentAttachmentRule();

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testStrategyCreation() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
assertThat(ClassReloadingStrategy.fromInstalledAgent(), notNullValue());
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testFromAgentClassReloadingStrategy() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
Foo foo = new Foo();
Expand All @@ -49,7 +49,7 @@ public void testFromAgentClassReloadingStrategy() throws Exception {
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testClassRedefinitionRenamingWithStackMapFrames() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
ClassReloadingStrategy classReloadingStrategy = ClassReloadingStrategy.fromInstalledAgent();
Expand All @@ -64,7 +64,7 @@ public void testClassRedefinitionRenamingWithStackMapFrames() throws Exception {
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testRedefinitionReloadingStrategy() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
Instrumentation instrumentation = spy(ByteBuddyAgent.getInstrumentation());
Expand All @@ -84,7 +84,7 @@ public void testRedefinitionReloadingStrategy() throws Exception {
}

@Test
@ToolsJarRule.Enforce
@AgentAttachmentRule.Enforce
public void testRetransformationReloadingStrategy() throws Exception {
assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
Foo foo = new Foo();
Expand Down
@@ -1,35 +1,31 @@
package net.bytebuddy.test.utility;

import net.bytebuddy.agent.ByteBuddyAgent;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

import java.io.File;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.logging.Logger;

/**
* This rules assures that the running JVM is a JDK JVM with an existing
* <a href="https://blogs.oracle.com/CoreJavaTechTips/entry/the_attach_api">'tools.jar'</a>.
* This rules assures that the running JVM is a JDK JVM with an available
* <a href="https://blogs.oracle.com/CoreJavaTechTips/entry/the_attach_api">attach API</a>.
*/
public class ToolsJarRule implements MethodRule {
public class AgentAttachmentRule implements MethodRule {

public static final String JAVA_HOME_PROPERTY = "java.home";
private final boolean available;

public static final String TOOLS_JAR_LOCATION = "/../lib/tools.jar";

private final boolean toolsJarExists;

public ToolsJarRule() {
toolsJarExists = new File(System.getProperty(JAVA_HOME_PROPERTY).replace('\\', '/') + TOOLS_JAR_LOCATION).isFile();
public AgentAttachmentRule() {
available = ByteBuddyAgent.AttachmentProvider.DEFAULT.attempt().isAvailable();
}

@Override
public Statement apply(Statement base, FrameworkMethod method, Object target) {
return toolsJarExists || method.getAnnotation(Enforce.class) == null
return available || method.getAnnotation(Enforce.class) == null
? base
: new NoOpStatement();
}
Expand Down

0 comments on commit 75b51ce

Please sign in to comment.