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 Anton V. Karnachuk 21 */ 22 23 /** 24 * Created on 09.03.2005 25 */ 26 package org.apache.harmony.jpda.tests.jdwp.ArrayReference; 27 28 import java.io.UnsupportedEncodingException; 29 30 import org.apache.harmony.jpda.tests.framework.jdwp.ArrayRegion; 31 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket; 32 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands; 33 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants; 34 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket; 35 import org.apache.harmony.jpda.tests.framework.jdwp.Value; 36 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer; 37 38 39 40 /** 41 * JDWP unit test for ArrayReference.GetValues command. 42 */ 43 44 public class GetValuesTest extends JDWPArrayReferenceTestCase { 45 /** 46 * This testcase exercises ArrayReference.GetValues command. 47 * <BR>Starts <A HREF="ArrayReferenceDebuggee.html">ArrayReferenceDebuggee</A>. 48 * <BR>Receives fields with ReferenceType.fields command, 49 * receives values with ArrayReference.GetValues then checks them. 50 */ 51 public void testGetValues001() throws UnsupportedEncodingException { 52 logWriter.println("==> GetValuesTest.testGetValues001 started..."); 53 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY); 54 55 String debuggeeSig = "Lorg/apache/harmony/jpda/tests/jdwp/ArrayReference/ArrayReferenceDebuggee;"; 56 57 // obtain classID 58 long classID = getClassIDBySignature(debuggeeSig); 59 60 // obtain fields 61 CommandPacket packet = new CommandPacket( 62 JDWPCommands.ReferenceTypeCommandSet.CommandSetID, 63 JDWPCommands.ReferenceTypeCommandSet.FieldsCommand); 64 packet.setNextValueAsReferenceTypeID(classID); 65 ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet); 66 checkReplyPacket(reply, "ReferenceType::Fields command"); 67 68 int declared = reply.getNextValueAsInt(); 69 for (int i = 0; i < declared; i++) { 70 long fieldID = reply.getNextValueAsFieldID(); 71 String name = reply.getNextValueAsString(); 72 reply.getNextValueAsString(); 73 reply.getNextValueAsInt(); 74 75 if (name.equals("threadArray")) { 76 logWriter.println 77 ("\n==> testGetValues001: check for array field: 'threadArray'..."); 78 checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 1, 79 1, JDWPConstants.Tag.OBJECT_TAG, JDWPConstants.Tag.THREAD_TAG, false); 80 } 81 if (name.equals("threadGroupArray")) { 82 logWriter.println 83 ("\n==> testGetValues001: check for array field: 'threadGroupArray..."); 84 checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 1, 85 1, JDWPConstants.Tag.OBJECT_TAG, JDWPConstants.Tag.THREAD_GROUP_TAG, false); 86 } 87 if (name.equals("classArray")) { 88 logWriter.println 89 ("\n==> testGetValues001: check for array field: 'classArray'..."); 90 checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 1, 91 1, JDWPConstants.Tag.OBJECT_TAG, JDWPConstants.Tag.CLASS_OBJECT_TAG, false); 92 } 93 if (name.equals("ClassLoaderArray")) { 94 logWriter.println 95 ("\n==> testGetValues001: check for array field: 'ClassLoaderArray'..."); 96 checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 1, 97 1, JDWPConstants.Tag.OBJECT_TAG, JDWPConstants.Tag.CLASS_LOADER_TAG, false); 98 } 99 if (name.equals("myThreadArray")) { 100 logWriter.println 101 ("\n==> testGetValues001: check for array field: 'myThreadArray'..."); 102 checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 1, 103 1, JDWPConstants.Tag.OBJECT_TAG, JDWPConstants.Tag.THREAD_TAG, false); 104 } 105 if (name.equals("objectArrayArray")) { 106 logWriter.println 107 ("\n==> testGetValues001: check for array field: 'objectArrayArray'..."); 108 checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 1, 109 1, JDWPConstants.Tag.ARRAY_TAG, JDWPConstants.Tag.ARRAY_TAG, false); 110 } 111 if (name.equals("intArray")) { 112 // int[] intArray = new int[10] 113 logWriter.println 114 ("\n==> testGetValues001: check for array field: 'int[] intArray'..."); 115 checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 10, 116 4, JDWPConstants.Tag.INT_TAG, JDWPConstants.Tag.INT_TAG, true); 117 } 118 if (name.equals("strArray")) { 119 // String[] strArray = new String[8] 120 logWriter.println 121 ("\n==> testGetValues001: check for array field: 'String[] strArray'..."); 122 checkArrayValues(classID, fieldID, JDWPConstants.Error.NONE, 8, 123 4, JDWPConstants.Tag.OBJECT_TAG, JDWPConstants.Tag.STRING_TAG, true); 124 } 125 if (name.equals("intField")) { 126 // Integer intField = new Integer(-1) 127 logWriter.println 128 ("\n==> testGetValues001: check for non-array field: 'Integer intField = new Integer(-1)'..."); 129 checkArrayValues(classID, fieldID, JDWPConstants.Error.INVALID_ARRAY, 0, 130 4, (byte)0, (byte)0, false); 131 } 132 } 133 134 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 135 } 136 137 private void checkArrayValues(long classID, long fieldID, int error, int length, 138 int checksNumber, byte expectedArrayTag, byte expectedElementTag, boolean checkValues) 139 throws UnsupportedEncodingException { 140 CommandPacket packet = new CommandPacket( 141 JDWPCommands.ReferenceTypeCommandSet.CommandSetID, 142 JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand); 143 // set referenceTypeID 144 packet.setNextValueAsReferenceTypeID(classID); 145 // repeat 1 time 146 packet.setNextValueAsInt(1); 147 // set fieldID 148 packet.setNextValueAsFieldID(fieldID); 149 ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet); 150 checkReplyPacket(reply, "ReferenceType::GetValues command"); 151 152 assertEquals("ReferenceType::GetValues command returned invalid int value,", reply.getNextValueAsInt(), 1); 153 Value value = reply.getNextValueAsValue(); 154 long arrayID = value.getLongValue(); 155 logWriter.println("==> testGetValues001: checked arrayID = " + arrayID); 156 157 logWriter.println("==> testGetValues001: checkArrayRegion: arrayID = " + arrayID 158 + "; Expected error = " + error + "(" + JDWPConstants.Error.getName(error) + ")" 159 + "; firstIndex = 0; length = " + length); 160 checkArrayRegion 161 (arrayID, error, 0, length, expectedArrayTag, expectedElementTag, checkValues); 162 logWriter.println("==> PASSED!"); 163 164 if ( checksNumber > 1 ) { 165 logWriter.println("==> testGetValues001: checkArrayRegion: arrayID = " + arrayID 166 + "; Expected error = " + error+ "(" + JDWPConstants.Error.getName(error) + ")" 167 + "; firstIndex = 1; length = " + (length-1)); 168 checkArrayRegion 169 (arrayID, error, 1, length-1, expectedArrayTag, expectedElementTag, checkValues); 170 logWriter.println("==> PASSED!"); 171 172 logWriter.println("==> testGetValues001: checkArrayRegion: arrayID = " + arrayID 173 + "; Expected error = " + error+ "(" + JDWPConstants.Error.getName(error) + ")" 174 + "; firstIndex = 0; length = " + (length-1)); 175 checkArrayRegion 176 (arrayID, error, 0, length-1, expectedArrayTag, expectedElementTag, checkValues); 177 logWriter.println("==> PASSED!"); 178 179 logWriter.println("==> testGetValues001: checkArrayRegion: arrayID = " + arrayID 180 + "; Expected error = " + error+ "(" + JDWPConstants.Error.getName(error) + ")" 181 + "; firstIndex = " + (length-1) + " length = 1"); 182 checkArrayRegion 183 (arrayID, error, length-1, 1, expectedArrayTag, expectedElementTag, checkValues); 184 logWriter.println("==> PASSED!"); 185 } 186 } 187 188 private void checkArrayRegion(long arrayID, int error, int firstIndex, int length, 189 byte expectedArrayTag, byte expectedElementTag, boolean checkValues) 190 throws UnsupportedEncodingException { 191 192 CommandPacket packet = new CommandPacket( 193 JDWPCommands.ArrayReferenceCommandSet.CommandSetID, 194 JDWPCommands.ArrayReferenceCommandSet.GetValuesCommand); 195 packet.setNextValueAsArrayID(arrayID); 196 packet.setNextValueAsInt(firstIndex); 197 packet.setNextValueAsInt(length); 198 199 ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet); 200 checkReplyPacket(reply, "ArrayReference::GetValues command", error); 201 202 if (reply.getErrorCode() == JDWPConstants.Error.NONE) { 203 // do not check values for non-array fields 204 ArrayRegion region = reply.getNextValueAsArrayRegion(); 205 //System.err.println("length="+region.length); 206 byte arrayTag = region.getTag(); 207 logWriter.println("==> arrayTag = " + arrayTag 208 + "(" + JDWPConstants.Tag.getName(arrayTag) + ")"); 209 logWriter.println("==> arrayLength = "+region.getLength()); 210 Value value_0 = region.getValue(0); 211 byte elementTag = value_0.getTag(); 212 logWriter.println("==> elementTag = " + elementTag 213 + "(" + JDWPConstants.Tag.getName(elementTag) + ")"); 214 215 assertEquals("ArrayReference::GetValues returned invalid array tag,", 216 expectedArrayTag, arrayTag, 217 JDWPConstants.Tag.getName(expectedArrayTag), 218 JDWPConstants.Tag.getName(arrayTag)); 219 assertEquals("ArrayReference::GetValues returned invalid array length,", 220 length, region.getLength()); 221 assertEquals("ArrayReference::GetValues returned invalid element tag", 222 expectedElementTag, elementTag, 223 JDWPConstants.Tag.getName(expectedElementTag), 224 JDWPConstants.Tag.getName(elementTag)); 225 226 if (checkValues) { 227 for (int i = 0; i < region.getLength(); i++) { 228 Value value = region.getValue(i); 229 if (value.getTag() == JDWPConstants.Tag.INT_TAG) { 230 assertEquals("ArrayReference::GetValues returned invalid value on index:<" + i + ">,", 231 value.getIntValue(), i + firstIndex); 232 } 233 else if (value.getTag() == JDWPConstants.Tag.STRING_TAG) { 234 long stringID = value.getLongValue(); 235 String s = getStringValue(stringID); 236 assertEquals("ArrayReference::GetValues returned invalid value on index:<" + i + ">,", 237 Integer.parseInt(s), i + firstIndex); 238 } 239 } 240 } 241 } 242 } 243 } 244