1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 package libcore.java.lang;
18 
19 import junit.framework.TestCase;
20 
21 public class OldObjectTest extends TestCase {
22 
23     public boolean isCalled = false;
24 
25     /**
26      * Test objects.
27      */
28     Object obj1 = new Object();
29 
30     /**
31      * Generic state indicator.
32      */
33     int status = 0;
34 
35     int ready = 0;
36     TestThread1 thr1;
37     TestThread2 thr2;
38 
test_hashCode()39     public void test_hashCode() {
40         Object o1 = new Object();
41         Object o2 = new Object();
42         int h1 = System.identityHashCode(o1);
43         int h2 = System.identityHashCode(o2);
44         assertEquals(h1, o1.hashCode());
45         assertEquals(h2, o2.hashCode());
46         assertTrue(h1 != h2);
47     }
48 
test_clone()49     public void test_clone() {
50         MockCloneableObject mco = new MockCloneableObject();
51         try {
52             assertFalse(mco.equals(mco.clone()));
53             assertEquals(mco.getClass(), mco.clone().getClass());
54         } catch(CloneNotSupportedException cnse) {
55             fail("CloneNotSupportedException was thrown.");
56         }
57 
58         MockObject mo = new MockObject();
59         try {
60             mo.clone();
61             fail("CloneNotSupportedException was not thrown.");
62         } catch(CloneNotSupportedException cnse) {
63             //expected
64         }
65     }
66 
67     class MockCloneableObject extends Object implements Cloneable {
clone()68         public Object clone() throws CloneNotSupportedException {
69             return super.clone();
70         }
71     }
72 
73     class MockObject extends Object {
74 
75         boolean isCalled = false;
76 
finalize()77         public void finalize() throws Throwable {
78             super.finalize();
79             isCalled = true;
80         }
81 
clone()82         public Object clone() throws CloneNotSupportedException {
83             return super.clone();
84         }
85     }
86 
test_notify()87     public void test_notify() {
88         try {
89             Object obj = new Object();
90             obj.notify();
91             fail("IllegalMonitorStateException was not thrown.");
92         } catch(IllegalMonitorStateException imse) {
93             //expected
94         }
95     }
96 
test_notifyAll()97     public void test_notifyAll() {
98         try {
99             Object obj = new Object();
100             obj.notifyAll();
101             fail("IllegalMonitorStateException was not thrown.");
102         } catch(IllegalMonitorStateException imse) {
103             //expected
104         }
105     }
106 
test_wait()107     public void test_wait() {
108 
109         try {
110             Object obj = new Object();
111             obj.wait();
112             fail("IllegalMonitorStateException was not thrown.");
113         } catch(IllegalMonitorStateException imse) {
114             //expected
115         } catch(InterruptedException ex) {
116             fail("InterruptedException was thrown.");
117         }
118 
119        try {
120            thr1 = new TestThread1(TestThread1.CASE_WAIT);
121            thr2 = new TestThread2();
122            thr1.start();
123            thr2.start();
124            thr2.join();
125            thr1.join();
126            thr1 = null;
127            thr2 = null;
128         } catch(InterruptedException e) {
129             fail("InterruptedException was thrown.");
130         }
131         assertEquals(3, status);
132     }
133 
134     class TestThread1 extends Thread {
135 
136         static final int CASE_WAIT = 0;
137         static final int CASE_WAIT_LONG = 1;
138         static final int CASE_WAIT_LONG_INT = 2;
139 
140         int testCase = CASE_WAIT;
141 
TestThread1(int option)142         public TestThread1(int option) {
143             testCase = option;
144         }
145 
run()146         public void run() {
147             synchronized (obj1) {
148                 try {
149                     switch(testCase) {
150                         case CASE_WAIT:
151                             obj1.wait();// Wait for ever.
152                             break;
153                         case CASE_WAIT_LONG:
154                             obj1.wait(5000L);
155                             break;
156                         case CASE_WAIT_LONG_INT:
157                             obj1.wait(10000L, 999999);
158                             break;
159                     }
160 
161                 } catch (InterruptedException ex) {
162                     status = 3;
163                 }
164             }
165         }
166     }
167 
168     class TestThread2 extends Thread {
run()169         public void run() {
170             thr1.interrupt();
171         }
172     }
173 
test_waitJI()174     public void test_waitJI() {
175         try {
176             Object obj = new Object();
177             obj.wait(5000L, 1);
178             fail("IllegalMonitorStateException was not thrown.");
179         } catch(IllegalMonitorStateException imse) {
180             //expected
181         } catch(InterruptedException ex) {
182             fail("InterruptedException was thrown.");
183         }
184 
185        try {
186            thr1 = new TestThread1(TestThread1.CASE_WAIT_LONG_INT);
187            thr2 = new TestThread2();
188            thr1.start();
189            thr2.start();
190            thr2.join();
191            thr1.join();
192            thr1 = null;
193            thr2 = null;
194         } catch(InterruptedException e) {
195             fail("InterruptedException was thrown.");
196         }
197         assertEquals(3, status);
198     }
199 
test_waitJI_invalid()200     public void test_waitJI_invalid() throws Exception {
201         Object o = new Object();
202         synchronized (o) {
203             try {
204                 o.wait(-1, 0);
205                 fail();
206             } catch (IllegalArgumentException expected) {
207             }
208 
209             try {
210                 o.wait(0, -1);
211                 fail();
212             } catch (IllegalArgumentException expected) {
213             }
214 
215             try {
216                 o.wait(-1, -1);
217                 fail();
218             } catch (IllegalArgumentException expected) {
219             }
220 
221             // The ms timeout must fit in 32 bits.
222             try {
223                 o.wait(Integer.MAX_VALUE + 1, 0);
224                 fail();
225             } catch (IllegalArgumentException expected) {
226             }
227         }
228     }
229 
test_waitJ()230     public void test_waitJ() {
231         try {
232             Object obj = new Object();
233             obj.wait(5000L);
234             fail("IllegalMonitorStateException was not thrown.");
235         } catch(IllegalMonitorStateException imse) {
236             //expected
237         } catch(InterruptedException ex) {
238             fail("InterruptedException was thrown.");
239         }
240 
241        try {
242            thr1 = new TestThread1(TestThread1.CASE_WAIT_LONG);
243            thr2 = new TestThread2();
244            thr1.start();
245            thr2.start();
246            thr2.join();
247            thr1.join();
248            thr1 = null;
249            thr2 = null;
250         } catch(InterruptedException e) {
251             fail("InterruptedException was thrown.");
252         }
253         assertEquals(3, status);
254     }
255 }
256