1 /*
   2  * Copyright (c) 2019, 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 8229212
  27  * @summary Verify that monitor operations by a non-owner thread throw
  28  *          IllegalMonitorStateException.
  29  * @run main NonOwnerOps
  30  */
  31 
  32 public class NonOwnerOps {
  33     public static void main(String[] args) {
  34         int error_count = 0;
  35         Object obj;
  36 
  37         obj = new Object();
  38         try {
  39             obj.wait();
  40             System.err.println("ERROR: wait() by non-owner thread did not " +
  41                                "throw IllegalMonitorStateException.");
  42             error_count++;
  43         } catch (InterruptedException ie) {
  44             System.err.println("ERROR: wait() by non-owner thread threw " +
  45                                "InterruptedException which is not expected.");
  46             error_count++;
  47         } catch (IllegalMonitorStateException imse) {
  48             System.out.println("wait() by non-owner thread threw the " +
  49                                "expected IllegalMonitorStateException:");
  50             System.out.println("    " + imse);
  51         }
  52 
  53         obj = new Object();
  54         try {
  55             obj.notify();
  56             System.err.println("ERROR: notify() by non-owner thread did not " +
  57                                "throw IllegalMonitorStateException.");
  58             error_count++;
  59         } catch (IllegalMonitorStateException imse) {
  60             System.out.println("notify() by non-owner thread threw the " +
  61                                "expected IllegalMonitorStateException:");
  62             System.out.println("    " + imse);
  63         }
  64 
  65         obj = new Object();
  66         try {
  67             obj.notifyAll();
  68             System.err.println("ERROR: notifyAll() by non-owner thread did " +
  69                                "not throw IllegalMonitorStateException.");
  70             error_count++;
  71         } catch (IllegalMonitorStateException imse) {
  72             System.out.println("notifyAll() by non-owner thread threw the " +
  73                                "expected IllegalMonitorStateException:");
  74             System.out.println("    " + imse);
  75         }
  76 
  77         if (error_count != 0) {
  78             throw new RuntimeException("Test failed with " + error_count +
  79                                        " errors.");
  80         }
  81         System.out.println("Test PASSED.");
  82     }
  83 }