Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/aod/VirtualMachine/VirtualMachine02/VirtualMachine02.java
41160 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/VirtualMachine/VirtualMachine02.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* Test checks following methods:33* - VirtualMachine.attach(String id) (test tries to attach to the VM running test34* and to the another VM started by this test)35* - VirtualMachine.attach(VirtualMachineDescriptor vmd) (test tries to attach to the VM running test36* and to the another VM started by this test)37* - VirtualMachine.detach() (test checks that after detaching operations on VirtualMachine throw IOException)38*39* @library /vmTestbase /test/hotspot/jtreg/vmTestbase40* /test/lib41* @build nsk.share.aod.DummyTargetApplication42* @run main/othervm43* -Djdk.attach.allowAttachSelf44* -XX:+UsePerfData45* nsk.aod.VirtualMachine.VirtualMachine02.VirtualMachine0246* -jdk ${test.jdk}47* -javaOpts="-XX:+UsePerfData ${test.vm.opts} ${test.java.opts}"48* -target nsk.share.aod.DummyTargetApplication49*/5051package nsk.aod.VirtualMachine.VirtualMachine02;5253import com.sun.tools.attach.VirtualMachine;54import com.sun.tools.attach.VirtualMachineDescriptor;55import com.sun.tools.attach.spi.AttachProvider;56import nsk.share.aod.AODTestRunner;57import nsk.share.test.TestUtils;5859import java.io.IOException;6061/*62* Test checks following methods:63* - VirtualMachine.attach(String) (test tries to attach to current and to another VM)64* - VirtualMachine.attach(VirtualMachineDescriptor) (test tries to attach to current and to another VM)65* - VirtualMachine.detach() (test checks that after detaching operations on VirtualMachine66* throw IOException)67*/68public class VirtualMachine02 extends AODTestRunner {6970public VirtualMachine02(String[] args) {71super(args);72}7374public void doTestActions(String targetVMId) throws Throwable {75log.display("Executing test for current VM");76String currentVMId = getCurrentVMId();77doTest(currentVMId);78log.display("");7980log.display("Executing test for another VM (id = " + targetVMId + ")");81doTest(targetVMId);82}8384void doTest(String targetVMId) throws Throwable {85VirtualMachine vm;8687log.display("Trying to attach using VirtualMachine(\"" + targetVMId + "\")");88vm = VirtualMachine.attach(targetVMId);89log.display("Attached: " + vm);90checkDetach(vm);9192log.display("Trying to attach using VirtualMachine(VirtualMachineDescriptor)");93AttachProvider provider;94TestUtils.assertTrue(AttachProvider.providers().size() > 0, "AttachProvider.providers() returns empty list");95provider = AttachProvider.providers().get(0);96log.display("Create VirtualMachineDescriptor using provider '" + provider + "'");97VirtualMachineDescriptor vmDescriptor = new VirtualMachineDescriptor(provider, targetVMId);98vm = VirtualMachine.attach(vmDescriptor);99log.display("Attached: " + vm);100TestUtils.assertEquals(vm.provider(), provider, "vm.provider() returns unexpected value: " + vm.provider());101checkDetach(vm);102}103104void checkDetach(VirtualMachine vm) throws Throwable {105log.display("Detaching from " + vm);106vm.detach();107108try {109vm.getSystemProperties();110TestUtils.testFailed("Expected IOException wasn't thrown");111} catch (IOException e) {112// expected exception113}114try {115vm.getAgentProperties();116TestUtils.testFailed("Expected IOException wasn't thrown");117} catch (IOException e) {118// expected exception119}120try {121vm.loadAgent("agent");122TestUtils.testFailed("Expected IOException wasn't thrown");123} catch (IOException e) {124// expected exception125}126try {127vm.loadAgentLibrary("agent");128TestUtils.testFailed("Expected IOException wasn't thrown");129} catch (IOException e) {130// expected exception131}132try {133vm.loadAgentPath("agent");134TestUtils.testFailed("Expected IOException wasn't thrown");135} catch (IOException e) {136// expected exception137}138139// shouldn't throw exception140log.display("Trying to call detach one more time for " + vm);141vm.detach();142}143144public static void main(String[] args) {145new VirtualMachine02(args).runTest();146}147}148149150