summaryrefslogtreecommitdiff
path: root/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/ObjectReference/ReferringObjectsTest.java
blob: d89b5fec73e01c8c7d3215356338b2a4cc28fc74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.harmony.jpda.tests.jdwp.ObjectReference;

import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
import org.apache.harmony.jpda.tests.framework.jdwp.Value;
import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;

public class ReferringObjectsTest extends JDWPSyncTestCase {

    static final int testStatusPassed = 0;

    static final int testStatusFailed = -1;

    static int maxReferrers;

    static final String thisCommandName = "ObjectReference.ReferringObjects command";

    static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ObjectReference/ReferringObjectsDebuggee;";

    static final String referreeObjSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ObjectReference/ReferringObjectsReferree001;";

    static final String referrerObjSignature = "Lorg/apache/harmony/jpda/tests/jdwp/ObjectReference/ReferringObjectsReferrer001;";

    @Override
    protected String getDebuggeeClassName() {
        return "org.apache.harmony.jpda.tests.jdwp.ObjectReference.ReferringObjectsDebuggee";
    }

    /**
     * This testcase exercises ObjectReference.ReferringObjects command.
     * <BR>The test starts ReferringObjectsDebuggee class, requests referree objectID,
     * for this class by ReferenceType.Instances command, then performs ObjectReference.ReferringObjects
     * command and checks that returned instances are equal to the expected referrer objects. Since maxReferrers
     * equals zero, all instances are returned.
     */
    public void testReferringObjects_MaxReferrersIsZero() {
        maxReferrers = 0;
        DoTestReferringObjects();
    }

    /**
     * This testcase exercises ObjectReference.ReferringObjects command.
     * <BR>The test starts ReferringObjectsDebuggee class, requests referree objectID,
     * for this class by ReferenceType.Instances command, then performs ObjectReference.ReferringObjects
     * command and checks that returned instances are equal to the expected referrer objects. Since maxReferrers
     * is more than the number of referrer objects, all instances are returned.
     */
    public void testReferringObjects_MaxReferrersIsLarge() {
        maxReferrers = 20;
        DoTestReferringObjects();
    }

    /**
     * This testcase exercises ObjectReference.ReferringObjects command.
     * <BR>The test starts ReferringObjectsDebuggee class, requests referree objectID,
     * for this class by ReferenceType.Instances command, then performs ObjectReference.ReferringObjects
     * command and checks that returned instances are equal to the expected referrer objects. Since maxReferrers
     * is less than the number of referrer objects, the number of instances returned is equal to maxReferrers.
     */
    public void testReferringObjects_MaxReferrersIsSmall() {
        maxReferrers = 1;
        DoTestReferringObjects();
    }

    /**
     * This is the real body of the testcase which exercises ObjectReference.ReferringObjects command.
     * <BR>The test starts ReferringObjectsDebuggee class, requests referree objectID,
     * for this class by ReferenceType.Instances command, then performs ObjectReference.ReferringObjects
     * command and checks that returned instances are equal to the expected referrer objects.
     */
    public void DoTestReferringObjects() {
        String thisTestName = getName();

        logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);

        // Get the number of referrer objects in ReferringObjectsDebuggee
        long debuggeeRefTypeID = getClassIDBySignature(debuggeeSignature);
        long referringObjNumID = debuggeeWrapper.vmMirror.getFieldID(
                debuggeeRefTypeID, "referringObjNum");
        long[] fieldIDs = new long[1];
        fieldIDs[0] = referringObjNumID;
        Value[] values = debuggeeWrapper.vmMirror.getReferenceTypeValues(
                debuggeeRefTypeID, fieldIDs);
        int expectedReferringObjNum = values[0].getIntValue();

        logWriter.println("=> ReferringObjNum in debuggee is: " + expectedReferringObjNum);

        if(maxReferrers > 0) {
            expectedReferringObjNum = (maxReferrers > expectedReferringObjNum) ? expectedReferringObjNum : maxReferrers;
        }

        // Compose Instances command to get referree objectID
        CommandPacket InstancesCommand = new CommandPacket(
                JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
                JDWPCommands.ReferenceTypeCommandSet.InstancesCommand);

        long referreeObjTypeID = getClassIDBySignature(referreeObjSignature);
        InstancesCommand.setNextValueAsReferenceTypeID(referreeObjTypeID);
        InstancesCommand.setNextValueAsInt(1);

        ReplyPacket checkedReply = debuggeeWrapper.vmMirror.performCommand(InstancesCommand);
        InstancesCommand = null;

        // Get the number of instances that returned.
        int objNum = checkedReply.getNextValueAsInt();
        // Get the tagged-objectID
        byte tag = checkedReply.getNextValueAsByte();
        long objectID = checkedReply.getNextValueAsObjectID();

