1 /*
   2  * Copyright (c) 2001, 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 4434232
  27  *  @summary Test ThreadReference.frames(int,int)
  28  *
  29  *  @author Robert Field
  30  *
  31  *  @run build TestScaffold VMConnection TargetListener TargetAdapter
  32  *  @run compile -g FramesTest.java
  33  *  @run driver FramesTest
  34  */
  35 import com.sun.jdi.*;
  36 import com.sun.jdi.event.*;
  37 import com.sun.jdi.request.*;
  38 
  39 import java.util.*;
  40 
  41     /********** target program **********/
  42 
  43 class FramesTarg {
  44     static void foo3() {
  45         System.out.println("executing foo3");
  46     }
  47     static void foo2() {
  48         foo3();
  49     }
  50     static void foo1() {
  51         foo2();
  52     }
  53     public static void main(String[] args){
  54         System.out.println("Howdy!");
  55         foo1();
  56     }
  57 }
  58 
  59     /********** test program **********/
  60 
  61 public class FramesTest extends TestScaffold {
  62     ReferenceType targetClass;
  63     ThreadReference mainThread;
  64 
  65     static String[] expectedNames = {"foo3", "foo2", "foo1", "main"};
  66 
  67     FramesTest (String args[]) {
  68         super(args);
  69     }
  70 
  71     public static void main(String[] args)      throws Exception {
  72         new FramesTest(args).startTests();
  73     }
  74 
  75     /********** test assist **********/
  76 
  77     void exceptionTest(int start, int length) {
  78         boolean gotException = false;
  79         try {
  80             mainThread.frames(start, length);
  81         } catch (IndexOutOfBoundsException exc) {
  82             gotException = true;
  83         } catch (Exception exc) {
  84             failure("unexpected exception thrown for: " +
  85                     "start = " + start + ", length = " + length +
  86                     " - " + exc);
  87             gotException = true;
  88         }
  89         if (!gotException) {
  90             failure("expected IndexOutOfBoundsException " +
  91                     "not thrown for: " +
  92                     "start = " + start + ", length = " + length);
  93         }
  94     }
  95 
  96     void nameTest(int start, int length) {
  97         try {
  98             List fs = mainThread.frames(start, length);
  99             if (fs.size() != length) {
 100                 failure("wrong length for: " +
 101                         "start = " + start + ", length = " + length);
 102             }
 103             for (int i = 0; i < length; ++i) {
 104                 StackFrame sf = (StackFrame)(fs.get(i));
 105                 String name = sf.location().method().name();
 106                 String expected = expectedNames[start+i];
 107                 if (!name.equals(expected)) {
 108                     failure("bad frame entry (" + start + "," + length +
 109                             ") - expected " + expected +
 110                             ", got " + name);
 111                 }
 112             }
 113         } catch (Exception exc) {
 114             failure("unexpected exception thrown for: " +
 115                     "start = " + start + ", length = " + length +
 116                     " - " + exc);
 117         }
 118     }
 119 
 120     /********** test core **********/
 121 
 122     protected void runTests() throws Exception {
 123         /*
 124          * Get to the top of main()
 125          * to determine targetClass and mainThread
 126          */
 127         BreakpointEvent bpe = startToMain("FramesTarg");
 128         targetClass = bpe.location().declaringType();
 129         mainThread = bpe.thread();
 130 
 131         int initialSize = mainThread.frames().size();
 132 
 133         resumeTo("FramesTarg", "foo3", "()V");
 134 
 135         if (!mainThread.frame(0).location().method().name()
 136                         .equals("foo3")) {
 137             failure("frame failed");
 138         }
 139 
 140         if (mainThread.frames().size() != (initialSize + 3)) {
 141             failure("frames size failed");
 142         }
 143 
 144         if (mainThread.frames().size() != mainThread.frameCount()) {
 145             failure("frames size not equal to frameCount");
 146         }
 147 
 148         exceptionTest(-1, 1);
 149         exceptionTest(mainThread.frameCount(), 1);
 150         exceptionTest(0, -1);
 151         exceptionTest(0, -2);
 152         exceptionTest(0, mainThread.frameCount()+1);
 153 
 154         nameTest(0, 0);
 155         nameTest(0, 1);
 156         nameTest(0, 4);
 157         nameTest(2, 2);
 158         nameTest(1, 1);
 159 
 160         /*
 161          * resume until end
 162          */
 163         listenUntilVMDisconnect();
 164 
 165         /*
 166          * deal with results of test
 167          * if anything has called failure("foo") testFailed will be true
 168          */
 169         if (!testFailed) {
 170             println("FramesTest: passed");
 171         } else {
 172             throw new Exception("FramesTest: failed");
 173         }
 174     }
 175 }