1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *
15 *  See the License for the specific language governing permissions and
16 *  limitations under the License.
17 */
18
19/**
20 * @author Vitaly A. Provodin
21 */
22
23/**
24 * Created on 28.02.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.VirtualMachine;
27
28import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
29import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
30import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
31import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
32import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
33import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
34
35
36/**
37 * JDWP Unit test for VirtualMachine.DisposeObjects command.
38 */
39public class DisposeObjectsTest extends JDWPSyncTestCase {
40
41    static final String CHECKED_STRING = "Hello World!";
42
43    protected String getDebuggeeClassName() {
44        return "org.apache.harmony.jpda.tests.jdwp.share.debuggee.HelloWorld";
45    }
46
47    /**
48     * This testcase exercises VirtualMachine.DisposeObjects command.
49     * <BR>At first the test starts HelloWorld debuggee.
50     * <BR> Then the test performs VirtualMachine.CreateString command
51     * for some string and checks that VirtualMachine.DisposeObjects command
52     * for returned by CreateString command stringID with refCount = 0
53     * does not dispose that stringID - ObjectReference::ReferenceType
54     * command should return some referenceTypeID without any error.
55     * <BR>Then the test check that repeated VirtualMachine.DisposeObjects command
56     * with refCount = 1 disposes that stringID - ObjectReference::ReferenceType
57     * command should return INVALID_OBJECT error.
58     */
59    public void testDisposeObjects001() {
60        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
61
62        CommandPacket packet = new CommandPacket(
63                JDWPCommands.VirtualMachineCommandSet.CommandSetID,
64                JDWPCommands.VirtualMachineCommandSet.CreateStringCommand);
65        packet.setNextValueAsString(CHECKED_STRING);
66        logWriter.println("\tcreate string: " + CHECKED_STRING);
67        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
68
69        long stringID = reply.getNextValueAsStringID();
70        logWriter.println("\tstring creared: stringID = " + stringID);
71
72        logWriter.println("\tsend DisposeObjects for created string with refCount = 0"
73                + " - string should not be disposed...");
74        packet = new CommandPacket(
75                JDWPCommands.VirtualMachineCommandSet.CommandSetID,
76                JDWPCommands.VirtualMachineCommandSet.DisposeObjectsCommand);
77        packet.setNextValueAsInt(1);
78        packet.setNextValueAsObjectID(stringID);
79        packet.setNextValueAsInt(0);
80        reply = debuggeeWrapper.vmMirror.performCommand(packet);
81        checkReplyPacket(reply, "VirtualMachine::DisposeObjects command");
82
83        logWriter.println
84            ("\tsend ObjectReference::ReferenceType command for created string"
85            + " to make sure that string is not disposed...");
86        packet = new CommandPacket(
87                JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
88                JDWPCommands.ObjectReferenceCommandSet.ReferenceTypeCommand);
89        packet.setNextValueAsObjectID(stringID);
90        reply = debuggeeWrapper.vmMirror.performCommand(packet);
91        checkReplyPacket(reply, "ObjectReference::ReferenceType command");
92
93        byte refTypeTag = reply.getNextValueAsByte();
94        long refTypeID = reply.getNextValueAsReferenceTypeID();
95        logWriter.println("\tReturned refTypeTag = " + refTypeTag
96                + "(" + JDWPConstants.TypeTag.getName(refTypeTag) + ")");
97        logWriter.println("\tReturned ReferenceTypeID for string = " + refTypeID);
98
99        logWriter.println("\tsend DisposeObjects for created string with refCount = 1"
100                + " - string should be disposed...");
101        packet = new CommandPacket(
102                JDWPCommands.VirtualMachineCommandSet.CommandSetID,
103                JDWPCommands.VirtualMachineCommandSet.DisposeObjectsCommand);
104        packet.setNextValueAsInt(1);
105        packet.setNextValueAsObjectID(stringID);
106        packet.setNextValueAsInt(1);
107        reply = debuggeeWrapper.vmMirror.performCommand(packet);
108        checkReplyPacket(reply, "VirtualMachine::DisposeObjects command");
109
110        logWriter.println
111            ("\tsend ObjectReference::ReferenceType command for disposed string"
112            + " - INVALID_OBJECT should be...");
113        packet = new CommandPacket(
114                JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
115                JDWPCommands.ObjectReferenceCommandSet.ReferenceTypeCommand);
116        packet.setNextValueAsObjectID(stringID);
117        reply = debuggeeWrapper.vmMirror.performCommand(packet);
118
119        checkReplyPacket(reply, "ObjectReference::ReferenceType command", JDWPConstants.Error.INVALID_OBJECT);
120
121        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
122    }
123}
124