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 05.07.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.ClassType;
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 ClassType.SetValues command with incorrect types of values.
39 */
40public class SetValues002Test extends JDWPSyncTestCase {
41
42    static final int testStatusPassed = 0;
43    static final int testStatusFailed = -1;
44    static final String thisCommandName = "ClassType::SetValues command";
45    static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ClassType/SetValues002Debuggee;";
46
47    protected String getDebuggeeClassName() {
48        return "org.apache.harmony.jpda.tests.jdwp.ClassType.SetValues002Debuggee";
49    }
50
51    /**
52     * The test checks ClassType.SetValues command for
53     * field of Debuggee class with value which has other
54     * referenceType than field to set.
55     * The test expects the field should not be set.
56     */
57    public void testSetValues002() {
58        String thisTestName = "testSetValues002";
59        logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
60        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
61
62        CommandPacket classesBySignatureCommand = new CommandPacket(
63                JDWPCommands.VirtualMachineCommandSet.CommandSetID,
64                JDWPCommands.VirtualMachineCommandSet.ClassesBySignatureCommand);
65        classesBySignatureCommand.setNextValueAsString(debuggeeSignature);
66        ReplyPacket classesBySignatureReply =
67            debuggeeWrapper.vmMirror.performCommand(classesBySignatureCommand);
68        classesBySignatureCommand = null;
69        checkReplyPacket(classesBySignatureReply, "VirtualMachine::ClassesBySignature command");
70
71        classesBySignatureReply.getNextValueAsInt();
72        // Number of returned reference types - is NOt used here
73
74        classesBySignatureReply.getNextValueAsByte();
75        // refTypeTag of class - is NOt used here
76
77        long refTypeID = classesBySignatureReply.getNextValueAsReferenceTypeID();
78        classesBySignatureReply = null;
79
80        logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
81        logWriter.println("=> referenceTypeID for Debuggee class = " + refTypeID);
82
83        String checkedFieldNames[] = {
84            "SetValues002DebuggeeObject",
85            "objectField",
86        };
87        long checkedFieldIDs[] = checkFields(refTypeID, checkedFieldNames);
88        int checkedFieldsNumber = checkedFieldNames.length;
89
90        logWriter.println
91            ("=> Send ReferenceType::GetValues command and get ObjectID for value to set...");
92
93        CommandPacket getValuesCommand = new CommandPacket(
94                JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
95                JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
96        getValuesCommand.setNextValueAsReferenceTypeID(refTypeID);
97        getValuesCommand.setNextValueAsInt(1);
98        getValuesCommand.setNextValueAsFieldID(checkedFieldIDs[0]);
99        ReplyPacket getValuesReply =
100            debuggeeWrapper.vmMirror.performCommand(getValuesCommand);
101        getValuesCommand = null;
102        checkReplyPacket(getValuesReply, "ReferenceType::GetValues command");
103
104        int returnedValuesNumber = getValuesReply.getNextValueAsInt();
105        logWriter.println("=> Returned values number = " + returnedValuesNumber);
106        assertEquals("ReferenceType::GetValues returned invalid values number,",
107                1, returnedValuesNumber);
108
109        Value objectFieldValueToSet = getValuesReply.getNextValueAsValue();
110        byte objectFieldValueToSetTag = objectFieldValueToSet.getTag();
111        logWriter.println
112            ("=> Returned field value tag for checked object= " + objectFieldValueToSetTag
113            + "(" + JDWPConstants.Tag.getName(objectFieldValueToSetTag) + ")");
114        assertEquals("ReferenceType::GetValues returned invalid value tag,",
115                JDWPConstants.Tag.OBJECT_TAG, objectFieldValueToSetTag,
116                JDWPConstants.Tag.getName(JDWPConstants.Tag.OBJECT_TAG),
117                JDWPConstants.Tag.getName(objectFieldValueToSetTag));
118
119        long objectFieldID = objectFieldValueToSet.getLongValue();
120        logWriter.println("=> Returned ObjectID = " + objectFieldID);
121        logWriter.println
122            ("=> CHECK: send " + thisCommandName
123            + " for Debuggee class with value which has other referenceType than field to set...");
124
125        CommandPacket checkedCommand = new CommandPacket(
126                JDWPCommands.ClassTypeCommandSet.CommandSetID,
127                JDWPCommands.ClassTypeCommandSet.SetValuesCommand);
128        checkedCommand.setNextValueAsClassID(refTypeID);
129        checkedCommand.setNextValueAsInt(checkedFieldsNumber-1);
130        int fieldIndex = 1;
131        for (; fieldIndex < checkedFieldsNumber; fieldIndex++) {
132            checkedCommand.setNextValueAsFieldID(checkedFieldIDs[fieldIndex]);
133            switch ( fieldIndex ) {
134            case 1: // objectField
135                checkedCommand.setNextValueAsObjectID(objectFieldID);
136                break;
137            }
138        }
139        ReplyPacket checkedReply =
140            debuggeeWrapper.vmMirror.performCommand(checkedCommand);
141        checkedCommand = null;
142
143        short errorCode = checkedReply.getErrorCode();
144        if ( errorCode == JDWPConstants.Error.NONE ) {
145            logWriter.println("=> " +  thisCommandName
146                    + " run without any ERROR!");
147        } else {
148            logWriter.println("=> " +  thisCommandName
149                    + " returns ERROR = " + errorCode
150                    + "(" + JDWPConstants.Error.getName(errorCode) + ")");
151        }
152
153        logWriter.println("=> Wait for Debuggee's status about check for set field...");
154        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
155        boolean debuggeeStatus = synchronizer.receiveMessage("PASSED");
156        if ( ! debuggeeStatus ) {
157            logWriter.println("## " + thisTestName + ": Debuggee returned status FAILED");
158            fail("Debuggee returned status FAILED");
159        } else {
160            logWriter.println("=> " + thisTestName + ": Debuggee returned status PASSED");
161        }
162
163        logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": OK");
164    }
165}
166