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