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 Anatoly F. Bondarenko
21 */
22
23/**
24 * Created on 03.03.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.ObjectReference;
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.framework.jdwp.Value;
33import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
34import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
35
36
37/**
38 * JDWP Unit test for ObjectReference.MonitorInfo command.
39 */
40public class MonitorInfoTest extends JDWPSyncTestCase {
41
42    static final String thisCommandName = "ObjectReference.MonitorInfo command";
43    static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ObjectReference/MonitorInfoDebuggee;";
44
45    @Override
46    protected String getDebuggeeClassName() {
47        return "org.apache.harmony.jpda.tests.jdwp.ObjectReference.MonitorInfoDebuggee";
48    }
49
50    /**
51     * This test exercises ObjectReference.MonitorInfo command.
52     * <BR>The test starts MonitorInfoDebuggee class, gets objectID
53     * as value of static field of this class which (field) represents checked object.
54     * Then for this objectID test executes ObjectReference.MonitorInfo command for
55     * checked object and checks that command returns the expected monitor info:
56     * <BR>monitorOwnerThreadID = 0, monitorEntryCount = 0, monitorWaiters = 0
57     * <BR>Then test waits for Debuggee to continue and to enter in synchronized block
58     * and again executes ObjectReference.MonitorInfo for checked object.
59     * Then test checks that expected results are received:
60     * <BR>monitorOwnerThreadID = 'not null', monitorEntryCount = 1, monitorWaiters = 0
61     */
62    public void testMonitorInfo001() {
63        String thisTestName = "testMonitorInfo001";
64
65        logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
66        String failMessage = "";
67        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
68        finalSyncMessage = "TO_FINISH";
69
70        long refTypeID = getClassIDBySignature(debuggeeSignature);
71
72        logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
73        logWriter.println("=> referenceTypeID for Debuggee class = " + refTypeID);
74
75        long checkedFieldID = checkField(refTypeID, "lockObject");
76
77        logWriter.println
78        ("=> Send ReferenceType::GetValues command for received fieldID and get ObjectID to check...");
79
80        CommandPacket getValuesCommand = new CommandPacket(
81                JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
82                JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
83        getValuesCommand.setNextValueAsReferenceTypeID(refTypeID);
84        getValuesCommand.setNextValueAsInt(1);
85        getValuesCommand.setNextValueAsFieldID(checkedFieldID);
86
87        ReplyPacket getValuesReply = debuggeeWrapper.vmMirror.performCommand(getValuesCommand);
88        getValuesCommand = null;
89        checkReplyPacket(getValuesReply, "ReferenceType::GetValues command");
90
91        int returnedValuesNumber = getValuesReply.getNextValueAsInt();
92        logWriter.println("=> Returned values number = " + returnedValuesNumber);
93        assertEquals("Invalid number of values returned by ReferenceType::GetValues command,", 1, returnedValuesNumber);
94
95        Value checkedObjectFieldValue = getValuesReply.getNextValueAsValue();
96        byte checkedObjectFieldTag = checkedObjectFieldValue.getTag();
97        logWriter.println("=> Returned field value tag for checked object= " + checkedObjectFieldTag
98            + "(" + JDWPConstants.Tag.getName(checkedObjectFieldTag) + ")");
99        assertEquals("Invalid value tag for checked object,", JDWPConstants.Tag.OBJECT_TAG, checkedObjectFieldTag
100                , JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG)
101                , JDWPConstants.Tag.getName(checkedObjectFieldTag));
102
103        long checkedObjectID = checkedObjectFieldValue.getLongValue();
104        logWriter.println("=> Returned checked ObjectID = " + checkedObjectID);
105
106        logWriter.println("=> Send VirtualMachine::Suspend command...");
107
108        CommandPacket suspendCommand = new CommandPacket(
109                JDWPCommands.VirtualMachineCommandSet.CommandSetID,
110                JDWPCommands.VirtualMachineCommandSet.SuspendCommand);
111
112        ReplyPacket suspendReply = debuggeeWrapper.vmMirror.performCommand(suspendCommand);
113        suspendCommand = null;
114        checkReplyPacket(suspendReply, "VirtualMachine::Suspend command");
115
116        logWriter.println
117            ("\n=> CHECK 1: send " + thisCommandName + " for checked ObjectID and check reply...");
118
119        CommandPacket checkedCommand = new CommandPacket(
120                JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
121                JDWPCommands.ObjectReferenceCommandSet.MonitorInfoCommand);
122        checkedCommand.setNextValueAsObjectID(checkedObjectID);
123
124        ReplyPacket checkedReply = debuggeeWrapper.vmMirror.performCommand(checkedCommand);
125        checkedCommand = null;
126
127        short errorCode = checkedReply.getErrorCode();
128        if ( errorCode == JDWPConstants.Error.NOT_IMPLEMENTED ) {
129            // it is possible case
130            logWriter.println("=> " +  thisCommandName + " returns ERROR = " + errorCode
131                    + "(" + JDWPConstants.Error.getName(errorCode) + ")");
132            logWriter.println("=> It is possible case - CHECK 1 PASSED");
133            logWriter.println("=> Send to Debuggee signal to funish ...");
134            synchronizer.sendMessage("TO_FINISH");
135            logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
136        } else {
137            checkReplyPacket(checkedReply, thisCommandName);
138        }
139
140        long monitorOwnerThreadID = checkedReply.getNextValueAsThreadID();
141        logWriter.println("=> Returned monitorOwnerThreadID = " + monitorOwnerThreadID);
142        if ( monitorOwnerThreadID != 0) {
143            logWriter.println
144                ("## FAILURE: " + thisCommandName + " returns unexpected monitorOwnerThreadID:" +
145                        monitorOwnerThreadID);
146            logWriter.println("## Expected monitorOwnerThreadID = 0");
147            failMessage = failMessage +
148                thisCommandName + " returns unexpected monitorOwnerThreadID: " +
149                monitorOwnerThreadID +
150                ", Expected: 0\n";
151        }
152
153        int monitorEntryCount = checkedReply.getNextValueAsInt();
154        logWriter.println("=> Returned monitorEntryCount = " + monitorEntryCount);
155        if ( monitorEntryCount != 0) {
156            logWriter.println
157                ("## FAILURE: " + thisCommandName + " returns unexpected monitorEntryCount:");
158            logWriter.println("## Expected monitorEntryCount = 0");
159            failMessage = failMessage +
160                thisCommandName + " returns unexpected monitorEntryCount:" +
161                monitorEntryCount +
162                ", expected: 0\n";
163        }
164
165        int monitorWaiters = checkedReply.getNextValueAsInt();
166        logWriter.println("=> Returned monitorWaiters = " + monitorWaiters);
167        if ( monitorWaiters != 0) {
168            logWriter.println
169                ("## FAILURE: " + thisCommandName + " returns unexpected monitorWaiters:");
170            logWriter.println("## Expected monitorWaiters = 0");
171            failMessage = failMessage +
172                thisCommandName + " returns unexpected monitorWaiters:" +
173                monitorWaiters +
174                ", expected: 0\n";
175        }
176
177        assertAllDataRead(checkedReply);
178        logWriter.println("=> CHECK 1: PASSED - expected monitor info is received");
179        checkedReply = null;
180
181        resumeDebuggee();
182
183        logWriter.println("=> Send to Debuggee signal to continue and to enter in synchronized block ...");
184        finalSyncMessage = null;
185        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
186        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
187
188        logWriter.println("=> Send VirtualMachine::Suspend command...");
189
190        suspendCommand = new CommandPacket(
191                JDWPCommands.VirtualMachineCommandSet.CommandSetID,
192                JDWPCommands.VirtualMachineCommandSet.SuspendCommand);
193
194        suspendReply = debuggeeWrapper.vmMirror.performCommand(suspendCommand);
195        suspendCommand = null;
196        checkReplyPacket(suspendReply, "VirtualMachine::Suspend command");
197
198        logWriter.println
199            ("\n=> CHECK 2: send " + thisCommandName + " for checked ObjectID when it is locked...");
200
201        checkedCommand = new CommandPacket(
202                JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
203                JDWPCommands.ObjectReferenceCommandSet.MonitorInfoCommand);
204        checkedCommand.setNextValueAsObjectID(checkedObjectID);
205
206        checkedReply = debuggeeWrapper.vmMirror.performCommand(checkedCommand);
207        checkedCommand = null;
208        checkReplyPacket(checkedReply, thisCommandName);
209
210        monitorOwnerThreadID = checkedReply.getNextValueAsThreadID();
211        logWriter.println("=> Returned monitorOwnerThreadID = " + monitorOwnerThreadID);
212        if ( monitorOwnerThreadID == 0) {
213            logWriter.println
214                ("## FAILURE: " + thisCommandName + " returns unexpected monitorOwnerThreadID:");
215            logWriter.println("## Expected monitorOwnerThreadID = 'not null'");
216            failMessage = failMessage +
217                thisCommandName + " returns unexpected monitorOwnerThreadID: 0" +
218                ", Expected monitorOwnerThreadID: 'not null'\n";
219        }
220
221        monitorEntryCount = checkedReply.getNextValueAsInt();
222        logWriter.println("=> Returned monitorEntryCount = " + monitorEntryCount);
223        if ( monitorEntryCount != 1) {
224            logWriter.println
225                ("## FAILURE: " + thisCommandName + " returns unexpected monitorEntryCount:" +
226                        monitorEntryCount);
227            logWriter.println("## Expected monitorEntryCount = 1");
228            failMessage = failMessage +
229                thisCommandName + " returns unexpected monitorEntryCount: " +
230                monitorEntryCount +
231                ", expected: 1\n";
232        }
233
234        monitorWaiters = checkedReply.getNextValueAsInt();
235        logWriter.println("=> Returned monitorWaiters = " + monitorWaiters);
236        if ( monitorWaiters != 0) {
237            logWriter.println
238                ("## FAILURE: " + thisCommandName + " returns unexpected monitorWaiters:" +
239                        monitorWaiters);
240            logWriter.println("## Expected monitorWaiters = 0");
241            failMessage = failMessage +
242                thisCommandName + " returns unexpected monitorWaiters: " +
243                monitorWaiters +
244                ", expected: 0\n";
245        }
246
247        logWriter.println("=> CHECK 2: PASSED - expected monitor info is received");
248        logWriter.println("\n=> Send VirtualMachine::Resume command ...");
249
250        resumeDebuggee();
251
252        logWriter.println("=> Send to Debuggee signal to funish ...");
253        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
254
255        logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
256
257        assertAllDataRead(checkedReply);
258
259        if (failMessage.length() > 0) {
260            fail(failMessage);
261        }
262    }
263}
264