Home | History | Annotate | Download | only in lang
      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 org.apache.harmony.tests.java.lang;
     18 
     19 public class ObjectTest extends junit.framework.TestCase {
     20 
     21     /**
     22      * Test objects.
     23      */
     24     Object obj1 = new Object();
     25 
     26     Object obj2 = new Object();
     27 
     28     /**
     29      * Generic state indicator.
     30      */
     31     int status = 0;
     32 
     33     int ready = 0;
     34 
     35     /**
     36      * java.lang.Object#Object()
     37      */
     38     public void test_Constructor() {
     39         // Test for method java.lang.Object()
     40         assertNotNull("Constructor failed !!!", new Object());
     41     }
     42 
     43     /**
     44      * java.lang.Object#equals(java.lang.Object)
     45      */
     46     public void test_equalsLjava_lang_Object() {
     47         // Test for method boolean java.lang.Object.equals(java.lang.Object)
     48         assertTrue("Same object should be equal", obj1.equals(obj1));
     49         assertTrue("Different objects should not be equal", !obj1.equals(obj2));
     50     }
     51 
     52     /**
     53      * java.lang.Object#getClass()
     54      */
     55     public void test_getClass() throws Exception {
     56         // Test for method java.lang.Class java.lang.Object.getClass()
     57         String classNames[] = { "java.lang.Object", "java.lang.Throwable",
     58                 "java.lang.StringBuffer" };
     59         Class<?> classToTest = null;
     60         Object instanceToTest = null;
     61 
     62         status = 0;
     63         for (int i = 0; i < classNames.length; ++i) {
     64             classToTest = Class.forName(classNames[i]);
     65             instanceToTest = classToTest.newInstance();
     66             assertTrue("Instance didn't match creator class.",
     67                     instanceToTest.getClass() == classToTest);
     68             assertTrue("Instance didn't match class with matching name.",
     69                     instanceToTest.getClass() == Class
     70                             .forName(classNames[i]));
     71         }
     72     }
     73 
     74     /**
     75      * java.lang.Object#hashCode()
     76      */
     77     public void test_hashCode() {
     78         // Test for method int java.lang.Object.hashCode()
     79         assertTrue("Same object should have same hash.",
     80                 obj1.hashCode() == obj1.hashCode());
     81         assertTrue("Same object should have same hash.",
     82                 obj2.hashCode() == obj2.hashCode());
     83     }
     84 
     85     /**
     86      * java.lang.Object#notify()
     87      */
     88     public void test_notify() {
     89         // Test for method void java.lang.Object.notify()
     90 
     91         // Inner class to run test thread.
     92         class TestThread implements Runnable {
     93             public void run() {
     94                 synchronized (obj1) {
     95                     try {
     96                         ready += 1;
     97                         obj1.wait();// Wait for ever.
     98                         status += 1;
     99                     } catch (InterruptedException ex) {
    100                         status = -1000;
    101                     }
    102                 }
    103             }
    104         }
    105         ;
    106 
    107         // Start of test code.
    108 
    109         // Warning:
    110         // This code relies on each thread getting serviced within
    111         // 200 mSec of when it is notified. Although this
    112         // seems reasonable, it could lead to false-failures.
    113 
    114         ready = 0;
    115         status = 0;
    116         final int readyWaitSecs = 3;
    117 
    118         final int threadCount = 20;
    119         for (int i = 0; i < threadCount; ++i) {
    120             new Thread(new TestThread()).start();
    121         }
    122         synchronized (obj1) {
    123             try {
    124 
    125                 // Wait up to readyWaitSeconds for all threads to be waiting on
    126                 // monitor
    127                 for (int i = 0; i < readyWaitSecs; i++) {
    128                     obj1.wait(1000, 0);
    129                     if (ready == threadCount) {
    130                         break;
    131                     }
    132                 }
    133 
    134                 // Check pre-conditions of testing notifyAll
    135                 assertTrue("Not all launched threads are waiting. (ready = "
    136                         + ready + ")", ready == threadCount);
    137                 assertTrue("Thread woke too early. (status = " + status + ")",
    138                         status == 0);
    139 
    140                 for (int i = 1; i <= threadCount; ++i) {
    141                     obj1.notify();
    142                     obj1.wait(200, 0);
    143                     assertTrue("Out of sync. (expected " + i + " but got "
    144                             + status + ")", status == i);
    145                 }
    146 
    147             } catch (InterruptedException ex) {
    148                 fail(
    149                         "Unexpectedly got an InterruptedException. (status = "
    150                                 + status + ")");
    151             }
    152         }
    153     }
    154 
    155     /**
    156      * java.lang.Object#notifyAll()
    157      */
    158     public void test_notifyAll() {
    159         // Test for method void java.lang.Object.notifyAll()
    160 
    161         // Inner class to run test thread.
    162         class TestThread implements Runnable {
    163             public void run() {
    164                 synchronized (obj1) {
    165                     try {
    166                         ready += 1;
    167                         obj1.wait();// Wait for ever.
    168                         status += 1;
    169                     } catch (InterruptedException ex) {
    170                         status = -1000;
    171                     }
    172                 }
    173             }
    174         }
    175         ;
    176 
    177         // Start of test code.
    178 
    179         // Warning:
    180         // This code relies on all threads getting serviced within
    181         // 5 seconds of when they are notified. Although this
    182         // seems reasonable, it could lead to false-failures.
    183 
    184         status = 0;
    185         ready = 0;
    186         final int readyWaitSecs = 3;
    187         final int threadCount = 20;
    188         for (int i = 0; i < threadCount; ++i) {
    189             new Thread(new TestThread()).start();
    190         }
    191 
    192         synchronized (obj1) {
    193 
    194             try {
    195 
    196                 // Wait up to readyWaitSeconds for all threads to be waiting on
    197                 // monitor
    198                 for (int i = 0; i < readyWaitSecs; i++) {
    199                     obj1.wait(1000, 0);
    200                     if (ready == threadCount) {
    201                         break;
    202                     }
    203                 }
    204 
    205                 // Check pre-conditions of testing notifyAll
    206                 assertTrue("Not all launched threads are waiting. (ready = "
    207                         + ready + ")", ready == threadCount);
    208                 assertTrue("At least one thread woke too early. (status = "
    209                         + status + ")", status == 0);
    210 
    211                 obj1.notifyAll();
    212 
    213                 obj1.wait(5000, 0);
    214 
    215                 assertTrue(
    216                         "At least one thread did not get notified. (status = "
    217                                 + status + ")", status == threadCount);
    218 
    219             } catch (InterruptedException ex) {
    220                 fail(
    221                         "Unexpectedly got an InterruptedException. (status = "
    222                                 + status + ")");
    223             }
    224 
    225         }
    226     }
    227 
    228     /**
    229      * java.lang.Object#toString()
    230      */
    231     public void test_toString() {
    232         // Test for method java.lang.String java.lang.Object.toString()
    233         assertNotNull("Object toString returned null.", obj1.toString());
    234     }
    235 
    236     /**
    237      * java.lang.Object#wait()
    238      */
    239     public void test_wait() {
    240         // Test for method void java.lang.Object.wait()
    241 
    242         // Inner class to run test thread.
    243         class TestThread implements Runnable {
    244             public void run() {
    245                 synchronized (obj1) {
    246                     try {
    247                         obj1.wait();// Wait for ever.
    248                         status = 1;
    249                     } catch (InterruptedException ex) {
    250                         status = -1;
    251                     }
    252                 }
    253             }
    254         }
    255         ;
    256 
    257         // Start of test code.
    258 
    259         // Warning:
    260         // This code relies on threads getting serviced within
    261         // 1 second of when they are notified. Although this
    262         // seems reasonable, it could lead to false-failures.
    263 
    264         status = 0;
    265         new Thread(new TestThread()).start();
    266         synchronized (obj1) {
    267             try {
    268                 obj1.wait(1000, 0);
    269                 assertTrue("Thread woke too early. (status = " + status + ")",
    270                         status == 0);
    271                 obj1.notifyAll();
    272                 obj1.wait(1000, 0);
    273                 assertTrue("Thread did not get notified. (status = " + status
    274                         + ")", status == 1);
    275             } catch (InterruptedException ex) {
    276                 fail(
    277                         "Unexpectedly got an InterruptedException. (status = "
    278                                 + status + ")");
    279             }
    280         }
    281     }
    282 
    283     /**
    284      * java.lang.Object#wait(long)
    285      */
    286     public void test_waitJ() {
    287         // Test for method void java.lang.Object.wait(long)
    288 
    289         // Start of test code.
    290 
    291         final int loopCount = 20;
    292         final int allowableError = 100; // millesconds
    293         final int delay = 200; // milliseconds
    294         synchronized (obj1) {
    295             try {
    296                 int count = 0;
    297                 long[][] toLong = new long[3][3];
    298                 for (int i = 0; i < loopCount; ++i) {
    299                     long before = System.currentTimeMillis();
    300                     obj1.wait(delay, 0);
    301                     long after = System.currentTimeMillis();
    302                     long error = (after - before - delay);
    303                     if (error < 0)
    304                         error = -error;
    305                     if (i > 0 && error > allowableError) {
    306                         // Allow jit to warm up before testing
    307                         if (count < toLong.length) {
    308                             toLong[count][0] = i;
    309                             toLong[count][1] = before;
    310                             toLong[count][2] = after;
    311                             count++;
    312                         }
    313                         if (error > (1000 + delay) || count == toLong.length) {
    314                             StringBuffer sb = new StringBuffer();
    315                             for (int j = 0; j < count; j++) {
    316                                 sb
    317                                         .append("wakeup time too inaccurate, iteration ");
    318                                 sb.append(toLong[j][0]);
    319                                 sb.append(", before: ");
    320                                 sb.append(toLong[j][1]);
    321                                 sb.append(" after: ");
    322                                 sb.append(toLong[j][2]);
    323                                 sb.append(" diff: ");
    324                                 sb.append(toLong[j][2] - toLong[j][1]);
    325                                 sb.append("\n");
    326                             }
    327                             fail(sb.toString());
    328                         }
    329                     }
    330                 }
    331             } catch (InterruptedException ex) {
    332                 fail(
    333                         "Unexpectedly got an InterruptedException. (status = "
    334                                 + status + ")");
    335             }
    336         }
    337     }
    338 
    339     /**
    340      * java.lang.Object#wait(long, int)
    341      */
    342     public void test_waitJI() {
    343         // Test for method void java.lang.Object.wait(long, int)
    344 
    345         // Inner class to run test thread.
    346         class TestThread implements Runnable {
    347             public void run() {
    348                 synchronized (obj1) {
    349                     try {
    350                         obj1.wait(0, 1); // Don't wait very long.
    351                         status = 1;
    352                         obj1.wait(0, 0); // Wait for ever.
    353                         status = 2;
    354                     } catch (InterruptedException ex) {
    355                         status = -1;
    356                     }
    357                 }
    358             }
    359         }
    360         ;
    361 
    362         // Start of test code.
    363 
    364         // Warning:
    365         // This code relies on threads getting serviced within
    366         // 1 second of when they are notified. Although this
    367         // seems reasonable, it could lead to false-failures.
    368 
    369         status = 0;
    370         new Thread(new TestThread()).start();
    371         synchronized (obj1) {
    372             try {
    373                 obj1.wait(1000, 0);
    374                 assertTrue("Thread did not wake after 1 ms. (status = "
    375                         + status + ")", status == 1);
    376                 obj1.notifyAll();
    377                 obj1.wait(1000, 0);
    378                 assertTrue("Thread did not get notified. (status = " + status
    379                         + ")", status == 2);
    380             } catch (InterruptedException ex) {
    381                 fail(
    382                         "Unexpectedly got an InterruptedException. (status = "
    383                                 + status + ")");
    384             }
    385         }
    386 
    387     }
    388 }
    389