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         boolean after;
  35         int error_count = 0;
  36         Object obj;
  37 
  38         obj = new Object();
  39         after = false;
  40         try {
  41             obj.wait();
  42             after = true;
  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         if (after) {
  53             System.err.println("ERROR: wait() by non-owner thread did not " +
  54                                "throw IllegalMonitorStateException.");
  55             error_count++;
  56         }
  57 
  58         obj = new Object();
  59         after = false;
  60         try {
  61             obj.notify();
  62             after = true;
  63         } catch (IllegalMonitorStateException imse) {
  64             System.out.println("notify() by non-owner thread threw the " +
  65                                "expected IllegalMonitorStateException:");
  66             System.out.println("    " + imse);
  67         }
  68         if (after) {
  69             System.err.println("ERROR: notify() by non-owner thread did not " +
  70                                "throw IllegalMonitorStateException.");
  71             error_count++;
  72         }
  73 
  74         obj = new Object();
  75         after = false;
  76         try {
  77             obj.notifyAll();
  78             after = true;
  79         } catch (IllegalMonitorStateException imse) {
  80             System.out.println("notifyAll() by non-owner thread threw the " +
  81                                "expected IllegalMonitorStateException:");
  82             System.out.println("    " + imse);
  83         }
  84         if (after) {
  85             System.err.println("ERROR: notifyAll() by non-owner thread did " +
  86                                "not throw IllegalMonitorStateException.");
  87             error_count++;
  88         }
  89 
  90         if (error_count != 0) {
  91             throw new RuntimeException("Test failed with " + error_count +
  92                                        " errors.");
  93         }
  94         System.out.println("Test PASSED.");
  95     }
  96 }