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.StepRequest.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  * StepRequest.
40  *
41  * The test checks that results of the method
42  * <code>com.sun.jdi.StepRequest.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 StepRequest object.
50  * There are two StepRequests to check as follows:
51  * (1) For StepRequest2, both invocations are with different
52  *     ReferenceTypes restricting one Step event to two classes.
53  *     The test expects no Step event will be received.
54  * (2) For StepRequest1, both invocations are with the same
55  *     ReferenceType restricting one Step event to one class.
56  *     The test expects this Step 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  *   thread1 will invoke methods in filter_rt003aTestClass10 and filter_rt003aTestClass11, and
62  *   thread2 in filter_rt003aTestClass20 and filter_rt003aTestClass21,
63  *   and steps into methods are objects to test;
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 StepRequests 1&2,
69  *   - double restricts StepRequest1 to the RefTypes 1 and 1,
70  *   - double restricts StepRequest2 to the RefTypes 1 and 2,
71  *   - resumes debuggee's main thread, and
72  *   - waits for the event.
73  * - Upon getting the events, the debugger performs checks required.
74  */
75 
76 public class filter_rt003 extends TestDebuggerType1 {
77 
main(String argv[])78     public static void main (String argv[]) {
79         System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
80     }
81 
run(String argv[], PrintStream out)82     public static int run (String argv[], PrintStream out) {
83         debuggeeName = "nsk.jdi.StepRequest.addClassFilter_rt.filter_rt003a";
84         return new filter_rt003().runThis(argv, out);
85     }
86 
87     private String testedClassName11 =
88         "nsk.jdi.StepRequest.addClassFilter_rt.filter_rt003aTestClass11";
89     private String testedClassName21 =
90         "nsk.jdi.StepRequest.addClassFilter_rt.filter_rt003aTestClass21";
91 
testRun()92     protected void testRun() {
93 
94         EventRequest  eventRequest1 = null;
95         EventRequest  eventRequest2 = null;
96 
97         String        property1     = "StepRequest1";
98         String        property2     = "StepRequest2";
99 
100         ReferenceType testClassReference11 = null;
101         ReferenceType testClassReference21 = null;
102 
103         ThreadReference thread1     = null;
104         String          threadName1 = "thread1";
105 
106         ThreadReference thread2     = null;
107         String          threadName2 = "thread2";
108 
109         for (int i = 0; ; i++) {
110 
111             if (!shouldRunAfterBreakpoint()) {
112                 vm.resume();
113                 break;
114             }
115 
116             display(":::::: case: # " + i);
117 
118             switch (i) {
119 
120                 case 0:
121                 testClassReference11 = (ReferenceType)debuggee.classByName(testedClassName11);
122                 testClassReference21 = (ReferenceType)debuggee.classByName(testedClassName21);
123 
124                 thread1 = debuggee.threadByName(threadName1);
125                 thread2 = debuggee.threadByName(threadName2);
126 
127                 eventRequest1 = setting21StepRequest(thread1, testClassReference11,
128                                              EventRequest.SUSPEND_ALL, property1);
129 
130                 eventRequest2 = setting21StepRequest(thread2, testClassReference11,
131                                              EventRequest.SUSPEND_ALL, property2);
132 
133                 ((StepRequest) eventRequest1).addClassFilter(testClassReference11);
134                 ((StepRequest) eventRequest2).addClassFilter(testClassReference21);
135 
136                 display("......waiting for StepEvent in expected thread");
137                 Event newEvent = eventHandler.waitForRequestedEvent(new EventRequest[]{eventRequest1, eventRequest2}, waitTime, false);
138 
139                 if ( !(newEvent instanceof StepEvent)) {
140                     setFailedStatus("ERROR: new event is not StepEvent");
141                 } else {
142 
143                     String property = (String) newEvent.request().getProperty("number");
144                     display("       got new StepEvent with property 'number' == " + property);
145 
146                     if ( !property.equals(property1) ) {
147                         setFailedStatus("ERROR: property is not : " + property1);
148                     }
149 
150                     ReferenceType refType = ((StepEvent)newEvent).location().declaringType();
151                     if (!refType.equals(testClassReference11)) {
152                         setFailedStatus("Received unexpected declaring type of the event: " + refType.name() +
153                             "\n\texpected one: " + testClassReference11.name());
154                     }
155                 }
156                 vm.resume();
157                 break;
158 
159                 default:
160                 throw new Failure("** default case 2 **");
161             }
162         }
163         return;
164     }
165 
setting21StepRequest( ThreadReference thread, ReferenceType testedClass, int suspendPolicy, String property )166     private StepRequest setting21StepRequest ( ThreadReference thread,
167                                                ReferenceType   testedClass,
168                                                int             suspendPolicy,
169                                                String          property        ) {
170         try {
171             display("......setting up StepRequest:");
172             display("       thread: " + thread + "; property: " + property);
173 
174             StepRequest
175             str = eventRManager.createStepRequest(thread, StepRequest.STEP_LINE, StepRequest.STEP_INTO);
176             str.putProperty("number", property);
177             str.setSuspendPolicy(suspendPolicy);
178             str.addClassFilter(testedClass);
179             str.addCountFilter(1);
180 
181             display("      StepRequest has been set up");
182             return str;
183         } catch ( Exception e ) {
184             throw new Failure("** FAILURE to set up StepRequest **");
185         }
186     }
187 }
188