From 75b51cea6380b8b2e0fe5efebae6f35a213e5032 Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Wed, 16 Sep 2015 21:57:54 +0200 Subject: [PATCH] Refactored tools-jar rule to rather assure that there is a default attachment 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. --- .../agent/ByteBuddyAgentInstallationTest.java | 6 +++--- ...sJarRule.java => AgentAttachmentRule.java} | 20 ++++++++----------- .../ByteBuddyTutorialExamplesTest.java | 6 +++--- .../AgentBuilderDefaultApplicationTest.java | 12 +++++------ .../ClassFileLocatorAgentBasedTest.java | 10 +++++----- .../ClassInjectorUsingImplementationTest.java | 8 ++++---- ...dingStrategyForBootstrapInjectionTest.java | 8 ++++---- .../loading/ClassReloadingStrategyTest.java | 14 ++++++------- ...sJarRule.java => AgentAttachmentRule.java} | 20 ++++++++----------- 9 files changed, 48 insertions(+), 56 deletions(-) rename byte-buddy-agent/src/test/java/net/bytebuddy/test/utility/{ToolsJarRule.java => AgentAttachmentRule.java} (67%) rename byte-buddy-dep/src/test/java/net/bytebuddy/test/utility/{ToolsJarRule.java => AgentAttachmentRule.java} (67%) diff --git a/byte-buddy-agent/src/test/java/net/bytebuddy/agent/ByteBuddyAgentInstallationTest.java b/byte-buddy-agent/src/test/java/net/bytebuddy/agent/ByteBuddyAgentInstallationTest.java index 07ccc8ba3a8..62f9315d185 100644 --- a/byte-buddy-agent/src/test/java/net/bytebuddy/agent/ByteBuddyAgentInstallationTest.java +++ b/byte-buddy-agent/src/test/java/net/bytebuddy/agent/ByteBuddyAgentInstallationTest.java @@ -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; @@ -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)); } diff --git a/byte-buddy-agent/src/test/java/net/bytebuddy/test/utility/ToolsJarRule.java b/byte-buddy-agent/src/test/java/net/bytebuddy/test/utility/AgentAttachmentRule.java similarity index 67% rename from byte-buddy-agent/src/test/java/net/bytebuddy/test/utility/ToolsJarRule.java rename to byte-buddy-agent/src/test/java/net/bytebuddy/test/utility/AgentAttachmentRule.java index ca8bf3d63ff..8a0a7a3f353 100644 --- a/byte-buddy-agent/src/test/java/net/bytebuddy/test/utility/ToolsJarRule.java +++ b/byte-buddy-agent/src/test/java/net/bytebuddy/test/utility/AgentAttachmentRule.java @@ -1,10 +1,10 @@ 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; @@ -12,24 +12,20 @@ import java.util.logging.Logger; /** - * This rules assures that the running JVM is a JDK JVM with an existing - * 'tools.jar'. + * This rules assures that the running JVM is a JDK JVM with an available + * attach API. */ -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(); } diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/ByteBuddyTutorialExamplesTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/ByteBuddyTutorialExamplesTest.java index c79acb1c7c4..a58c9127cf4 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/ByteBuddyTutorialExamplesTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/ByteBuddyTutorialExamplesTest.java @@ -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; @@ -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) { @@ -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(); diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/agent/builder/AgentBuilderDefaultApplicationTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/agent/builder/AgentBuilderDefaultApplicationTest.java index 675e6fc6832..4671732fd93 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/agent/builder/AgentBuilderDefaultApplicationTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/agent/builder/AgentBuilderDefaultApplicationTest.java @@ -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; @@ -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; @@ -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() @@ -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() @@ -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() @@ -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() diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/ClassFileLocatorAgentBasedTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/ClassFileLocatorAgentBasedTest.java index da40b986805..bd8d4544f63 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/ClassFileLocatorAgentBasedTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/ClassFileLocatorAgentBasedTest.java @@ -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; @@ -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()); @@ -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"); diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/loading/ClassInjectorUsingImplementationTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/loading/ClassInjectorUsingImplementationTest.java index cf334a72e06..0d3817c8ecb 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/loading/ClassInjectorUsingImplementationTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/loading/ClassInjectorUsingImplementationTest.java @@ -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; @@ -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; @@ -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, @@ -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, diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/loading/ClassLoadingStrategyForBootstrapInjectionTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/loading/ClassLoadingStrategyForBootstrapInjectionTest.java index 93cf977a47e..415f9039ebc 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/loading/ClassLoadingStrategyForBootstrapInjectionTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/loading/ClassLoadingStrategyForBootstrapInjectionTest.java @@ -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; @@ -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; @@ -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(); @@ -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(); diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/loading/ClassReloadingStrategyTest.java b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/loading/ClassReloadingStrategyTest.java index c2c5d286bda..bee3da0182e 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/loading/ClassReloadingStrategyTest.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/dynamic/loading/ClassReloadingStrategyTest.java @@ -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; @@ -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(); @@ -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(); @@ -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()); @@ -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(); diff --git a/byte-buddy-dep/src/test/java/net/bytebuddy/test/utility/ToolsJarRule.java b/byte-buddy-dep/src/test/java/net/bytebuddy/test/utility/AgentAttachmentRule.java similarity index 67% rename from byte-buddy-dep/src/test/java/net/bytebuddy/test/utility/ToolsJarRule.java rename to byte-buddy-dep/src/test/java/net/bytebuddy/test/utility/AgentAttachmentRule.java index ca8bf3d63ff..8a0a7a3f353 100644 --- a/byte-buddy-dep/src/test/java/net/bytebuddy/test/utility/ToolsJarRule.java +++ b/byte-buddy-dep/src/test/java/net/bytebuddy/test/utility/AgentAttachmentRule.java @@ -1,10 +1,10 @@ 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; @@ -12,24 +12,20 @@ import java.util.logging.Logger; /** - * This rules assures that the running JVM is a JDK JVM with an existing - * 'tools.jar'. + * This rules assures that the running JVM is a JDK JVM with an available + * attach API. */ -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(); }