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 4453310
  27  * @summary Test the deletion of event requests that are expired
  28  * by virtue of addCountFilter.
  29  *
  30  * @author Robert Field
  31  *
  32  * @run build TestScaffold VMConnection TargetListener TargetAdapter
  33  * @run compile -g ExpiredRequestDeletionTest.java
  34  * @run driver ExpiredRequestDeletionTest
  35  */
  36 import com.sun.jdi.*;
  37 import com.sun.jdi.event.*;
  38 import com.sun.jdi.request.*;
  39 
  40 import java.util.*;
  41 
  42     /********** target program **********/
  43 
  44 class ExpiredRequestDeletionTarg {
  45     int foo = 9;
  46 
  47     public static void main(String[] args){
  48         System.out.println("Why, hello there...");
  49         (new ExpiredRequestDeletionTarg()).bar();
  50     }
  51 
  52     void bar() {
  53         ++foo;
  54     }
  55 }
  56 
  57     /********** test program **********/
  58 
  59 public class ExpiredRequestDeletionTest extends TestScaffold {
  60     EventRequestManager erm;
  61     ReferenceType targetClass;
  62     ThreadReference mainThread;
  63     Throwable throwable = null;
  64 
  65     ExpiredRequestDeletionTest (String args[]) {
  66         super(args);
  67     }
  68 
  69     public static void main(String[] args)      throws Exception {
  70         new ExpiredRequestDeletionTest(args).startTests();
  71     }
  72 
  73     /********** event handlers **********/
  74 
  75     public void breakpointReached(BreakpointEvent event) {
  76         try {
  77             EventRequest req = event.request();
  78             if (req != null) {
  79                 println("Deleting BreakpointRequest");
  80                 erm.deleteEventRequest(req);
  81             } else {
  82                 println("Got BreakpointEvent with null request");
  83             }
  84         } catch (Throwable exc) {
  85             throwable = exc;
  86             failure("Deleting BreakpointRequest threw - " + exc);
  87         }
  88     }
  89 
  90     public void stepCompleted(StepEvent event) {
  91         try {
  92             EventRequest req = event.request();
  93             if (req != null) {
  94                 println("Deleting StepRequest");
  95                 erm.deleteEventRequest(req);
  96             } else {
  97                 println("Got StepEvent with null request");
  98             }
  99         } catch (Throwable exc) {
 100             throwable = exc;
 101             failure("Deleting StepRequest threw - " + exc);
 102         }
 103     }
 104 
 105     /********** test core **********/
 106 
 107     protected void runTests() throws Exception {
 108         /*
 109          * Get to the top of main()
 110          * to determine targetClass and mainThread
 111          */
 112         BreakpointEvent bpe = startToMain("ExpiredRequestDeletionTarg");
 113         targetClass = bpe.location().declaringType();
 114         mainThread = bpe.thread();
 115         erm = vm().eventRequestManager();
 116 
 117         List meths = targetClass.methodsByName("bar");
 118         if (meths.size() != 1) {
 119             throw new Exception("test error: should be one bar()");
 120         }
 121         Method barMethod = (Method)meths.get(0);
 122 
 123         /*
 124          * Set event requests
 125          */
 126         StepRequest sr = erm.createStepRequest(mainThread,
 127                                                     StepRequest.STEP_LINE,
 128                                                     StepRequest.STEP_OVER);
 129         sr.addCountFilter(1);
 130         sr.enable();
 131 
 132         BreakpointRequest bpr =
 133             erm.createBreakpointRequest(barMethod.location());
 134         bpr.addCountFilter(1);
 135         bpr.enable();
 136 
 137         /*
 138          * resume the target listening for events
 139          */
 140         listenUntilVMDisconnect();
 141 
 142         /*
 143          * deal with results of test
 144          * if anything has called failure("foo") testFailed will be true
 145          */
 146         if (!testFailed) {
 147             println("ExpiredRequestDeletionTest: passed");
 148         } else {
 149             throw new Exception("ExpiredRequestDeletionTest: failed", throwable);
 150         }
 151     }
 152 }