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