        // Compose ReferringObjects commnad
        CommandPacket ReferringObjectsCommand = new CommandPacket(
                JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
                JDWPCommands.ObjectReferenceCommandSet.ReferringObjectsCommand);

        ReferringObjectsCommand.setNextValueAsObjectID(objectID);
        ReferringObjectsCommand.setNextValueAsInt(maxReferrers);

        // Perform ReferringObjects command and attain reply package
        checkedReply = debuggeeWrapper.vmMirror
                .performCommand(ReferringObjectsCommand);
        ReferringObjectsCommand = null;

        // Get referrer objects numbers
        int referringObjects = checkedReply.getNextValueAsInt();
        assertEquals(thisCommandName + "returned instances number is wrong.", expectedReferringObjNum, referringObjects,null,null);

        long referrerTypeID = getClassIDBySignature(referrerObjSignature);
        long referrerFieldID = debuggeeWrapper.vmMirror.getFieldID(
                referrerTypeID, "isReferrer");

        // Check the returned objects are referrer objects
        for (int i = 0; i < referringObjects; i++) {
            //Get the tagged-objectID
            tag = checkedReply.getNextValueAsByte();
            assertEquals(thisCommandName
                    + "returned object tag is invalid.", 'L', tag, null, null);

            objectID = checkedReply.getNextValueAsObjectID();
            logWriter.println("=> ObjectID is: " + objectID);
            values = debuggeeWrapper.vmMirror.getObjectReferenceValues(
                    objectID, new long[] { referrerFieldID });
            boolean isReferrer = values[0].getBooleanValue();
            if (!isReferrer) {
                printErrorAndFail(thisCommandName
                        + "returned object is not a referrer which references this object.");
            }
        }
        logWriter.println("=> CHECK: PASSED: expected instances are returned:");
        logWriter.println("=> Returned referringObjects number is" + referringObjects);

        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
        logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": FINISH");
        assertAllDataRead(checkedReply);
    }

    /**
     * This testcase exercises ObjectReference.ReferringObjects command. <BR>
     * Compose a ReferringObjects command with negative maxReferrers
     * The vm should throw a ILLEGAL_ARGUMENT exception.
     */
    public void testReferringObjects_IllegalArgument() {
        String thisTestName = "testReferringObjects_IllegalArgument";

        int maxReferrers = -1;

        logWriter.println("==> " + thisTestName + " for " + thisCommandName + ": START...");
        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);

        // Compose Instances command to get referree objectID
        CommandPacket InstancesCommand = new CommandPacket(
                JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
                JDWPCommands.ReferenceTypeCommandSet.InstancesCommand);

        long referreeObjTypeID = getClassIDBySignature(referreeObjSignature);
        InstancesCommand.setNextValueAsReferenceTypeID(referreeObjTypeID);
        InstancesCommand.setNextValueAsInt(1);

        ReplyPacket checkedReply = debuggeeWrapper.vmMirror.performCommand(InstancesCommand);
        InstancesCommand = null;

        // Get the number of instances that returned.
        int objNum = checkedReply.getNextValueAsInt();
        // Get the tagged-objectID
        byte tag = checkedReply.getNextValueAsByte();
        long objectID = checkedReply.getNextValueAsObjectID();

        // Compose ReferringObjects commnad
        CommandPacket ReferringObjectsCommand = new CommandPacket(
                JDWPCommands.ObjectReferenceCommandSet.CommandSetID,
                JDWPCommands.ObjectReferenceCommandSet.ReferringObjectsCommand);

        ReferringObjectsCommand.setNextValueAsObjectID(objectID);
        ReferringObjectsCommand.setNextValueAsInt(maxReferrers);

        // Perform ReferringObjects command and attain reply package
        checkedReply = debuggeeWrapper.vmMirror
                .performCommand(ReferringObjectsCommand);
        ReferringObjectsCommand = null;

        short errorCode = checkedReply.getErrorCode();
        if (errorCode != JDWPConstants.Error.NONE) {
            if (errorCode == JDWPConstants.Error.NOT_IMPLEMENTED) {
                logWriter.println("=> CHECK PASSED: Expected error (NOT_IMPLEMENTED) is returned");
                return;
            }
            else if(errorCode == JDWPConstants.Error.ILLEGAL_ARGUMENT) {
                logWriter.println("=> CHECK PASSED: Expected error (ILLEGAL_ARGUMENT) is returned");
                return;
            }
        }
        printErrorAndFail(thisCommandName + " should throw ILLEGAL_ARGUMENT exception when maxReferrers is negative.");
    }
}