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 
     18 package libcore.java.lang;
     19 
     20 import java.io.File;
     21 import java.io.IOException;
     22 import java.lang.reflect.Method;
     23 import java.nio.channels.Channel;
     24 import java.nio.channels.spi.SelectorProvider;
     25 import java.util.Map;
     26 import java.util.Properties;
     27 import java.util.Vector;
     28 
     29 public class OldSystemTest extends junit.framework.TestCase {
     30 
     31     public void test_arraycopyLjava_lang_ObjectILjava_lang_ObjectII() {
     32         // Test for method void java.lang.System.arraycopy(java.lang.Object,
     33         // int, java.lang.Object, int, int)
     34         Integer a[] = new Integer[20];
     35         Integer b[] = new Integer[20];
     36 
     37         try {
     38             // copy from non array object into Object array
     39             System.arraycopy(new Object(), 0, b, 0, 0);
     40             fail("ArrayStoreException is not thrown.");
     41         } catch(ArrayStoreException  ase) {
     42             //expected
     43         }
     44 
     45         try {
     46             // copy from Object array into non array object
     47             System.arraycopy(a, 0, new Object(), 0, 0);
     48             fail("ArrayStoreException is not thrown.");
     49         } catch(ArrayStoreException  ase) {
     50             //expected
     51         }
     52 
     53         try {
     54             // copy from primitive array into object array
     55             System.arraycopy(new char[] {'a'}, 0, new String[1], 0, 1);
     56             fail("ArrayStoreException is not thrown.");
     57         } catch(ArrayStoreException  ase) {
     58             //expected
     59         }
     60 
     61         try {
     62             // copy from object array into primitive array
     63             System.arraycopy(new String[] {"a"}, 0, new char[1], 0, 1);
     64             fail("ArrayStoreException is not thrown.");
     65         } catch(ArrayStoreException  ase) {
     66             //expected
     67         }
     68 
     69         try {
     70             // copy from primitive array into an array of another primitive type
     71             System.arraycopy(new char[] {'a'}, 0, new int[1], 0, 1);
     72             fail("ArrayStoreException is not thrown.");
     73         } catch(ArrayStoreException  ase) {
     74             //expected
     75         }
     76 
     77         try {
     78             // copy from object array into an array of another Object type
     79             System.arraycopy(new Character[] {'a'}, 0, new Integer[1], 0, 1);
     80             fail("ArrayStoreException is not thrown.");
     81         } catch(ArrayStoreException  ase) {
     82             //expected
     83         }
     84 
     85         try {
     86             // copy from null into an array of a primitive type
     87             System.arraycopy(null, 0, new int[1], 0, 1);
     88             fail("NullPointerException is not thrown.");
     89         } catch(NullPointerException npe) {
     90             //expected
     91         }
     92 
     93         try {
     94             // copy from a primitive array into null
     95             System.arraycopy(new int[]{'1'}, 0, null, 0, 1);
     96             fail("NullPointerException is not thrown.");
     97         } catch(NullPointerException npe) {
     98             //expected
     99         }
    100 
    101         try {
    102             System.arraycopy(a, a.length + 1, b, 0, 1);
    103             fail("IndexOutOfBoundsException is not thrown.");
    104         } catch(IndexOutOfBoundsException ioobe) {
    105             //expected
    106         }
    107 
    108         try {
    109             System.arraycopy(a, -1, b, 0, 1);
    110             fail("IndexOutOfBoundsException is not thrown.");
    111         } catch(IndexOutOfBoundsException ioobe) {
    112             //expected
    113         }
    114 
    115         try {
    116             System.arraycopy(a, 0, b, -1, 1);
    117             fail("IndexOutOfBoundsException is not thrown.");
    118         } catch(IndexOutOfBoundsException ioobe) {
    119             //expected
    120         }
    121 
    122         try {
    123             System.arraycopy(a, 0, b, 0, -1);
    124             fail("IndexOutOfBoundsException is not thrown.");
    125         } catch(IndexOutOfBoundsException ioobe) {
    126             //expected
    127         }
    128 
    129         try {
    130             System.arraycopy(a, 11, b, 0, 10);
    131             fail("IndexOutOfBoundsException is not thrown.");
    132         } catch(IndexOutOfBoundsException ioobe) {
    133             //expected
    134         }
    135 
    136         try {
    137             System.arraycopy(a, Integer.MAX_VALUE, b, 0, 10);
    138             fail("IndexOutOfBoundsException is not thrown.");
    139         } catch(IndexOutOfBoundsException ioobe) {
    140             //expected
    141         }
    142 
    143         try {
    144             System.arraycopy(a, 0, b, Integer.MAX_VALUE, 10);
    145             fail("IndexOutOfBoundsException is not thrown.");
    146         } catch(IndexOutOfBoundsException ioobe) {
    147             //expected
    148         }
    149 
    150         try {
    151             System.arraycopy(a, 0, b, 10, Integer.MAX_VALUE);
    152             fail("IndexOutOfBoundsException is not thrown.");
    153         } catch(IndexOutOfBoundsException ioobe) {
    154             //expected
    155         }
    156     }
    157 
    158     public void test_currentTimeMillis() {
    159         // Test for method long java.lang.System.currentTimeMillis()
    160         try {
    161             long firstRead = System.currentTimeMillis();
    162             try {
    163                 Thread.sleep(150);
    164             } catch (InterruptedException e) {
    165             }
    166             long secondRead = System.currentTimeMillis();
    167             assertTrue("Incorrect times returned: " + firstRead + ", "
    168                     + secondRead, firstRead < secondRead);
    169         } catch (Exception e) {
    170             fail("Exception during test: " + e.toString());
    171         }
    172     }
    173 
    174     public void test_getProperties() {
    175         String [] props = {"java.vendor.url",
    176                 "java.class.path", "user.home",
    177                 "java.class.version", "os.version",
    178                 "java.vendor", "user.dir",
    179                 /*"user.timezone",*/ "path.separator",
    180                 "os.name", "os.arch",
    181                 "line.separator", "file.separator",
    182                 "user.name", "java.version", "java.home" };
    183 
    184         Properties p = System.getProperties();
    185         assertTrue(p.size() > 0);
    186 
    187         // Ensure spec'ed properties are non-null. See System.getProperties()
    188         // spec.
    189 
    190         for (String prop : props) {
    191             assertNotNull("There is no property among returned properties: "
    192                     + prop, p.getProperty(prop));
    193             assertNotNull("System property is null: " + prop,
    194                     System.getProperty(prop));
    195         }
    196     }
    197 
    198     public void test_getPropertyLjava_lang_String() {
    199         try {
    200             System.getProperty(null);
    201             fail("NullPointerException should be thrown.");
    202         } catch(NullPointerException npe) {
    203             //expected
    204         }
    205 
    206         try {
    207             System.getProperty("");
    208             fail("IllegalArgumentException should be thrown.");
    209         } catch(IllegalArgumentException  iae) {
    210             //expected
    211         }
    212     }
    213 
    214     public void test_getPropertyLjava_lang_StringLjava_lang_String() {
    215         try {
    216             System.getProperty(null, "0.0");
    217             fail("NullPointerException should be thrown.");
    218         } catch(NullPointerException npe) {
    219             //expected
    220         }
    221 
    222         try {
    223             System.getProperty("", "0");
    224             fail("IllegalArgumentException should be thrown.");
    225         } catch(IllegalArgumentException  iae) {
    226             //expected
    227         }
    228     }
    229 
    230     public void test_inheritedChannel() throws IOException {
    231         Channel iChannel = System.inheritedChannel();
    232         assertNull("Incorrect value of channel", iChannel);
    233         SelectorProvider sp = SelectorProvider.provider();
    234         assertEquals("Incorrect value of channel",
    235                 sp.inheritedChannel(), iChannel);
    236     }
    237 
    238     public void test_clearProperty() {
    239         System.setProperty("test", "value");
    240         System.clearProperty("test");
    241         assertNull("Property was not deleted.", System.getProperty("test"));
    242 
    243         try {
    244             System.clearProperty(null);
    245             fail("NullPointerException is not thrown.");
    246         } catch(NullPointerException npe) {
    247             //expected
    248         }
    249 
    250         try {
    251             System.clearProperty("");
    252             fail("IllegalArgumentException is not thrown.");
    253         } catch(IllegalArgumentException iae) {
    254             //expected
    255         }
    256     }
    257 
    258     // Android-changed: test_gc() was deleted. PhantomReferenceTest provides basic
    259     // coverage for the fact that System.gc() executes a garbage collection if
    260     // followed by System.runFinalization().
    261 
    262     public void test_getenv() {
    263         // String[] props = { "PATH", "HOME", "USER"};
    264         // only PATH of these three exists on android
    265         String[] props = { "PATH" };
    266 
    267         Map<String,String> envMap = System.getenv();
    268         assertFalse("environment map is empty.", envMap.isEmpty());
    269         assertTrue("env map contains less than 3 keys.",
    270                 props.length < envMap.keySet().size());
    271         for (String prop : props) {
    272             assertNotNull("There is no property: " + prop,
    273                     envMap.get(prop));
    274         }
    275     }
    276 
    277     public void test_getenvLString() {
    278         assertNotNull("PATH environment variable is not found",
    279                   System.getenv("PATH"));
    280 
    281         assertNull("Doesn't return NULL for non existent property",
    282                   System.getenv("nonexistent.property"));
    283 
    284         try {
    285             System.getenv(null);
    286             fail("NullPointerException is not thrown.");
    287         } catch(NullPointerException npe) {
    288             //expected
    289         }
    290     }
    291 
    292     public void test_load() throws Exception {
    293         try {
    294             Runtime.getRuntime().load("nonExistentLibrary");
    295             fail("UnsatisfiedLinkError was not thrown.");
    296         } catch(UnsatisfiedLinkError expected) {
    297         }
    298 
    299         try {
    300             System.load("nonExistentLibrary");
    301             fail("UnsatisfiedLinkError was not thrown.");
    302         } catch(UnsatisfiedLinkError expected) {
    303         }
    304 
    305         try {
    306             System.load(null);
    307             fail("NullPointerException was not thrown.");
    308         } catch(NullPointerException expected) {
    309         }
    310 
    311         // Trivial positive test for System.load: Attempt to load a liblog.so - it's guaranteed
    312         // to exist and is whitelisted for use from applications. Also, it's in the library search
    313         // path for host builds.
    314         final ClassLoader cl = getClass().getClassLoader();
    315         // ClassLoader.findLibrary has protected access, so it's guaranteed to exist.
    316         final Method m = ClassLoader.class.getDeclaredMethod("findLibrary", String.class);
    317         assertNotNull(m);
    318         String libPath = (String) m.invoke(cl, "log");
    319         assertNotNull(libPath);
    320         System.load(new File(libPath).getAbsolutePath());
    321 
    322         // A negative test for a library that exists but isn't specified as an absolute path.
    323         // In other words, a name for which System.loadLibrary(libname) would suceed and
    324         // System.load(libname) would fail.
    325         String libName = new File(libPath).getName();
    326         try {
    327             System.load(libName);
    328             fail();
    329         } catch (UnsatisfiedLinkError expected) {
    330         }
    331     }
    332 
    333     public void test_loadLibrary() {
    334         try {
    335             System.loadLibrary("nonExistentLibrary");
    336             fail("UnsatisfiedLinkError was not thrown.");
    337         } catch(UnsatisfiedLinkError ule) {
    338             //expected
    339         }
    340 
    341         try {
    342             System.loadLibrary(null);
    343             fail("NullPointerException was not thrown.");
    344         } catch(NullPointerException npe) {
    345             //expected
    346         }
    347     }
    348 
    349     public void test_mapLibraryName() {
    350         assertEquals("libname.so", System.mapLibraryName("name"));
    351 
    352         try {
    353             System.mapLibraryName(null);
    354             fail("NullPointerException is not thrown.");
    355         } catch(NullPointerException npe) {
    356             //expected
    357         }
    358     }
    359 
    360     public void test_nanoTime() {
    361         long sleepTime = 5000;
    362         long beginTime = System.nanoTime();
    363         try {
    364             Thread.sleep(sleepTime);
    365         } catch(Exception e) {
    366             fail("Unknown exception was thrown.");
    367         }
    368         long endTime = System.nanoTime();
    369         assertTrue((endTime - beginTime) > sleepTime * 1000000);
    370     }
    371 }
    372