1 /*
2  * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package nsk.jdi.WatchpointEvent._itself_;
25 
26 import nsk.share.*;
27 import nsk.share.jpda.*;
28 import nsk.share.jdi.*;
29 
30 import com.sun.jdi.*;
31 import com.sun.jdi.event.*;
32 import com.sun.jdi.request.*;
33 
34 import java.util.*;
35 import java.io.*;
36 
37 /**
38  * The test for the implementation of an object of the type
39  * WatchpointEvent.
40  *
41  * The test checks that results of methods in the interface
42  * <code>com.sun.jdi.WatchpointEvent</code>
43  * complies with its spec.
44  *
45  * The test checks up on the following assertion:
46  *    The number of WatchpointEvents received is equal to
47  *    the number of WatchpointRequestsset up by a debugger.
48  * Tested are both Access and Modification WatchpointEvents.
49  *
50  * The test works as follows.
51  * - The debugger resumes the debuggee and waits for the BreakpointEvent.
52  * - The debuggee an object foo of CheckedClass and invokes
53  *   the methodForCommunication to be suspended and
54  *   to inform the debugger with the event.
55  * - Upon getting the BreakpointEvent, the debugger:
56  *   - set up enabled WatchpointRequests for all fields,
57  *   - resumes the debuggee, and
58  *   - waiting for all expected events to receive.
59  */
60 
61 public class wevent001 extends TestDebuggerType1 {
62 
main(String argv[])63     public static void main (String argv[]) {
64         System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
65     }
66 
run(String argv[], PrintStream out)67     public static int run (String argv[], PrintStream out) {
68         debuggeeName = "nsk.jdi.WatchpointEvent._itself_.wevent001a";
69         return new wevent001().runThis(argv, out);
70     }
71 
72     private String testedClassName =
73        "nsk.jdi.WatchpointEvent._itself_.CheckedClass";
74 
75     volatile int mwEventsCount = 0;
76     volatile int awEventsCount = 0;
77     volatile int requestsCount = 0;
78 
testRun()79     protected void testRun() {
80 
81         if ( !vm.canWatchFieldModification() ) {
82             display("......vm.canWatchFieldModification == false :: test cancelled");
83             vm.exit(Consts.JCK_STATUS_BASE);
84             return;
85         }
86         if ( !vm.canWatchFieldAccess() ) {
87             display("......vm.canWatchFieldAccess == false :: test cancelled");
88             vm.exit(Consts.JCK_STATUS_BASE);
89             return;
90         }
91 
92         ReferenceType refType = null;
93         List          fields  = null;
94         ListIterator  li      = null;
95         Field         field   = null;
96 
97         ModificationWatchpointRequest mwRequest = null;
98         AccessWatchpointRequest       awRequest = null;
99 
100         for (int i = 0; ; i++) {
101 
102             if (!shouldRunAfterBreakpoint()) {
103                 vm.resume();
104                 break;
105             }
106 
107             display(":::::: case: # " + i);
108 
109             switch (i) {
110                 case 0:
111                 refType = (ReferenceType) vm.classesByName(testedClassName).get(0);
112                 fields = refType.fields();
113                 li = fields.listIterator();
114 
115                 display("......setting up WatchpointRequests");
116                 while (li.hasNext()) {
117                     field = (Field) li.next();
118                     if (!field.isSynthetic()) {
119                         mwRequest = eventRManager.createModificationWatchpointRequest(field);
120                         mwRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
121                         mwRequest.enable();
122 
123                         awRequest = eventRManager.createAccessWatchpointRequest(field);
124                         awRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
125                         awRequest.enable();
126 
127                         requestsCount++;
128                     }
129                 }
130                 display("       # Requests set up : " + requestsCount);
131 
132                 eventHandler.addListener(
133                      new EventHandler.EventListener() {
134                          public boolean eventReceived(Event event) {
135                             if (event instanceof ModificationWatchpointEvent) {
136                                 display("Received ModificationWatchpointEvent ");
137                                 mwEventsCount++;
138                                 return true;
139                             } else if (event instanceof AccessWatchpointEvent) {
140                                 display("Received AccessWatchpointEvent ");
141                                 awEventsCount++;
142                                 return true;
143                             }
144                             if (gotAllRequestedEvents()) {
145                                 synchronized (eventHandler) {
146                                     eventHandler.notifyAll();
147                                 }
148                             }
149                             return false;
150                          }
151                      }
152                 );
153 
154                 display("......waiting for WatchpointEvents");
155                 vm.resume();
156 
157                 long timeToFinish = System.currentTimeMillis() + waitTime;
158                 long timeLeft = waitTime;
159                 while (!gotAllRequestedEvents() && timeLeft > 0) {
160                     try {
161                         synchronized (eventHandler) {
162                             eventHandler.wait(timeLeft);
163                             timeLeft = timeToFinish - System.currentTimeMillis();
164                         }
165                     } catch (InterruptedException e) {
166                         setFailedStatus("InterruptedException was thrown while waiting for the events.");
167                         throw new Failure(e);
168                     }
169                 }
170 
171                 display("......checking up on numbers");
172                 if ( mwEventsCount != requestsCount ) {
173                     setFailedStatus("ERROR: # ModificationWatchpointEvents != # Requests :: " +
174                          mwEventsCount + " != " + requestsCount);
175                 }
176                 if ( awEventsCount != requestsCount ) {
177                     setFailedStatus("ERROR: # AccessWatchpointEvents != # Requests :: " +
178                          awEventsCount + " != " + requestsCount);
179                 }
180 
181                 display("      # mwEventsCount == " + mwEventsCount);
182                 display("      # awEventsCount == " + awEventsCount);
183 
184                 break;
185 
186                 default:
187                 throw new Failure("** default case 1 **");
188             }
189         }
190         return;
191     }
192 
gotAllRequestedEvents()193     boolean gotAllRequestedEvents() {
194         return (mwEventsCount >= requestsCount && awEventsCount >= requestsCount);
195     }
196 }
197