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      */
testN1()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      */
testN2()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      */
testE1()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      */
testVFE1()85     public void testVFE1() {
86         load("dot.junit.opcodes.monitor_enter.d.T_monitor_enter_4", VerifyError.class);
87     }
88 
89     /**
90      * @constraint B1
91      * @title  types of arguments - int
92      */
testVFE2()93     public void testVFE2() {
94         load("dot.junit.opcodes.monitor_enter.d.T_monitor_enter_5", VerifyError.class);
95     }
96 
97     /**
98      * @constraint B1
99      * @title  types of arguments - float
100      */
testVFE3()101     public void testVFE3() {
102         load("dot.junit.opcodes.monitor_enter.d.T_monitor_enter_6", VerifyError.class);
103     }
104 
105     /**
106      * @constraint B1
107      * @title  types of arguments - long
108      */
testVFE4()109     public void testVFE4() {
110         load("dot.junit.opcodes.monitor_enter.d.T_monitor_enter_7", VerifyError.class);
111     }
112 
113     /**
114      * @constraint B1
115      * @title  types of arguments - double
116      */
testVFE5()117     public void testVFE5() {
118         load("dot.junit.opcodes.monitor_enter.d.T_monitor_enter_8", VerifyError.class);
119     }
120 
121 }
122 
123 class TestRunnable implements Runnable {
124     private T_monitor_enter_1 t1;
TestRunnable(T_monitor_enter_1 t1)125     TestRunnable(T_monitor_enter_1 t1) {
126         this.t1 = t1;
127     }
128 
run()129     public void run() {
130         try {
131             t1.run();
132         } catch (InterruptedException e) {
133             throw new RuntimeException("interrupted!");
134         }
135     }
136 }
137 
138 class TestRunnable2 implements Runnable {
139     private T_monitor_enter_2 t2;
140     private int val;
TestRunnable2(T_monitor_enter_2 t2, int val)141     TestRunnable2(T_monitor_enter_2 t2, int val) {
142         this.t2 = t2;
143         this.val = val;
144     }
145 
run()146     public void run() {
147         try {
148             t2.run(val);
149         } catch (InterruptedException e) {
150             throw new RuntimeException("interrupted!");
151         }
152     }
153 }
154 
155