summaryrefslogtreecommitdiff
path: root/jdwp/src/test/java/org/apache/harmony/jpda/tests/jdwp/VirtualMachine/InstanceCountsTest.java
blob: cc4ffe40bd55fc36092c02ddbcaf8027329704c0 (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
/*
 * 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.VirtualMachine;

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 InstanceCountsTest extends JDWPSyncTestCase {

    static final int testStatusPassed = 0;

    static final int testStatusFailed = -1;

    static final String thisCommandName = "VirtualMachine.InstanceCounts command ";

    static final String mockClass1Signature = "Lorg/apache/harmony/jpda/tests/jdwp/VirtualMachine/MockClass1;";

    static final String mockClass2Signature = "Lorg/apache/harmony/jpda/tests/jdwp/VirtualMachine/MockClass2;";

    static final String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/VirtualMachine/InstanceCountsDebuggee;";

    protected String getDebuggeeClassName() {
        return "org.apache.harmony.jpda.tests.jdwp.VirtualMachine.InstanceCountsDebuggee";
    }

    /**
     * This testcase exercises VirtualMachine.InstanceCounts command.
     * <BR>The test starts InstanceCountsDebuggee class, requests referenceTypeId,
     * MockClass1, MockClass2 for this class by VirtualMachine.ClassesBySignature command,
     * then performs VirtualMachine.InstanceCounts command and checks that returned
     * Reachable Objects of MockClass1 and MockClass2 are equal to the actual ones.
     */
    public void testInstanceCounts_Normal() {
        String thisTestName = "testInstanceCounts_Normal";

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

        long debuggeeRefTypeID = getClassIDBySignature(debuggeeSignature);
        long mockClassRefTypeIDOfClass1 = getClassIDBySignature(mockClass1Signature);
        long mockClassRefTypeIDOfClass2 = getClassIDBySignature(mockClass2Signature);

        //Get the number of reachable objects of MockClass1 and MockClass2 in debuggee class
        long reachableObjNumOfClass1ID = debuggeeWrapper.vmMirror.getFieldID(
                debuggeeRefTypeID, "reachableObjNumOfClass1");
        long reachableObjNumOfClass2ID = debuggeeWrapper.vmMirror.getFieldID(
                debuggeeRefTypeID, "reachableObjNumOfClass2");

        long[] fieldIDs = new long[2];
        fieldIDs[0] = reachableObjNumOfClass1ID;
        fieldIDs[1] = reachableObjNumOfClass2ID;

        Value[] values = debuggeeWrapper.vmMirror.getReferenceTypeValues(
                debuggeeRefTypeID, fieldIDs);
        int expectedObjNumOfClass1 = values[0].getIntValue();
        int expectedObjNumOfClass2 = values[1].getIntValue();

        logWriter.println("=> ReachableObjNum of MockClass1 in debuggee is: " + expectedObjNumOfClass1);
        logWriter.println("=> ReachableObjNum of MockClass2 in debuggee is: " + expectedObjNumOfClass2);

        logWriter.println("=> CHECK: send " + thisCommandName
                + " and check reply for ERROR...");

        // Compose InstanceCounts command
        CommandPacket InstanceCountsCommand = new CommandPacket(
                JDWPCommands.VirtualMachineCommandSet.CommandSetID,
                JDWPCommands.VirtualMachineCommandSet.InstanceCountsCommand);

        final int refTypesCount = 2;
        InstanceCountsCommand.setNextValueAsInt(refTypesCount);
        InstanceCountsCommand.setNextValueAsReferenceTypeID(mockClassRefTypeIDOfClass1);
        InstanceCountsCommand.setNextValueAsReferenceTypeID(mockClassRefTypeIDOfClass2);

        // Perform InstanceCounts command and attain reply package
        ReplyPacket checkedReply = debuggeeWrapper.vmMirror
                .performCommand(InstanceCountsCommand);
        InstanceCountsCommand = null;

        short errorCode = checkedReply.getErrorCode();
        if (errorCode != JDWPConstants.Error.NONE) {
            if(errorCode == JDWPConstants.Error.ILLEGAL_ARGUMENT) {
                logWriter.println("=> CHECK PASSED: Expected error (ILLEGAL_ARGUMENT) is returned");
                return;
            }
        }

        //Check the reference types count that returned.
        int returnedRefTypesCount = checkedReply.getNextValueAsInt();
        assertEquals(thisCommandName + "returned reference types count is wrong.",
                refTypesCount, returnedRefTypesCount, null, null);
        logWriter.println("=> CHECK: PASSED: expected reference types count is returned:");
        logWriter.println("=> Returned reference types count is " + returnedRefTypesCount);

        long returnedObjNumOfClass1 = checkedReply.getNextValueAsLong();
        assertEquals(thisCommandName + "returned instance count of MockClass1 is wrong.",
                expectedObjNumOfClass1, returnedObjNumOfClass1, null, null);
        logWriter.println("=> CHECK: PASSED: expected instance count of MockClass1 is returned:");
        logWriter.println("=> Returned instance count of MockClass1 is " + returnedObjNumOfClass1);

        long returnedObjNumOfClass2 = checkedReply.getNextValueAsLong();
        assertEquals(thisCommandName + "returned instance count of MockClass2 is wrong.",
                expectedObjNumOfClass2, returnedObjNumOfClass2, null, null);
        logWriter.println("=> CHECK: PASSED: expected instance count of MockClass2 is returned:");
        logWriter.println("=> Returned instance count of MockClass2 is " + returnedObjNumOfClass2);

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

    /**
     * This testcase exercises ReferenceType.InstanceCounts command. <BR>
     * Compose a InstanceCounts command with negative reference types count
     * The vm should throw a ILLEGAL_ARGUMENT exception.
     */
    public void testInstanceCounts_IllegalArgument() {
        String thisTestName = "testInstanceCounts_IllegalArgument";

        int refTypesCount = -1;

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

        // Compose InstanceCounts commnad
        CommandPacket InstanceCountsCommand = new CommandPacket(
                JDWPCommands.VirtualMachineCommandSet.CommandSetID,
                JDWPCommands.VirtualMachineCommandSet.InstanceCountsCommand);

        InstanceCountsCommand.setNextValueAsInt(refTypesCount);
        InstanceCountsCommand.setNextValueAsReferenceTypeID(0);

        // Perform InstanceCounts command and attain reply package
        ReplyPacket checkedReply = debuggeeWrapper.vmMirror
                .performCommand(InstanceCountsCommand);
        InstanceCountsCommand = 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 refTypesCount is negative.");
    }


    /**
     * This testcase exercises ReferenceType.InstanceCounts command. <BR>
     * Compose a InstanceCounts command with zero reference types count.
     * The data following the reference types count should be ignored.
     * And debuggee vm should return a package with zero reference types count.
     */
    public void testInstanceCounts_Zero() {
        String thisTestName = "testInstanceCounts_Zero";

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

        // Compose InstanceCounts command
        CommandPacket InstanceCountsCommand = new CommandPacket(
                JDWPCommands.VirtualMachineCommandSet.CommandSetID,
                JDWPCommands.VirtualMachineCommandSet.InstanceCountsCommand);

        InstanceCountsCommand.setNextValueAsInt(0); // count == 0

        // Perform InstanceCounts command and attain reply package
        ReplyPacket checkedReply = debuggeeWrapper.vmMirror
                .performCommand(InstanceCountsCommand);
        InstanceCountsCommand = 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;
            }
        }

        int returnedRefTypesCount = checkedReply.getNextValueAsInt();
        assertEquals(thisCommandName + "returned reference types count is wrong.",
                0, returnedRefTypesCount, null, null);

        logWriter.println("=> CHECK: PASSED: expected reference types count is returned:");
        logWriter.println("=> Returned reference types count is " + returnedRefTypesCount);
        assertAllDataRead(checkedReply);
    }
}