Home | History | Annotate | Download | only in monitor_enter
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package dot.junit.opcodes.monitor_enter;
     18 
     19 import dot.junit.DxTestCase;
     20 import dot.junit.DxUtil;
     21 import dot.junit.opcodes.monitor_enter.d.T_monitor_enter_1;
     22 import dot.junit.opcodes.monitor_enter.d.T_monitor_enter_2;
     23 import dot.junit.opcodes.monitor_enter.d.T_monitor_enter_3;
     24 
     25 public class Test_monitor_enter extends DxTestCase {
     26 
     27     /**
     28      * @title tests monitor-enter functionality
     29      *
     30      * @throws InterruptedException
     31      */
     32     public void testN1() throws InterruptedException {
     33         //@uses dot.junit.opcodes.monitor_enter.TestRunnable
     34         final T_monitor_enter_1 t1 = new T_monitor_enter_1();
     35         Runnable r1 = new TestRunnable(t1);
     36         Runnable r2 = new TestRunnable(t1);
     37         Thread tr1 = new Thread(r1);
     38         Thread tr2 = new Thread(r2);
     39         tr1.start();
     40         tr2.start();
     41 
     42         tr1.join();
     43         tr2.join();
     44         assertEquals(2, t1.counter);
     45     }
     46 
     47     /**
     48      * @title Tests behavior when monitor owned by current thread.
     49      *
     50      * @throws InterruptedException
     51      */
     52     public void testN2() throws InterruptedException {
     53         //@uses dot.junit.opcodes.monitor_enter.TestRunnable2
     54         final T_monitor_enter_2 t1 = new T_monitor_enter_2();
     55         Runnable r1 = new TestRunnable2(t1, 10);
     56         Runnable r2 = new TestRunnable2(t1, 20);
     57         Thread tr1 = new Thread(r1);
     58         Thread tr2 = new Thread(r2);
     59         tr1.start();
     60         tr2.start();
     61 
     62         tr1.join();
     63         tr2.join();
     64         assertTrue(t1.result);
     65     }
     66 
     67 
     68     /**
     69      * @title expected NullPointerException
     70      */
     71     public void testE1() {
     72         T_monitor_enter_3 t = new T_monitor_enter_3();
     73         try {
     74             t.run();
     75             fail("expected NullPointerException");
     76         } catch (NullPointerException npe) {
     77             // expected
     78         }
     79     }
     80 
     81     /**
     82      * @constraint A23
     83      * @title  number of registers
     84      */
     85     public void testVFE1() {
     86         try {
     87             Class.forName("dot.junit.opcodes.monitor_enter.d.T_monitor_enter_4");
     88             fail("expected a verification exception");
     89         } catch (Throwable t) {
     90             DxUtil.checkVerifyException(t);
     91         }
     92     }
     93 
     94     /**
     95      * @constraint B1
     96      * @title  types of arguments - int
     97      */
     98     public void testVFE2() {
     99         try {
    100             Class.forName("dot.junit.opcodes.monitor_enter.d.T_monitor_enter_5");
    101             fail("expected a verification exception");
    102         } catch (Throwable t) {
    103             DxUtil.checkVerifyException(t);
    104         }
    105     }
    106 
    107     /**
    108      * @constraint B1
    109      * @title  types of arguments - float
    110      */
    111     public void testVFE3() {
    112         try {
    113             Class.forName("dot.junit.opcodes.monitor_enter.d.T_monitor_enter_6");
    114             fail("expected a verification exception");
    115         } catch (Throwable t) {
    116             DxUtil.checkVerifyException(t);
    117         }
    118     }
    119 
    120     /**
    121      * @constraint B1
    122      * @title  types of arguments - long
    123      */
    124     public void testVFE4() {
    125         try {
    126             Class.forName("dot.junit.opcodes.monitor_enter.d.T_monitor_enter_7");
    127             fail("expected a verification exception");
    128         } catch (Throwable t) {
    129             DxUtil.checkVerifyException(t);
    130         }
    131     }
    132 
    133     /**
    134      * @constraint B1
    135      * @title  types of arguments - double
    136      */
    137     public void testVFE5() {
    138         try {
    139             Class.forName("dot.junit.opcodes.monitor_enter.d.T_monitor_enter_8");
    140             fail("expected a verification exception");
    141         } catch (Throwable t) {
    142             DxUtil.checkVerifyException(t);
    143         }
    144     }
    145 
    146 }
    147 
    148 class TestRunnable implements Runnable {
    149     private T_monitor_enter_1 t1;
    150     TestRunnable(T_monitor_enter_1 t1) {
    151         this.t1 = t1;
    152     }
    153 
    154     public void run() {
    155         try {
    156             t1.run();
    157         } catch (InterruptedException e) {
    158             throw new RuntimeException("interrupted!");
    159         }
    160     }
    161 }
    162 
    163 class TestRunnable2 implements Runnable {
    164     private T_monitor_enter_2 t2;
    165     private int val;
    166     TestRunnable2(T_monitor_enter_2 t2, int val) {
    167         this.t2 = t2;
    168         this.val = val;
    169     }
    170 
    171     public void run() {
    172         try {
    173             t2.run(val);
    174         } catch (InterruptedException e) {
    175             throw new RuntimeException("interrupted!");
    176         }
    177     }
    178 }
    179 
    180