001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Anton V. Karnachuk
021: * @version $Revision: 1.3 $
022: */
023:
024: /**
025: * Created on 10.02.2005
026: */package org.apache.harmony.jpda.tests.jdwp.ClassType;
027:
028: import java.io.UnsupportedEncodingException;
029:
030: import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
031: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
032: import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
033: import org.apache.harmony.jpda.tests.framework.jdwp.Value;
034: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
035:
036: /**
037: * JDWP unit test for ClassType.SetValues command.
038: */
039:
040: public class SetValuesTest extends JDWPClassTypeTestCase {
041:
042: /**
043: * Starts this test by junit.textui.TestRunner.run() method.
044: */
045: public static void main(String[] args) {
046: junit.textui.TestRunner.run(SetValuesTest.class);
047: }
048:
049: /**
050: * This testcase exercises ClassType.SetValues command.
051: * <BR>Starts <A HREF="ClassTypeDebuggee.html">ClassTypeDebuggee</A>.
052: * <BR>Receives all fields from debuggee with ReferenceType.fields command.
053: * Then sets values for these fields with ClassType.SetValues command
054: * and checks set values using ReferenceType.GetValues command.
055: */
056: public void testSetValues001() throws UnsupportedEncodingException {
057: logWriter.println("testSetValues001 started");
058: synchronizer
059: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
060:
061: long classID = getClassIDBySignature(getDebuggeeSignature());
062:
063: FieldInfo[] fields = jdwpGetFieldIDs(classID);
064:
065: for (int i = 0; i < fields.length; i++) {
066: FieldInfo field = fields[i];
067: //logWriter.println(field.toString());
068: testField(classID, field);
069: }
070: }
071:
072: private void testField(long classID, FieldInfo fieldInfo)
073: throws UnsupportedEncodingException {
074:
075: //System.err.println("testField: "+fieldInfo.toString());
076: // if field has primitive type
077: if (fieldInfo.getSignature().length() >= 1) {
078: switch (fieldInfo.getSignature().charAt(0)) {
079: case 'B': // byte
080: testField(classID, fieldInfo, new Value(Byte.MIN_VALUE));
081: testField(classID, fieldInfo, new Value(Byte.MAX_VALUE));
082: testField(classID, fieldInfo, new Value((byte) 0));
083: break;
084: case 'C': // char
085: testField(classID, fieldInfo, new Value(
086: (char) Character.MAX_VALUE));
087: testField(classID, fieldInfo, new Value(
088: (char) Character.MIN_VALUE));
089: break;
090: case 'F': // float
091: testField(classID, fieldInfo, new Value(
092: (float) Float.MIN_VALUE));
093: testField(classID, fieldInfo, new Value(
094: (float) Float.MAX_VALUE));
095: testField(classID, fieldInfo, new Value(
096: (float) Float.NaN));
097: testField(classID, fieldInfo, new Value(
098: (float) Float.NEGATIVE_INFINITY));
099: testField(classID, fieldInfo, new Value(
100: (float) Float.POSITIVE_INFINITY));
101: testField(classID, fieldInfo, new Value((float) 0));
102: break;
103: case 'D': // double
104: testField(classID, fieldInfo, new Value(
105: (double) Double.MIN_VALUE));
106: testField(classID, fieldInfo, new Value(
107: (double) Double.MAX_VALUE));
108: testField(classID, fieldInfo, new Value(
109: (double) Double.NaN));
110: testField(classID, fieldInfo, new Value(
111: (double) Double.NEGATIVE_INFINITY));
112: testField(classID, fieldInfo, new Value(
113: (double) Double.POSITIVE_INFINITY));
114: testField(classID, fieldInfo, new Value((double) 0));
115: break;
116: case 'I': // int
117: testField(classID, fieldInfo, new Value(
118: (int) Integer.MIN_VALUE));
119: testField(classID, fieldInfo, new Value(
120: (int) Integer.MAX_VALUE));
121: testField(classID, fieldInfo, new Value((int) 0));
122: break;
123: case 'J': // long
124: testField(classID, fieldInfo, new Value(
125: (long) Long.MIN_VALUE));
126: testField(classID, fieldInfo, new Value(
127: (long) Long.MAX_VALUE));
128: testField(classID, fieldInfo, new Value((long) 0));
129: break;
130: case 'S': // short
131: testField(classID, fieldInfo, new Value(
132: (short) Short.MIN_VALUE));
133: testField(classID, fieldInfo, new Value(
134: (short) Short.MAX_VALUE));
135: testField(classID, fieldInfo, new Value((short) 0));
136: break;
137: case 'Z': // boolean
138: testField(classID, fieldInfo, new Value(
139: (boolean) Boolean.FALSE.booleanValue()));
140: testField(classID, fieldInfo, new Value(
141: (boolean) Boolean.TRUE.booleanValue()));
142: break;
143: }
144: }
145: }
146:
147: private void testField(long classID, FieldInfo fieldInfo,
148: Value value) throws UnsupportedEncodingException {
149:
150: logWriter.println("\n==> testField: ");
151: logWriter.println(" classID = " + classID);
152: logWriter.println(" fieldInfo = " + fieldInfo);
153: logWriter.println(" value to set = " + value);
154: CommandPacket packet = new CommandPacket(
155: JDWPCommands.ClassTypeCommandSet.CommandSetID,
156: JDWPCommands.ClassTypeCommandSet.SetValuesCommand);
157: packet.setNextValueAsClassID(classID);
158: packet.setNextValueAsInt(1);
159: packet.setNextValueAsFieldID(fieldInfo.getFieldID());
160:
161: packet.setNextValueAsUntaggedValue(value);
162:
163: ReplyPacket reply = debuggeeWrapper.vmMirror
164: .performCommand(packet);
165: checkReplyPacket(reply, "ClassType::SetValues command");
166:
167: packet = new CommandPacket(
168: JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
169: JDWPCommands.ReferenceTypeCommandSet.GetValuesCommand);
170: packet.setNextValueAsReferenceTypeID(classID);
171: packet.setNextValueAsInt(1);
172: packet.setNextValueAsFieldID(fieldInfo.getFieldID());
173:
174: reply = debuggeeWrapper.vmMirror.performCommand(packet);
175: checkReplyPacket(reply, "ClassType::GetValues command");
176:
177: int fields = reply.getNextValueAsInt();
178: assertEquals(
179: "ClassType::SetValues returne invalid fields number,",
180: 1, fields);
181: Value returnedValue = reply.getNextValueAsValue();
182: assertEquals(
183: "ClassType::SetValues returne invalid returned value,",
184: value, returnedValue);
185: logWriter.println("==> testField: OK");
186: }
187:
188: private FieldInfo[] jdwpGetFieldIDs(long classID) {
189: CommandPacket packet = new CommandPacket(
190: JDWPCommands.ReferenceTypeCommandSet.CommandSetID,
191: JDWPCommands.ReferenceTypeCommandSet.FieldsCommand);
192: packet.setNextValueAsReferenceTypeID(classID);
193:
194: ReplyPacket reply = debuggeeWrapper.vmMirror
195: .performCommand(packet);
196: checkReplyPacket(reply, "ReferenceType::Fields command");
197:
198: int declared = reply.getNextValueAsInt();
199: FieldInfo[] fields = new FieldInfo[declared];
200: for (int i = 0; i < declared; i++) {
201: fields[i] = new FieldInfo(reply.getNextValueAsFieldID(),
202: reply.getNextValueAsString(), reply
203: .getNextValueAsString(), reply
204: .getNextValueAsInt());
205: }
206:
207: return fields;
208: }
209:
210: }
|