1/*
2 * Copyright (c) 2001, 2015, 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/**
25 * @test
26 * @bug 4425840
27 * @summary RequestReflectionTest checks to see that reflective
28 * accessors on EventRequests return what they are given.
29 * @author Robert Field
30 *
31 * @run build TestScaffold VMConnection TargetListener TargetAdapter
32 * @run compile -g RequestReflectionTest.java
33 * @run driver RequestReflectionTest
34 */
35import com.sun.jdi.*;
36import com.sun.jdi.event.*;
37import com.sun.jdi.request.*;
38
39import java.util.List;
40
41
42    /********** target program **********/
43
44class RequestReflectionTarg {
45    int foo = 9;
46
47    public static void main(String args[]) {
48        System.out.println("Why, hello there...");
49        (new RequestReflectionTarg()).bar();
50    }
51
52    void bar() {
53        ++foo;
54    }
55}
56
57    /********** test program **********/
58
59public class RequestReflectionTest extends TestScaffold {
60
61    RequestReflectionTest (String args[]) {
62        super(args);
63    }
64
65    public static void main(String[] args)      throws Exception {
66        new RequestReflectionTest(args).startTests();
67    }
68
69    /********** test core **********/
70
71    protected void runTests() throws Exception {
72        /*
73         * Get to the top of main():
74         */
75        BreakpointEvent bpe = startToMain("RequestReflectionTarg");
76        ReferenceType targ = bpe.location().declaringType();
77        ThreadReference thread = bpe.thread();
78
79        Field fooField = targ.fieldByName("foo");
80        if (fooField == null) {
81            throw new Exception("test error: cannot find field foo");
82        }
83        List meths = targ.methodsByName("bar");
84        if (meths.size() != 1) {
85            throw new Exception("test error: should be one bar()");
86        }
87        Method barMethod = (Method)meths.get(0);
88
89        List exClasses = vm().classesByName("java.lang.Exception");
90        if (exClasses.size() != 1) {
91            throw new Exception(
92               "test error: should be one java.lang.Exception");
93        }
94        ReferenceType exceptionClass = (ReferenceType)exClasses.get(0);
95        EventRequestManager erm = eventRequestManager();
96
97        StepRequest sr =
98            erm.createStepRequest(thread, StepRequest.STEP_MIN,
99                                  StepRequest.STEP_OUT);
100        sr.setSuspendPolicy(EventRequest.SUSPEND_NONE);
101        sr.enable();
102        if (!sr.thread().equals(thread)) {
103            throw new Exception(
104                    "RequestReflectionTest fail: exceptions do not match " +
105                    thread + " != " + sr.thread());
106        }
107        if (sr.size() != StepRequest.STEP_MIN) {
108            throw new Exception(
109                    "RequestReflectionTest fail: size does not match " +
110                    sr.size() + " != " + StepRequest.STEP_MIN);
111        }
112        if (sr.depth() != StepRequest.STEP_OUT) {
113            throw new Exception(
114                    "RequestReflectionTest fail: depth does not match " +
115                    sr.depth() + " != " + StepRequest.STEP_OUT);
116        }
117        if (sr.suspendPolicy() != EventRequest.SUSPEND_NONE) {
118            throw new Exception(
119                    "RequestReflectionTest fail: wrong suspend policy " +
120                    sr.suspendPolicy());
121        }
122        if (!sr.isEnabled()) {
123            throw new Exception(
124                    "RequestReflectionTest fail: should be enabled");
125        }
126        sr.disable();
127
128        sr = erm.createStepRequest(thread, StepRequest.STEP_LINE,
129                                  StepRequest.STEP_INTO);
130        sr.setSuspendPolicy(EventRequest.SUSPEND_ALL);
131        if (sr.size() != StepRequest.STEP_LINE) {
132            throw new Exception(
133                    "RequestReflectionTest fail: size does not match " +
134                    sr.size() + " != " + StepRequest.STEP_LINE);
135        }
136        if (sr.depth() != StepRequest.STEP_INTO) {
137            throw new Exception(
138                    "RequestReflectionTest fail: depth does not match " +
139                    sr.depth() + " != " + StepRequest.STEP_INTO);
140        }
141        if (sr.suspendPolicy() != EventRequest.SUSPEND_ALL) {
142            throw new Exception(
143                    "RequestReflectionTest fail: wrong suspend policy " +
144                    sr.suspendPolicy());
145        }
146        if (sr.isEnabled()) {
147            throw new Exception(
148                    "RequestReflectionTest fail: should not be enabled");
149        }
150
151        AccessWatchpointRequest awr =
152            erm.createAccessWatchpointRequest(fooField);
153        if (!awr.field().equals(fooField)) {
154            throw new Exception(
155                    "RequestReflectionTest fail: fields do not match " +
156                    fooField + " != " + awr.field());
157        }
158        if (awr.suspendPolicy() != EventRequest.SUSPEND_ALL) {
159            throw new Exception(
160                    "RequestReflectionTest fail: wrong suspend policy " +
161                    awr.suspendPolicy());
162        }
163        if (awr.isEnabled()) {
164            throw new Exception(
165                    "RequestReflectionTest fail: should not be enabled");
166        }
167        BreakpointRequest bpr =
168            erm.createBreakpointRequest(barMethod.location());
169        bpr.setSuspendPolicy(EventRequest.SUSPEND_NONE);
170        bpr.enable();
171        if (!bpr.location().method().equals(barMethod)) {
172            throw new Exception(
173                    "RequestReflectionTest fail: methodss do not match " +
174                    barMethod + " != " + bpr.location().method());
175        }
176        if (bpr.suspendPolicy() != EventRequest.SUSPEND_NONE) {
177            throw new Exception(
178                    "RequestReflectionTest fail: wrong suspend policy " +
179                    bpr.suspendPolicy());
180        }
181        if (!bpr.isEnabled()) {
182            throw new Exception(
183                    "RequestReflectionTest fail: should be enabled");
184        }
185        ExceptionRequest exr =
186            erm.createExceptionRequest(exceptionClass, true, false);
187        exr.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
188        exr.enable();
189        exr.disable();
190        if (!exr.exception().equals(exceptionClass)) {
191            throw new Exception(
192                    "RequestReflectionTest fail: not java.lang.Exception " +
193                    exr.exception());
194        }
195        if (exr.suspendPolicy() != EventRequest.SUSPEND_EVENT_THREAD) {
196            throw new Exception(
197                    "RequestReflectionTest fail: wrong suspend policy " +
198                    exr.suspendPolicy());
199        }
200        if (exr.isEnabled()) {
201            throw new Exception(
202                    "RequestReflectionTest fail: should not be enabled");
203        }
204
205        listenUntilVMDisconnect();
206
207        println("RequestReflectionTest: passed");
208    }
209}
210