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 Ruslan A. Scherbakov
21  */
22 
23 /**
24  * Created on 14.06.2006
25  */
26 package org.apache.harmony.jpda.tests.jdwp.Events;
27 
28 import org.apache.harmony.jpda.tests.framework.jdwp.EventPacket;
29 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
30 import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
31 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
32 import org.apache.harmony.jpda.tests.framework.jdwp.TaggedObject;
33 import org.apache.harmony.jpda.tests.framework.jdwp.Value;
34 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
35 
36 /**
37  * JDWP Unit test for FIELD_MODIFICATION event.
38  */
39 public class FieldModification002Test extends JDWPEventTestCase {
getDebuggeeClassName()40     protected String getDebuggeeClassName() {
41         return FieldModification002Debuggee.class.getName();
42     }
43 
44     /**
45      * This testcase is for FIELD_MODIFICATION event.
46      * <BR>It FieldModification002Debuggee that modifies the value of its own fields
47      * and verifies that requested FIELD_MODIFICATION events occur and
48      * correct type tag is returned for each event.
49      */
testFieldModifyEvent()50     public void testFieldModifyEvent() {
51 
52         logWriter.println("FieldModification002Test started");
53 
54         //check capability, relevant for this test
55         logWriter.println("=> Check capability: canWatchFieldModification");
56         debuggeeWrapper.vmMirror.capabilities();
57         boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canWatchFieldModification;
58         if (!isCapability) {
59             logWriter.println("##WARNING: this VM doesn't possess capability: canWatchFieldModification");
60             return;
61         }
62 
63         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
64         String classSignature = "L" + getDebuggeeClassName().replace('.', '/') + ";";
65 
66         hookFieldModification(classSignature, "testBoolField", JDWPConstants.Tag.BOOLEAN_TAG);
67         hookFieldModification(classSignature, "testByteField", JDWPConstants.Tag.BYTE_TAG);
68         hookFieldModification(classSignature, "testCharField", JDWPConstants.Tag.CHAR_TAG);
69         hookFieldModification(classSignature, "testShortField", JDWPConstants.Tag.SHORT_TAG);
70         hookFieldModification(classSignature, "testIntField", JDWPConstants.Tag.INT_TAG);
71         hookFieldModification(classSignature, "testLongField", JDWPConstants.Tag.LONG_TAG);
72         hookFieldModification(classSignature, "testFloatField", JDWPConstants.Tag.FLOAT_TAG);
73         hookFieldModification(classSignature, "testDoubleField", JDWPConstants.Tag.DOUBLE_TAG);
74         hookFieldModification(classSignature, "testObjectField", JDWPConstants.Tag.OBJECT_TAG);
75         hookFieldModification(classSignature, "testThreadField", JDWPConstants.Tag.THREAD_TAG);
76         hookFieldModification(classSignature, "testThreadGroupField", JDWPConstants.Tag.THREAD_GROUP_TAG);
77         hookFieldModification(classSignature, "testClassField", JDWPConstants.Tag.CLASS_OBJECT_TAG);
78         hookFieldModification(classSignature, "testClassLoaderField", JDWPConstants.Tag.CLASS_LOADER_TAG);
79         hookFieldModification(classSignature, "testStringField", JDWPConstants.Tag.STRING_TAG);
80         hookFieldModification(classSignature, "testIntArrayField", JDWPConstants.Tag.ARRAY_TAG);
81         hookFieldModification(classSignature, "testStringArrayField", JDWPConstants.Tag.ARRAY_TAG);
82         hookFieldModification(classSignature, "testObjectArrayField", JDWPConstants.Tag.ARRAY_TAG);
83 
84         logWriter.println("FieldModification002Test done");
85     }
86 
87     /**
88      * Sets FIELD_MODIFICATION breakpoint,
89      * synchrinizes debuggee,
90      * expects field notification event,
91      * verifies new value assigned to the field
92      * and clears set breakpoint.
93      *
94      * @param classSignature signature of class containing the given field
95      * @param fieldName the name of field to break on modification
96      * @param expectedTag expected type tag of new values assigned to the field
97      */
hookFieldModification(String classSignature, String fieldName, byte expectedTag)98     public void hookFieldModification(String classSignature, String fieldName, byte expectedTag) {
99 
100         // set breakpoint
101         logWriter.println("Set hook for: " + fieldName);
102         ReplyPacket reply = debuggeeWrapper.vmMirror.setFieldModification(classSignature, JDWPConstants.TypeTag.CLASS, fieldName);
103         checkReplyPacket(reply, "Set FIELD_MODIFICATION event");
104         int requestID = reply.getNextValueAsInt();
105         assertAllDataRead(reply);
106 
107         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
108 
109         EventPacket event = debuggeeWrapper.vmMirror.receiveEvent();
110         ParsedEvent[] parsedEvents = ParsedEvent.parseEventPacket(event);
111 
112         // assert that event is the expected one
113         assertEquals("Invalid number of events,", 1, parsedEvents.length);
114         assertEquals("Invalid event kind,",
115                 JDWPConstants.EventKind.FIELD_MODIFICATION,
116                 parsedEvents[0].getEventKind(),
117                 JDWPConstants.EventKind.getName(JDWPConstants.EventKind.FIELD_MODIFICATION),
118                 JDWPConstants.EventKind.getName(parsedEvents[0].getEventKind()));
119 
120         Value value =
121             ((ParsedEvent.Event_FIELD_MODIFICATION)parsedEvents[0]).getValueToBe();
122         byte tag = value.getTag();
123         assertEquals("Invalid value tag,",
124                 expectedTag,
125                 tag,
126                 JDWPConstants.Tag.getName(expectedTag),
127                 JDWPConstants.Tag.getName(tag));
128 
129         TaggedObject modifiedField =
130             ((ParsedEvent.Event_FIELD_MODIFICATION)parsedEvents[0]).getObject();
131 
132         // assert that exception class is the expected one
133         long typeID = getObjectReferenceType(modifiedField.objectID);
134         String returnedExceptionSignature = getClassSignature(typeID);
135         assertString("Invalid class signature,",
136                 classSignature, returnedExceptionSignature);
137 
138         logWriter.println("Field: " + fieldName +
139                 ", tag of new value: " + value +
140                 ", tag: " + (char)tag+ " - OK");
141 
142         // clear breakpoint
143         clearEvent(JDWPConstants.EventKind.FIELD_MODIFICATION, requestID, false);
144 
145         // and resume target VM
146         resumeDebuggee();
147     }
148 }
149