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 Viacheslav G. Rybalov
21 */
22
23/**
24 * Created on 10.03.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.ArrayType;
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.TaggedObject;
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 ArrayType.NewInstance command.
39 */
40
41public class NewInstanceTest extends JDWPSyncTestCase {
42
43    /**
44     * Returns full name of debuggee class which is used by this test.
45     * @return full name of debuggee class.
46     */
47    protected String getDebuggeeClassName() {
48        return "org.apache.harmony.jpda.tests.jdwp.ArrayType.NewInstanceDebuggee";
49    }
50
51    /**
52     * This testcase exercises ArrayType.NewInstance command.
53     * <BR>Starts <A HREF="../share/debuggee/HelloWorld.html">HelloWorld</A> debuggee.
54     * <BR>Creates new instance of array by ArrayType.NewInstance command,
55     * check it length by ArrayReference.Length command.
56     */
57    public void testNewInstance001() {
58        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
59        String[] testedArrayRefs = {
60            "[Lorg/apache/harmony/jpda/tests/jdwp/ArrayType/checkClass;",
61            "[Ljava/lang/String;",
62            "[I",
63            "[[Lorg/apache/harmony/jpda/tests/jdwp/ArrayType/checkClass;",
64            "[[Ljava/lang/String;",
65            "[[I"
66        };
67        for (int i = 0; i < testedArrayRefs.length; i++) {
68            logWriter.println("Checking reference " + testedArrayRefs[i]);
69
70            // Get referenceTypeID
71            CommandPacket packet = new CommandPacket(
72                JDWPCommands.VirtualMachineCommandSet.CommandSetID,
73                JDWPCommands.VirtualMachineCommandSet.ClassesBySignatureCommand);
74            packet.setNextValueAsString(testedArrayRefs[i]);
75            ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
76            checkReplyPacket(reply, "VirtualMachine::ClassesBySignature command");
77
78            int classes = reply.getNextValueAsInt();
79            if (classes <= 0) {
80                logWriter.println("## WARNING: class " + testedArrayRefs[i]+ " is not loadded ");
81                continue;
82            }
83
84            byte refInitTypeTag = reply.getNextValueAsByte();
85            long typeArrayID = reply.getNextValueAsReferenceTypeID();
86            int status = reply.getNextValueAsInt();
87
88            assertEquals("VirtualMachine::ClassesBySignature returned invalid TypeTag,",
89                    JDWPConstants.TypeTag.ARRAY, refInitTypeTag,
90                    JDWPConstants.TypeTag.getName(JDWPConstants.TypeTag.ARRAY),
91                    JDWPConstants.TypeTag.getName(refInitTypeTag));
92
93            logWriter.println(" VirtualMachine.ClassesBySignature: classes="
94                + classes + " refTypeTag=" + refInitTypeTag + " typeID= "
95                + typeArrayID + " status=" + status);
96
97            // Make NewInstance
98            packet = new CommandPacket(
99                JDWPCommands.ArrayTypeCommandSet.CommandSetID,
100                JDWPCommands.ArrayTypeCommandSet.NewInstanceCommand);
101            packet.setNextValueAsReferenceTypeID(typeArrayID);
102            packet.setNextValueAsInt(10);
103            reply = debuggeeWrapper.vmMirror.performCommand(packet);
104            checkReplyPacket(reply, "ArrayType::NewInstance command");
105
106            TaggedObject newArray = reply.getNextValueAsTaggedObject();
107            assertAllDataRead(reply);
108            assertNotNull("ArrayType::NewInstance returned null newArray", newArray);
109
110            logWriter.println("ArrayType.NewInstance: newArray.tag="
111                + newArray.tag + " newArray.objectID=" + newArray.objectID);
112
113            if (newArray.tag != JDWPConstants.Tag.ARRAY_TAG) {
114                logWriter.println("##FAILURE: typeTag is not ARRAY_TAG");
115                fail("Returned tag " + JDWPConstants.Tag.getName(newArray.tag)
116                    + "(" + newArray.tag + ") is not ARRAY_TAG");
117            }
118
119            // Let's check array length
120            packet = new CommandPacket(
121                JDWPCommands.ArrayReferenceCommandSet.CommandSetID,
122                JDWPCommands.ArrayReferenceCommandSet.LengthCommand);
123            packet.setNextValueAsObjectID(newArray.objectID);
124            reply = debuggeeWrapper.vmMirror.performCommand(packet);
125            checkReplyPacket(reply, "ArrayReference::Length command");
126
127            int arrayLength = reply.getNextValueAsInt();
128            logWriter.println("ArrayReference.Length: arrayLength=" + arrayLength);
129            assertEquals("ArrayReference::Length command returned ivalid array length,",
130                    10, arrayLength);
131            assertAllDataRead(reply);
132        }
133        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
134    }
135}
136