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.MethodEntryRequest.addClassFilter_rt;
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  * MethodEntryRequest.
40  *
41  * The test checks that results of the method
42  * <code>com.sun.jdi.MethodEntryRequest.addClassFilter(ReferenceType)</code>
43  * complies with its spec.
44  *
45  * The test checks up on the following assertion:
46  *    Restricts the events generated by this request to be
47  *    the preparation of the given reference type and any subtypes.
48  * The cases to test include re-invocations of the method
49  * addClassFilter() on the same MethodEntryRequest object.
50  * There are two MethodEntryRequests to check as follows:
51  * (1) For MethodEntryRequest2, both invocations are with different
52  *     ReferenceTypes restricting one MethodEntry event to two classes.
53  *     The test expects no MethodEntry event will be received.
54  * (2) For MethodEntryRequest1, both invocations are with the same
55  *     ReferenceType restricting one MethodEntry event to one class.
56  *     The test expects this MethodEntry event will be received.
57  *
58  * The test works as follows.
59  * - The debugger resumes the debuggee and waits for the BreakpointEvent.
60  * - The debuggee creates two threads, thread1 and thread2,
61  *   invoking methods correspondingly
62  *   in the super-class filter_rt003aTestClass10 and its sub-class filter_rt003aTestClass11 and
63  *   in the super-class filter_rt003aTestClass20 and its sub-class filter_rt003aTestClass21,
64  *   and invokes the methodForCommunication to be suspended and
65  *   to inform the debugger with the event.
66  * - Upon getting the BreakpointEvent, the debugger
67  *   - gets ReferenceTypes 1&2 for the Classes to filter,
68  *   - sets up two MethodEntryRequests 1&2,
69  *   - double restricts MethodEntryRequest1 to the RefTypes 1 and 1,
70  *   - double restricts MethodEntryRequest2 to the RefTypes 1 and 2,
71  *   - resumes debuggee's main thread, and
72  *   - waits for the event.
73  * - The debuggee creates and starts two threads, thread1 and thread2,
74  *   generating the events to be filtered.
75  * - Upon getting the events, the debugger performs checks required.
76  */
77 
78 public class filter_rt003 extends TestDebuggerType1 {
79 
main(String argv[])80     public static void main (String argv[]) {
81         System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
82     }
83 
run(String argv[], PrintStream out)84     public static int run (String argv[], PrintStream out) {
85         debuggeeName = "nsk.jdi.MethodEntryRequest.addClassFilter_rt.filter_rt003a";
86         return new filter_rt003().runThis(argv, out);
87     }
88 
89     private String testedClassName11 =
90       "nsk.jdi.MethodEntryRequest.addClassFilter_rt.filter_rt003aTestClass11";
91 
92     private String testedClassName21 =
93       "nsk.jdi.MethodEntryRequest.addClassFilter_rt.filter_rt003aTestClass21";
94 
testRun()95     protected void testRun() {
96 
97         EventRequest  eventRequest1      = null;
98         EventRequest  eventRequest2      = null;
99 
100         String        property1          = "MethodEntryRequest1";
101         String        property2          = "MethodEntryRequest2";
102 
103         ReferenceType testClassReference11 = null;
104         ReferenceType testClassReference21 = null;
105 
106         for (int i = 0; ; i++) {
107 
108             if (!shouldRunAfterBreakpoint()) {
109                 vm.resume();
110                 break;
111             }
112 
113             display(":::::: case: # " + i);
114 
115             switch (i) {
116 
117                 case 0:
118                 testClassReference11 = (ReferenceType) debuggee.classByName(testedClassName11);
119                 testClassReference21 = (ReferenceType) debuggee.classByName(testedClassName21);
120 
121                 eventRequest1 = setting21MethodEntryRequest(null, testClassReference11,
122                                                    EventRequest.SUSPEND_ALL, property1);
123 
124                 eventRequest2 = setting21MethodEntryRequest(null, testClassReference11,
125                                                    EventRequest.SUSPEND_ALL, property2);
126 
127                 ((MethodEntryRequest) eventRequest1).addClassFilter(testClassReference11);
128                 ((MethodEntryRequest) eventRequest2).addClassFilter(testClassReference21);
129 
130                 display("......waiting for MethodEntryEvent in expected thread");
131                 Event newEvent = eventHandler.waitForRequestedEvent(new EventRequest[]{eventRequest1, eventRequest2}, waitTime, false);
132 
133                 if ( !(newEvent instanceof MethodEntryEvent)) {
134                     setFailedStatus("ERROR: new event is not MethodEntryEvent");
135                 } else {
136 
137                     String property = (String) newEvent.request().getProperty("number");
138                     display("       got new MethodEntryEvent with property 'number' == " + property);
139 
140                     if ( !property.equals(property1) ) {
141                         setFailedStatus("ERROR: property is not : " + property1);
142                     }
143 
144                     ReferenceType refType = ((MethodEntryEvent)newEvent).location().declaringType();
145                     if (!refType.equals(testClassReference11)) {
146                         setFailedStatus("Received unexpected declaring type of the event: " + refType.name() +
147                             "\n\texpected one: " + testClassReference11.name());
148                     }
149                 }
150                 vm.resume();
151                 break;
152 
153                 default:
154                 throw new Failure("** default case 2 **");
155             }
156         }
157         return;
158     }
159 
setting21MethodEntryRequest( ThreadReference thread, ReferenceType testedClass, int suspendPolicy, String property )160     private MethodEntryRequest setting21MethodEntryRequest ( ThreadReference thread,
161                                                              ReferenceType    testedClass,
162                                                              int              suspendPolicy,
163                                                              String           property       ) {
164         try {
165             display("......setting up MethodEntryRequest:");
166             display("       thread: " + thread + "; class: " + testedClass +  "; property: " + property);
167 
168             MethodEntryRequest
169             menr = eventRManager.createMethodEntryRequest();
170             menr.putProperty("number", property);
171             if (thread != null)
172                 menr.addThreadFilter(thread);
173             menr.addClassFilter(testedClass);
174             menr.setSuspendPolicy(suspendPolicy);
175 
176             display("      a MethodEntryRequest has been set up");
177             return menr;
178         } catch ( Exception e ) {
179             throw new Failure("** FAILURE to set up MethodEntryRequest **");
180         }
181     }
182 }
183