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 package org.apache.harmony.jpda.tests.jdwp.Events;
20 
21 import org.apache.harmony.jpda.tests.framework.jdwp.EventBuilder;
22 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
23 import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
24 import org.apache.harmony.jpda.tests.framework.jdwp.TaggedObject;
25 import org.apache.harmony.jpda.tests.framework.jdwp.VmMirror;
26 
27 /**
28  *
29  * JDWP Unit test for FIELD_ACCESS and FIELD_MODIFICATION events with
30  * LocationOnly modifier.
31  */
32 public class FieldWithLocationTest extends EventLocationEventTestCase {
33 
34     private static final String DEBUGGEE_SIGNATURE =
35             "Lorg/apache/harmony/jpda/tests/jdwp/Events/FieldWithLocationDebuggee;";
36     private static final String FIELD_NAME = "testIntField";
37 
38     // Cache debuggee class ID.
39     private long debuggeeClassId = -1;
40 
41     // Cache field ID.
42     private long fieldId = -1;
43 
44     /**
45      * This testcase is for FIELD_ACCESS event.
46      * <BR>It runs FieldDebuggee that accesses to the value of its internal field
47      * and verify that requested FIELD_ACCESS event occurs in the
48      * expected method.
49      */
testFieldAccessLocationEvent()50     public void testFieldAccessLocationEvent() {
51         logWriter.println("testFieldAccessLocationEvent started");
52 
53         runFieldLocationTest(false);
54 
55         logWriter.println("testFieldAccessLocationEvent done");
56     }
57 
58     /**
59      * This testcase is for FIELD_MODIFICATION event.
60      * <BR>It runs FieldDebuggee that modifies the value of its internal field
61      * and verify that requested FIELD_MODIFICATION event occurs in the
62      * expected method.
63      */
testFieldModificationLocationEvent()64     public void testFieldModificationLocationEvent() {
65         logWriter.println("testFieldModificationLocationEvent started");
66 
67         runFieldLocationTest(true);
68 
69         logWriter.println("testFieldModificationLocationEvent done");
70     }
71 
72     @Override
getDebuggeeClassName()73     protected final String getDebuggeeClassName() {
74         return FieldWithLocationDebuggee.class.getName();
75     }
76 
77     @Override
getDebuggeeSignature()78     protected final String getDebuggeeSignature() {
79         return DEBUGGEE_SIGNATURE;
80     }
81 
82     @Override
getExpectedLocationMethodName()83     protected final String getExpectedLocationMethodName() {
84         return "expectedMethodForFieldEvent";
85     }
86 
87     @Override
createEventBuilder(EventBuilder builder)88     protected final void createEventBuilder(EventBuilder builder) {
89         if (debuggeeClassId == -1) {
90             debuggeeClassId = getClassIDBySignature(DEBUGGEE_SIGNATURE);
91         }
92         if (fieldId == -1) {
93             fieldId = debuggeeWrapper.vmMirror.getFieldID(debuggeeClassId, FIELD_NAME);
94         }
95         builder.setFieldOnly(debuggeeClassId, fieldId);
96     }
97 
98     @Override
checkEvent(ParsedEvent event)99     protected void checkEvent(ParsedEvent event) {
100         TaggedObject accessedField = null;
101         byte fieldEventKind = event.getEventKind();
102         if (fieldEventKind  == JDWPConstants.EventKind.FIELD_ACCESS) {
103             accessedField = ((ParsedEvent.Event_FIELD_ACCESS)event).getObject();
104         } else if (fieldEventKind == JDWPConstants.EventKind.FIELD_MODIFICATION) {
105             accessedField = ((ParsedEvent.Event_FIELD_MODIFICATION)event).getObject();
106         }
107 
108         // Check the field receiver is an instance of our debuggee class.
109         long typeID = getObjectReferenceType(accessedField.objectID);
110         String returnedExceptionSignature = getClassSignature(typeID);
111         assertString("Invalid class signature,",
112                 DEBUGGEE_SIGNATURE, returnedExceptionSignature);
113     }
114 
supportFieldCapability(boolean modification)115     private boolean supportFieldCapability(boolean modification) {
116         VmMirror mirror = debuggeeWrapper.vmMirror;
117         return modification ? mirror.canWatchFieldModification() :
118             mirror.canWatchFieldAccess();
119     }
120 
getFieldCapabilityName(boolean modification)121     private static String getFieldCapabilityName(boolean modification) {
122         return modification ? "canWatchFieldModification" :
123             "canWatchFieldAccess";
124     }
125 
getFieldEventKind(boolean modification)126     private static byte getFieldEventKind(boolean modification) {
127         return modification ? JDWPConstants.EventKind.FIELD_MODIFICATION :
128             JDWPConstants.EventKind.FIELD_ACCESS;
129     }
130 
runFieldLocationTest(boolean modification)131     private void runFieldLocationTest(boolean modification) {
132         final byte eventKind = getFieldEventKind(modification);
133         final String capabilityname = getFieldCapabilityName(modification);
134 
135         logWriter.println("Check capability " + capabilityname);
136         if (supportFieldCapability(modification)) {
137             runEventWithLocationTest(eventKind);
138         } else {
139             logWriter.println("##WARNING: this VM doesn't possess capability " +
140                     capabilityname);
141         }
142     }
143 }
144