1 /*
  2  * Copyright (c) 2004, 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.jvmti.scenarios.bcinstr.BI04;
 25 
 26 import java.io.PrintStream;
 27 
 28 import nsk.share.Log;
 29 import nsk.share.Consts;
 30 
 31 import nsk.share.jvmti.ArgumentHandler;
 32 import nsk.share.jvmti.DebugeeClass;
 33 
 34 public class bi04t002 extends DebugeeClass {
 35 
 36     // run test from command line
 37     public static void main(String argv[]) {
 38         argv = nsk.share.jvmti.JVMTITest.commonInit(argv);
 39 
 40         // JCK-compatible exit
 41         System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
 42     }
 43 
 44     // run test from JCK-compatible environment
 45     public static int run(String argv[], PrintStream out) {
 46         return new bi04t002().runIt(argv, out);
 47     }
 48 
 49     /* =================================================================== */
 50 
 51     // scaffold objects
 52     ArgumentHandler argHandler = null;
 53     Log.Logger logger;
 54 
 55     /* =================================================================== */
 56 
 57     // run debuggee
 58     public int runIt(String argv[], PrintStream out) {
 59         argHandler = new ArgumentHandler(argv);
 60         Log log = new Log(out, argHandler);
 61         logger = new Log.Logger(log,"debuggee> ");
 62 
 63         if (checkStatus(Consts.TEST_PASSED) != Consts.TEST_PASSED) {
 64             return Consts.TEST_FAILED;
 65         }
 66 
 67         //checking that instrumenetation code works
 68         if (!checkInstrumentedMethods()) {
 69             logger.complain("Test FAILED");
 70             return Consts.TEST_FAILED;
 71         }
 72 
 73         logger.display("Test PASSED");
 74         return Consts.TEST_PASSED;
 75     }
 76 
 77     /** Checks instrumented methods. */
 78     boolean checkInstrumentedMethods() {
 79         //checking that instrumenetation code works
 80         boolean result = true;
 81 
 82         logger.display("Checking instrumented methods");
 83         bi04t002b thrd =  new bi04t002b();
 84 
 85         synchronized(bi04t002b.started) {
 86             thrd.start();
 87             try {
 88                 bi04t002b.started.wait();
 89                 thrd.join();
 90             } catch (InterruptedException e) {
 91             }
 92 
 93         }
 94 
 95         for (int i = 0; i < bi04t002a.TOTAL_INSTRUMENTED_METHODS; i++) {
 96 
 97             logger.display("instrumented method " + methodName(i)
 98                                     + " was invoked "
 99                                     + bi04t002a.invocationsCount[i] + " times");
100             if (bi04t002a.invocationsCount[i] <= 0) {
101                 logger.complain("Unexpected value " + bi04t002a.invocationsCount[i]);
102                 result = false;
103             }
104 
105         }
106         return result;
107     }
108 
109     String methodName(int idx) {
110         switch (idx) {
111         case bi04t002a.INSTR_EQUALS: return "equals(Object)";
112         case bi04t002a.INSTR_TOSTRING: return "toString()";
113         case bi04t002a.INSTR_WAIT_JI: return "wait(long, int)";
114         case bi04t002a.INSTR_WAIT: return "wait()";
115         }
116         logger.complain("unknown method for " + idx + " index");
117         return null;
118     }
119 }
120 
121 class bi04t002b extends Thread {
122 
123     Object obj = new Object();
124     static Object started = new Object();
125 
126     public void run() {
127 
128         synchronized(started) {
129 
130             started.notify();
131 
132             Object obj1 = new Object();
133             obj.equals(obj1);
134             obj.toString();
135 
136             synchronized (obj) {
137                 try {
138                     obj.wait(1, 1);
139                 } catch (InterruptedException e) {
140                     // do nothing
141                 }
142             }
143         }
144     }
145 }