• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.TestErrorException;
22 import org.apache.harmony.jpda.tests.framework.jdwp.EventBuilder;
23 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
24 import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
25 import org.apache.harmony.jpda.tests.framework.jdwp.TaggedObject;
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     private static final String FIELD_NAME = "testIntField";
34 
35     // Cache debuggee class ID.
36     private long debuggeeClassId = -1;
37 
38     // Cache field ID.
39     private long fieldId = -1;
40 
41     /**
42      * This testcase is for FIELD_ACCESS event.
43      * <BR>It runs FieldDebuggee that accesses to the value of its internal field
44      * and verify that requested FIELD_ACCESS event occurs in the
45      * expected method.
46      */
testFieldAccessLocationEvent()47     public void testFieldAccessLocationEvent() {
48         logWriter.println("testFieldAccessLocationEvent started");
49 
50         runFieldLocationTest(false);
51 
52         logWriter.println("testFieldAccessLocationEvent done");
53     }
54 
55     /**
56      * This testcase is for FIELD_MODIFICATION event.
57      * <BR>It runs FieldDebuggee that modifies the value of its internal field
58      * and verify that requested FIELD_MODIFICATION event occurs in the
59      * expected method.
60      */
testFieldModificationLocationEvent()61     public void testFieldModificationLocationEvent() {
62         logWriter.println("testFieldModificationLocationEvent started");
63 
64         runFieldLocationTest(true);
65 
66         logWriter.println("testFieldModificationLocationEvent done");
67     }
68 
69     @Override
getDebuggeeClassName()70     protected final String getDebuggeeClassName() {
71         return FieldWithLocationDebuggee.class.getName();
72     }
73 
74     @Override
getExpectedLocationMethodName()75     protected final String getExpectedLocationMethodName() {
76         return "expectedMethodForFieldEvent";
77     }
78 
79     @Override
createEventBuilder(EventBuilder builder)80     protected final void createEventBuilder(EventBuilder builder) {
81         if (debuggeeClassId == -1) {
82             debuggeeClassId = getClassIDBySignature(getDebuggeeClassSignature());
83         }
84         if (fieldId == -1) {
85             fieldId = debuggeeWrapper.vmMirror.getFieldID(debuggeeClassId, FIELD_NAME);
86         }
87         builder.setFieldOnly(debuggeeClassId, fieldId);
88     }
89 
90     @Override
checkEvent(ParsedEvent event)91     protected void checkEvent(ParsedEvent event) {
92         TaggedObject accessedField = null;
93         byte fieldEventKind = event.getEventKind();
94         if (fieldEventKind  == JDWPConstants.EventKind.FIELD_ACCESS) {
95             accessedField = ((ParsedEvent.Event_FIELD_ACCESS)event).getObject();
96         } else if (fieldEventKind == JDWPConstants.EventKind.FIELD_MODIFICATION) {
97             accessedField = ((ParsedEvent.Event_FIELD_MODIFICATION)event).getObject();
98         } else {
99             throw new TestErrorException(
100                     "Unexpected event " + JDWPConstants.EventKind.getName(fieldEventKind));
101         }
102 
103         // Check the field receiver is an instance of our debuggee class.
104         long typeID = getObjectReferenceType(accessedField.objectID);
105         String returnedExceptionSignature = getClassSignature(typeID);
106         assertString("Invalid class signature,",
107                 getDebuggeeClassSignature(), returnedExceptionSignature);
108     }
109 
getFieldCapabilityName(boolean modification)110     private static String getFieldCapabilityName(boolean modification) {
111         return modification ? "canWatchFieldModification" :
112             "canWatchFieldAccess";
113     }
114 
getFieldEventKind(boolean modification)115     private static byte getFieldEventKind(boolean modification) {
116         return modification ? JDWPConstants.EventKind.FIELD_MODIFICATION :
117             JDWPConstants.EventKind.FIELD_ACCESS;
118     }
119 
runFieldLocationTest(boolean modification)120     private void runFieldLocationTest(boolean modification) {
121         final byte eventKind = getFieldEventKind(modification);
122         final String capabilityname = getFieldCapabilityName(modification);
123 
124         logWriter.println("Check capability " + capabilityname);
125         runEventWithLocationTest(eventKind);
126     }
127 }
128