|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 SAP SE. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * @test |
| 27 | + * @summary Runs the test for the jmc agent integration. |
| 28 | + * |
| 29 | + * @run main/othervm JmcAgentIntegrationTest |
| 30 | + */ |
| 31 | + |
| 32 | +import java.lang.reflect.Method; |
| 33 | +import java.io.File; |
| 34 | +import java.net.URL; |
| 35 | +import java.net.URLClassLoader; |
| 36 | +import java.nio.file.DirectoryStream; |
| 37 | +import java.nio.file.Files; |
| 38 | +import java.nio.file.Path; |
| 39 | +import java.util.ArrayList; |
| 40 | + |
| 41 | +public class JmcAgentIntegrationTest { |
| 42 | + |
| 43 | + public static void main(String[] args) throws Exception { |
| 44 | + File jar = new File(System.getenv("TEST_IMAGE_DIR") + "/jars/agent-tests.jar"); |
| 45 | + |
| 46 | + if (!jar.exists()) { |
| 47 | + return; // Feature was not enabled. |
| 48 | + } |
| 49 | + |
| 50 | + ArrayList<String> testOptions = new ArrayList<>(); |
| 51 | + testOptions.add("-dump"); |
| 52 | + |
| 53 | + // Only run VM-agnostic tests if the ASM version we are using cannot handle the class file spec of this VM. |
| 54 | + int spec = Integer.getInteger("java.vm.specification.version", 99999); |
| 55 | + String javaHome = System.getProperty("java.home"); |
| 56 | + File agentJar = new File(javaHome + "/lib/agent.jar"); |
| 57 | + ClassLoader parentLoader = JmcAgentIntegrationTest.class.getClassLoader(); |
| 58 | + URLClassLoader agentLoader = new URLClassLoader(new URL[] {agentJar.toURI().toURL()}, parentLoader); |
| 59 | + try { |
| 60 | + Class.forName("org.openjdk.jmc.internal.org.objectweb.asm.Opcodes", true, agentLoader).getDeclaredField("V" + spec); |
| 61 | + } catch (NoSuchFieldException e) { |
| 62 | + System.out.println("Incompatible class file version. Skipping VM specific tests."); |
| 63 | + testOptions.add("-vm-agnostic-tests"); |
| 64 | + } |
| 65 | + |
| 66 | + URL url = jar.toURI().toURL(); |
| 67 | + String classPath = System.getProperty("java.class.path", "."); |
| 68 | + |
| 69 | + System.setProperty("java.class.path", classPath + System.getProperty("path.separator") + jar.toString()); |
| 70 | + System.setProperty("useJmcAgentOption", "true"); |
| 71 | + System.setProperty("traceExecs", "true"); |
| 72 | + |
| 73 | + URLClassLoader cl = new URLClassLoader(new URL[] {url}, parentLoader); |
| 74 | + Class<?> testClass = Class.forName("org.openjdk.jmc.agent.sap.test.TestRunner", true, cl); |
| 75 | + Method mainMethod = testClass.getDeclaredMethod("main", String[].class, String[].class); |
| 76 | + mainMethod.invoke(null, new Object[] {testOptions.toArray(new String[0]), new String[] {"-XX:+EnableDynamicAgentLoading"}}); |
| 77 | + } |
| 78 | +} |
0 commit comments