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 20.05.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.ReferenceType;
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 ReferenceType.GetValues command for field from another class.
39 */
40public class GetValues004Test extends JDWPSyncTestCase {
41
42    static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/GetValues004Debuggee;";
43    static final String anotherClassSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ReferenceType/RFGetValues004AnotherClass;";
44
45    protected String getDebuggeeClassName() {
46        return "org.apache.harmony.jpda.tests.jdwp.ReferenceType.GetValues004Debuggee";
47    }
48
49    /**
50     * This testcase exercises ReferenceType.GetValues command for field from another class.
51     * <BR>The test starts GetValues004Debuggee and checks that
52     * ReferenceType.GetValues command runs correctly for field declaring
53     * in some another class than passed to GetValues command ReferenceTypeID which is
54     * not assignable from that another class.
55     * <BR>Test expects that INVALID_FIELDID error is returned.
56     */
57    public void testGetValues004() {
58        String thisTestName = "testGetValues004";
59        logWriter.println("==> " + thisTestName + " for ReferenceType.GetValues command: START...");
60        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
61
62        logWriter.println
63        ("\n=> Get anotherClassRefTypeID for checkedClass class = RFGetValues003AnotherClass...");
64        long anotherClassRefTypeID = 0;
65        try {
66            anotherClassRefTypeID = debuggeeWrapper.vmMirror.getClassID(anotherClassSignature);
67        } catch ( Throwable thrown) {
68            logWriter.println("## FAILURE: Can not get anotherClassRefTypeID:");
69            logWriter.println("## Exception: " + thrown);
70            fail("Can not get anotherClassRefTypeID, Exception: " + thrown);
71        }
72        if ( anotherClassRefTypeID == -1 ) {
73            logWriter.println("## FAILURE: Can not get debuggeeRefTypeID for given signature!");
74            logWriter.println("## Signature = |" + anotherClassSignature + "|");
75            fail("Can not get debuggeeRefTypeID for given signature:<" + anotherClassSignature + ">");
76        }
77        logWriter.println("=> anotherClassRefTypeID = " + anotherClassRefTypeID);
78
79        logWriter.println
80        ("\n=> Get anotherClassCheckedFieldID for field of anotherClass...");
81        String anotherClassCheckedFieldName = "anotherClassStaticIntVar";
82        long anotherClassCheckedFieldID = 0;
83        try {
84            anotherClassCheckedFieldID
85                = debuggeeWrapper.vmMirror.getFieldID(anotherClassRefTypeID, anotherClassCheckedFieldName);
86        } catch ( Throwable thrown) {
87            logWriter.println("## FAILURE: Can not get anotherClassCheckedFieldID:");
88            logWriter.println("## Exception: " + thrown);
89            fail("Can not get anotherClassCheckedFieldID, Exception: " + thrown);
90        }
91        logWriter.println("=> superClassCheckedFieldID = " + anotherClassCheckedFieldID);
92
93        logWriter.println
94        ("\n=> Get debuggeeRefTypeID for debuggee class = " + getDebuggeeClassName() + "...");
95        long debuggeeRefTypeID = 0;
96        try {
97            debuggeeRefTypeID = debuggeeWrapper.vmMirror.getClassID(debuggeeSignature);
98        } catch ( Throwable thrown) {
99            logWriter.println("## FAILURE: Can not get debuggeeRefTypeID:");
100            logWriter.println("## Exception: " + thrown);
101            fail("Can not get debuggeeRefTypeID, Exception: " + thrown);
102        }
103        if ( debuggeeRefTypeID == -1 ) {
104            logWriter.println("## FAILURE: Can not get debuggeeRefTypeID for given signature!");
105            logWriter.println("## Signature = |" + debuggeeSignature + "|");
106            fail("Can not get debuggeeRefTypeID for given signature:<" + debuggeeSignature + ">");
107        }
108        logWriter.println("=> debuggeeRefTypeID = " + debuggeeRefTypeID);
109
110        logWriter.println
111        ("\n=> CHECK ReferenceType::GetValues command for debuggeeRefTypeID, anotherClassCheckedFieldID...");
112        logWriter.println
113        ("=> 'INVALID_FIELDID' error is expected as anotherClassCheckedField is not field of debuggee class!");
114        CommandPacket getValuesCommand = new CommandPacket(
115                JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
116                JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
117        getValuesCommand.setNextValueAsReferenceTypeID(debuggeeRefTypeID);
118        getValuesCommand.setNextValueAsInt(1);
119        getValuesCommand.setNextValueAsFieldID(anotherClassCheckedFieldID);
120        ReplyPacket getValuesReply = debuggeeWrapper.vmMirror.performCommand(getValuesCommand);
121        short errorCode = getValuesReply.getErrorCode();
122        if ( errorCode != JDWPConstants.Error.NONE ) {
123            checkReplyPacket(getValuesReply, "ReferenceType::GetValues command", JDWPConstants.Error.INVALID_FIELDID);
124            logWriter.println("=> CHECK PASSED: Expected error (INVALID_FIELDID) is returned");
125            synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
126            return;
127        }
128        logWriter.println
129        ("## FAILURE: ReferenceType::GetValues command does NOT return expected error - INVALID_FIELDID");
130
131        // next is only for extra info
132        //int returnedValuesNumber =
133            getValuesReply.getNextValueAsInt();
134        Value fieldValue = getValuesReply.getNextValueAsValue();
135        byte fieldTag = fieldValue.getTag();
136        logWriter.println("## Returned value tag = " + fieldTag
137            + "(" + JDWPConstants.Tag.getName(fieldTag) + ")");
138        if ( fieldTag == JDWPConstants.Tag.INT_TAG ) {
139            int intValue = fieldValue.getIntValue();
140            logWriter.println("## Returned value = " + intValue);
141        }
142        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
143        fail("ReferenceType::GetValues command does NOT return expected error - INVALID_FIELDID");
144    }
145}
146