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.File;
21import java.io.IOException;
22import java.lang.reflect.Method;
23import java.nio.channels.Channel;
24import java.nio.channels.spi.SelectorProvider;
25import java.util.Map;
26import java.util.Properties;
27import java.util.Vector;
28
29public 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    public void test_gc() {
259        Runtime rt =  Runtime.getRuntime();
260        Vector<StringBuffer> vec = new Vector<StringBuffer>();
261        long beforeTest = rt.freeMemory();
262        while(rt.freeMemory() < beforeTest * 2/3) {
263             vec.add(new StringBuffer(1000));
264        }
265        long beforeGC = rt.totalMemory() - rt.freeMemory();
266        vec = null;
267        System.gc();
268        System.runFinalization();
269        long afterGC = rt.totalMemory() - rt.freeMemory();
270        assertTrue("memory was not released after calling System.gc()." +
271                "before gc: " + beforeGC + "; after gc: " + afterGC,
272                beforeGC > afterGC);
273    }
274
275    public void test_getenv() {
276        // String[] props = { "PATH", "HOME", "USER"};
277        // only PATH of these three exists on android
278        String[] props = { "PATH" };
279
280        Map<String,String> envMap = System.getenv();
281        assertFalse("environment map is empty.", envMap.isEmpty());
282        assertTrue("env map contains less than 3 keys.",
283                props.length < envMap.keySet().size());
284        for (String prop : props) {
285            assertNotNull("There is no property: " + prop,
286                    envMap.get(prop));
287        }
288    }
289
290    public void test_getenvLString() {
291        assertNotNull("PATH environment variable is not found",
292                  System.getenv("PATH"));
293
294        assertNull("Doesn't return NULL for non existent property",
295                  System.getenv("nonexistent.property"));
296
297        try {
298            System.getenv(null);
299            fail("NullPointerException is not thrown.");
300        } catch(NullPointerException npe) {
301            //expected
302        }
303    }
304
305    public void test_load() throws Exception {
306        try {
307            Runtime.getRuntime().load("nonExistentLibrary");
308            fail("UnsatisfiedLinkError was not thrown.");
309        } catch(UnsatisfiedLinkError expected) {
310        }
311
312        try {
313            System.load("nonExistentLibrary");
314            fail("UnsatisfiedLinkError was not thrown.");
315        } catch(UnsatisfiedLinkError expected) {
316        }
317
318        try {
319            System.load(null);
320            fail("NullPointerException was not thrown.");
321        } catch(NullPointerException expected) {
322        }
323
324        // Trivial positive test for System.load: Attempt to load a libc.so - it's guaranteed
325        // to exist and is whitelisted for use from applications.
326        final ClassLoader cl = getClass().getClassLoader();
327        // ClassLoader.findLibrary has protected access, so it's guaranteed to exist.
328        final Method m = ClassLoader.class.getDeclaredMethod("findLibrary", String.class);
329        assertNotNull(m);
330        String libPath = (String) m.invoke(cl, "c");
331        assertNotNull(libPath);
332        System.load(new File(libPath).getAbsolutePath());
333    }
334
335    public void test_loadLibrary() {
336        try {
337            System.loadLibrary("nonExistentLibrary");
338            fail("UnsatisfiedLinkError was not thrown.");
339        } catch(UnsatisfiedLinkError ule) {
340            //expected
341        }
342
343        try {
344            System.loadLibrary(null);
345            fail("NullPointerException was not thrown.");
346        } catch(NullPointerException npe) {
347            //expected
348        }
349    }
350
351    public void test_mapLibraryName() {
352        assertEquals("libname.so", System.mapLibraryName("name"));
353
354        try {
355            System.mapLibraryName(null);
356            fail("NullPointerException is not thrown.");
357        } catch(NullPointerException npe) {
358            //expected
359        }
360    }
361
362    public void test_nanoTime() {
363        long sleepTime = 5000;
364        long beginTime = System.nanoTime();
365        try {
366            Thread.sleep(sleepTime);
367        } catch(Exception e) {
368            fail("Unknown exception was thrown.");
369        }
370        long endTime = System.nanoTime();
371        assertTrue((endTime - beginTime) > sleepTime * 1000000);
372    }
373}
374