Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/aod/AttachProvider/AttachProvider02/AttachProvider02.java
41161 views
/*1* Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25*26* @summary converted from VM Testbase nsk/aod/AttachProvider/AttachProvider02.27* VM Testbase keywords: [feature_282, jdk]28* VM Testbase readme:29* Description :30* Test checks work of Attach API (com.sun.tools.attach).31* Test is based on the nsk.share.aod framework.32* This test tries to attach to the VM started by this test using 2 methods:33* - AttachProvider.attachVirtualMachine(String id)34* - AttachProvider.attachVirtualMachine(VirtualMachineDescriptor vmd)35* After attaching test tries to use created VirtualMachine object (tries to call VirtualMachine.getSystemProperties()).36*37* @library /vmTestbase /test/hotspot/jtreg/vmTestbase38* /test/lib39* @build nsk.share.aod.DummyTargetApplication40* @run main/othervm41* -Djdk.attach.allowAttachSelf42* -XX:+UsePerfData43* nsk.aod.AttachProvider.AttachProvider02.AttachProvider0244* -jdk ${test.jdk}45* -javaOpts="-XX:+UsePerfData ${test.vm.opts} ${test.java.opts}"46* -target nsk.share.aod.DummyTargetApplication47*/4849package nsk.aod.AttachProvider.AttachProvider02;5051import com.sun.tools.attach.VirtualMachine;52import com.sun.tools.attach.VirtualMachineDescriptor;53import com.sun.tools.attach.spi.AttachProvider;54import nsk.share.aod.AODTestRunner;55import nsk.share.test.TestUtils;5657import java.util.Properties;5859/*60* Test checks following methods:61* - AttachProvider.attachVirtualMachine(String id)62* - AttachProvider.attachVirtualMachine(VirtualMachineDescriptor vmd)63*/64public class AttachProvider02 extends AODTestRunner {6566public AttachProvider02(String[] args) {67super(args);68}6970public void doTestActions(String targetVMId) throws Throwable {71TestUtils.assertTrue(AttachProvider.providers().size() > 0, "Method AttachProvider.providers() returns empty collection");7273String currentVMId = getCurrentVMId();7475for (AttachProvider provider : AttachProvider.providers()) {76log.display("Provider: " + provider);77log.display("Provider.name(): " + provider.name());78log.display("Provider.type(): " + provider.type());7980TestUtils.assertNotNull(provider.name(), "Provider.name() returns null");81TestUtils.assertNotNull(provider.type(), "Provider.type() returns null");8283tryAttach(provider, currentVMId, false);84tryAttach(provider, currentVMId, true);8586tryAttach(provider, targetVMId, false);87tryAttach(provider, targetVMId, true);88}89}9091void tryAttach(AttachProvider provider, String vmId, boolean useVMDescriptor) throws Throwable {92log.display("Attaching to vm " + vmId + " using " +93(useVMDescriptor ? "VirtualMachineDescriptor " : "VM id"));9495VirtualMachine vm;9697if (useVMDescriptor) {98vm = provider.attachVirtualMachine(new VirtualMachineDescriptor(provider, vmId));99} else {100vm = provider.attachVirtualMachine(vmId);101}102103try {104log.display("Attached to vm: " + vm);105TestUtils.assertEquals(vm.id(), vmId, "VirtualMachine.id() returns unexpected value for attached vm: " + vm.id());106107// try to use created VirtualMachine108log.display("Trying to call VirtualMachine.getSystemProperties()");109Properties properties = vm.getSystemProperties();110TestUtils.assertNotNull(properties, "VirtualMachine.getSystemProperties() returns null");111112TestUtils.assertTrue(properties.size() > 0, "VirtualMachine.getSystemProperties() returns empty collection");113} finally {114vm.detach();115}116}117118public static void main(String[] args) {119new AttachProvider02(args).runTest();120}121}122123124