RuntimeTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
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 org.apache.harmony.luni.tests.java.lang;
19
20import dalvik.annotation.TestLevel;
21import dalvik.annotation.TestTargetNew;
22import dalvik.annotation.TestTargetClass;
23
24import java.io.ByteArrayInputStream;
25import java.io.ByteArrayOutputStream;
26import java.io.File;
27import java.io.IOException;
28import java.io.InputStream;
29import java.io.OutputStream;
30import java.io.UnsupportedEncodingException;
31import java.security.Permission;
32import java.util.Arrays;
33import java.util.Vector;
34
35import tests.support.resource.Support_Resources;
36
37@TestTargetClass(Runtime.class)
38public class RuntimeTest extends junit.framework.TestCase {
39
40    Runtime r = Runtime.getRuntime();
41
42    InputStream is;
43
44    String s;
45
46    static boolean flag = false;
47
48    static boolean ranFinalize = false;
49
50    int statusCode = -1;
51
52    class HasFinalizer {
53        String internalString;
54
55        HasFinalizer(String s) {
56            internalString = s;
57        }
58
59        @Override
60        protected void finalize() {
61            internalString = "hit";
62        }
63    }
64
65    @Override
66    protected void finalize() {
67        if (flag)
68            ranFinalize = true;
69    }
70
71    protected RuntimeTest createInstance() {
72        return new RuntimeTest("FT");
73    }
74
75    /**
76     * @tests java.lang.Runtime#exec(java.lang.String)
77     */
78    @TestTargetNew(
79        level = TestLevel.ADDITIONAL,
80        notes = "",
81        method = "exec",
82        args = {java.lang.String.class}
83    )
84    public void test_exec() {
85        /* successful exec's are tested by java.lang.Process */
86        try {
87            Runtime.getRuntime().exec("AnInexistentProgram");
88            fail("failed to throw IOException when exec'ed inexistent program");
89        } catch (IOException e) { /* expected */ }
90    }
91
92    /**
93     * @tests java.lang.Runtime#freeMemory()
94     */
95    @TestTargetNew(
96        level = TestLevel.COMPLETE,
97        notes = "",
98        method = "freeMemory",
99        args = {}
100    )
101    public void test_freeMemory() {
102        try {
103            long before = r.freeMemory();
104            Vector<StringBuffer> v = new Vector<StringBuffer>();
105            for (int i = 1; i < 10; i++)
106                v.addElement(new StringBuffer(10000));
107            long after =  r.freeMemory();
108            v = null;
109            r.gc();
110            assertTrue("freeMemory should return less value after " +
111                    "creating an object", after < before);
112            long afterGC =  r.freeMemory();
113            assertTrue("freeMemory should not return less value after " +
114                    "creating an object", afterGC > after);
115        } catch (Exception e) {
116            System.out.println("Out of memory during freeMemory test: "
117                    + e.getMessage());
118            r.gc();
119        } finally {
120            r.gc();
121        }
122    }
123
124    /**
125     * @tests java.lang.Runtime#gc()
126     */
127    @TestTargetNew(
128        level = TestLevel.COMPLETE,
129        notes = "",
130        method = "gc",
131        args = {}
132    )
133    public void test_gc() {
134        // Test for method void java.lang.Runtime.gc()
135        try {
136            r.gc(); // ensure all garbage objects have been collected
137            r.gc(); // two GCs force collection phase to complete
138            long firstRead = r.totalMemory() - r.freeMemory();
139            Vector<StringBuffer> v = new Vector<StringBuffer>();
140            for (int i = 1; i < 10; i++)
141                v.addElement(new StringBuffer(10000));
142            long secondRead = r.totalMemory() - r.freeMemory();
143            v = null;
144            r.gc();
145            r.gc();
146            assertTrue("object memory did not grow", secondRead > firstRead);
147            assertTrue("space was not reclaimed", (r.totalMemory() - r
148                    .freeMemory()) < secondRead);
149        } catch (Throwable t) {
150            System.out.println("Out of memory during gc test");
151            r.gc();
152            r.gc();
153        }
154    }
155
156    /**
157     * @tests java.lang.Runtime#getRuntime()
158     */
159    @TestTargetNew(
160        level = TestLevel.COMPLETE,
161        notes = "getRuntime method is verified in initial setup for other tests.",
162        method = "getRuntime",
163        args = {}
164    )
165    public void test_getRuntime() {
166        // Test for method java.lang.Runtime java.lang.Runtime.getRuntime()
167        assertNotNull(Runtime.getRuntime());
168    }
169
170    /**
171     * @tests java.lang.Runtime#runFinalization()
172     */
173    @TestTargetNew(
174        level = TestLevel.COMPLETE,
175        notes = "",
176        method = "runFinalization",
177        args = {}
178    )
179    public void test_runFinalization() {
180        // Test for method void java.lang.Runtime.runFinalization()
181
182        flag = true;
183        createInstance();
184        int count = 10;
185        // the gc below likely bogosifies the test, but will have to do for
186        // the moment
187        while (!ranFinalize && count-- > 0) {
188            r.gc();
189            r.runFinalization();
190        }
191        assertTrue("Failed to run finalization", ranFinalize);
192    }
193
194    /**
195     * @tests java.lang.Runtime#totalMemory()
196     */
197    @TestTargetNew(
198        level = TestLevel.COMPLETE,
199        notes = "",
200        method = "totalMemory",
201        args = {}
202    )
203    public void test_totalMemory() {
204        // Test for method long java.lang.Runtime.totalMemory()
205        assertTrue("totalMemory returned nonsense value", r.totalMemory() >= r
206                .freeMemory());
207    }
208
209    @TestTargetNew(
210        level = TestLevel.COMPLETE,
211        notes = "",
212        method = "addShutdownHook",
213        args = {java.lang.Thread.class}
214    )
215    public void test_addShutdownHook() {
216        Thread thrException = new Thread () {
217            public void run() {
218                try {
219                    Runtime.getRuntime().addShutdownHook(this);
220                    fail("IllegalStateException was not thrown.");
221                } catch(IllegalStateException ise) {
222                    //expected
223                }
224            }
225        };
226
227        try {
228            Runtime.getRuntime().addShutdownHook(thrException);
229        } catch (Throwable t) {
230            fail(t.getMessage());
231        }
232
233        try {
234            Runtime.getRuntime().addShutdownHook(thrException);
235            fail("IllegalArgumentException was not thrown.");
236        } catch(IllegalArgumentException  iae) {
237            // expected
238        }
239
240        SecurityManager sm = new SecurityManager() {
241
242            public void checkPermission(Permission perm) {
243                if (perm.getName().equals("shutdownHooks")) {
244                    throw new SecurityException();
245                }
246            }
247        };
248
249        // remove previously added hook so we're not depending on the priority
250        // of the Exceptions to be thrown.
251        Runtime.getRuntime().removeShutdownHook(thrException);
252
253        SecurityManager oldSm = System.getSecurityManager();
254        System.setSecurityManager(sm);
255        try {
256            Runtime.getRuntime().addShutdownHook(thrException);
257            fail("SecurityException should be thrown.");
258        } catch (SecurityException e) {
259            // expected
260        } finally {
261            System.setSecurityManager(oldSm);
262        }
263
264        try {
265            Thread.currentThread().sleep(1000);
266        } catch (InterruptedException ie) {
267        }
268    }
269
270    @TestTargetNew(
271        level = TestLevel.COMPLETE,
272        notes = "",
273        method = "availableProcessors",
274        args = {}
275    )
276    public void test_availableProcessors() {
277        assertTrue(Runtime.getRuntime().availableProcessors() > 0);
278    }
279
280    @TestTargetNew(
281        level = TestLevel.COMPLETE,
282        notes = "",
283        method = "exec",
284        args = {java.lang.String.class, java.lang.String[].class}
285    )
286    public void test_execLjava_lang_StringLjava_lang_StringArray() {
287
288        String [] envp =  getEnv();
289
290        checkExec(0, envp, null);
291        checkExec(0, null, null);
292
293        try {
294            Runtime.getRuntime().exec((String)null, null);
295            fail("NullPointerException should be thrown.");
296        } catch(IOException ioe) {
297            fail("IOException was thrown.");
298        } catch(NullPointerException npe) {
299            //expected
300        }
301
302        SecurityManager sm = new SecurityManager() {
303
304            public void checkPermission(Permission perm) {
305                if (perm.getName().equals("checkExec")) {
306                    throw new SecurityException();
307                }
308            }
309
310            public void checkExec(String cmd) {
311                throw new SecurityException();
312            }
313        };
314
315        SecurityManager oldSm = System.getSecurityManager();
316        System.setSecurityManager(sm);
317
318        try {
319            Runtime.getRuntime().exec("ls", envp);
320            fail("SecurityException should be thrown.");
321        } catch (SecurityException e) {
322            // expected
323        } catch (IOException e) {
324            fail("IOException was thrown.");
325        } finally {
326            System.setSecurityManager(oldSm);
327        }
328
329        try {
330            Runtime.getRuntime().exec("", envp);
331            fail("IllegalArgumentException should be thrown.");
332        } catch(IllegalArgumentException iae) {
333            //expected
334        } catch (IOException e) {
335            fail("IOException was thrown.");
336        }
337    }
338
339    @TestTargetNew(
340        level = TestLevel.COMPLETE,
341        notes = "",
342        method = "exec",
343        args = {java.lang.String[].class, java.lang.String[].class}
344    )
345    public void test_execLjava_lang_StringArrayLjava_lang_StringArray() {
346        String [] envp =  getEnv();
347
348        checkExec(4, envp, null);
349        checkExec(4, null, null);
350
351        try {
352            Runtime.getRuntime().exec((String[])null, null);
353            fail("NullPointerException should be thrown.");
354        } catch(IOException ioe) {
355            fail("IOException was thrown.");
356        } catch(NullPointerException npe) {
357            //expected
358        }
359
360        try {
361            Runtime.getRuntime().exec(new String[]{"ls", null}, null);
362            fail("NullPointerException should be thrown.");
363        } catch(IOException ioe) {
364            fail("IOException was thrown.");
365        } catch(NullPointerException npe) {
366            //expected
367        }
368
369        SecurityManager sm = new SecurityManager() {
370
371            public void checkPermission(Permission perm) {
372                if (perm.getName().equals("checkExec")) {
373                    throw new SecurityException();
374                }
375            }
376
377            public void checkExec(String cmd) {
378                throw new SecurityException();
379            }
380        };
381
382        SecurityManager oldSm = System.getSecurityManager();
383        System.setSecurityManager(sm);
384
385        try {
386            Runtime.getRuntime().exec(new String[]{"ls"}, envp);
387            fail("SecurityException should be thrown.");
388        } catch (SecurityException e) {
389            // expected
390        } catch (IOException e) {
391            fail("IOException was thrown.");
392        } finally {
393            System.setSecurityManager(oldSm);
394        }
395
396        try {
397            Runtime.getRuntime().exec(new String[]{}, envp);
398            fail("IndexOutOfBoundsException should be thrown.");
399        } catch(IndexOutOfBoundsException ioob) {
400            //expected
401        } catch (IOException e) {
402            fail("IOException was thrown.");
403        }
404
405        try {
406            Runtime.getRuntime().exec(new String[]{""}, envp);
407            fail("IOException should be thrown.");
408        } catch (IOException e) { /* expected */ }
409    }
410
411    @TestTargetNew(
412        level = TestLevel.COMPLETE,
413        notes = "",
414        method = "exec",
415        args = {java.lang.String.class, java.lang.String[].class, java.io.File.class}
416    )
417    public void test_execLjava_lang_StringLjava_lang_StringArrayLjava_io_File() {
418
419        String [] envp =  getEnv();
420
421        File workFolder = Support_Resources.createTempFolder();
422
423        checkExec(2, envp, workFolder);
424        checkExec(2, null, null);
425
426        try {
427            Runtime.getRuntime().exec((String)null, null, workFolder);
428            fail("NullPointerException should be thrown.");
429        } catch(IOException ioe) {
430            fail("IOException was thrown.");
431        } catch(NullPointerException npe) {
432            //expected
433        }
434
435        SecurityManager sm = new SecurityManager() {
436
437            public void checkPermission(Permission perm) {
438                if (perm.getName().equals("checkExec")) {
439                    throw new SecurityException();
440                }
441            }
442
443            public void checkExec(String cmd) {
444                throw new SecurityException();
445            }
446        };
447
448        SecurityManager oldSm = System.getSecurityManager();
449        System.setSecurityManager(sm);
450
451        try {
452            Runtime.getRuntime().exec("ls",  envp, workFolder);
453            fail("SecurityException should be thrown.");
454        } catch (SecurityException e) {
455            // expected
456        } catch (IOException e) {
457            fail("IOException was thrown.");
458        } finally {
459            System.setSecurityManager(oldSm);
460        }
461
462        try {
463            Runtime.getRuntime().exec("",  envp, workFolder);
464            fail("SecurityException should be thrown.");
465        } catch(IllegalArgumentException iae) {
466            //expected
467        } catch (IOException e) {
468            fail("IOException was thrown.");
469        }
470    }
471
472    @TestTargetNew(
473        level = TestLevel.COMPLETE,
474        notes = "",
475        method = "exec",
476        args = {java.lang.String[].class, java.lang.String[].class, java.io.File.class}
477    )
478    public void test_execLjava_lang_StringArrayLjava_lang_StringArrayLjava_io_File() {
479        String [] envp =  getEnv();
480
481        File workFolder = Support_Resources.createTempFolder();
482
483        checkExec(5, envp, workFolder);
484        checkExec(5, null, null);
485
486        try {
487            Runtime.getRuntime().exec((String[])null, null, workFolder);
488            fail("NullPointerException should be thrown.");
489        } catch(IOException ioe) {
490            fail("IOException was thrown.");
491        } catch(NullPointerException npe) {
492            //expected
493        }
494
495        try {
496            Runtime.getRuntime().exec(new String[]{"ls", null}, null, workFolder);
497            fail("NullPointerException should be thrown.");
498        } catch(IOException ioe) {
499            fail("IOException was thrown.");
500        } catch(NullPointerException npe) {
501            //expected
502        }
503
504        SecurityManager sm = new SecurityManager() {
505
506            public void checkPermission(Permission perm) {
507                if (perm.getName().equals("checkExec")) {
508                    throw new SecurityException();
509                }
510            }
511
512            public void checkExec(String cmd) {
513                throw new SecurityException();
514            }
515        };
516
517        SecurityManager oldSm = System.getSecurityManager();
518        System.setSecurityManager(sm);
519
520        try {
521            Runtime.getRuntime().exec(new String[] {"ls"},  envp, workFolder);
522            fail("SecurityException should be thrown.");
523        } catch (SecurityException e) {
524            // expected
525        } catch (IOException e) {
526            fail("IOException was thrown.");
527        } finally {
528            System.setSecurityManager(oldSm);
529        }
530
531        try {
532            Runtime.getRuntime().exec(new String[]{""}, envp, workFolder);
533            fail("IOException should be thrown.");
534        } catch (IOException e) {
535            //expected
536        }
537    }
538
539    String [] getEnv() {
540        Object [] valueSet = System.getenv().values().toArray();
541        Object [] keySet = System.getenv().keySet().toArray();
542        String [] envp = new String[valueSet.length];
543        for(int i = 0; i < envp.length; i++) {
544            envp[i] = keySet[i] + "=" + valueSet[i];
545        }
546        return envp;
547    }
548
549    void checkExec(int testCase, String [] envp, File file) {
550        String dirName = "Test_Directory";
551        String dirParentName = "Parent_Directory";
552        File resources = Support_Resources.createTempFolder();
553        String folder = resources.getAbsolutePath() + "/" + dirName;
554        String folderWithParent = resources.getAbsolutePath() + "/"  +
555                                    dirParentName + "/" + dirName;
556        String command = "mkdir " + folder;
557        String [] commandArguments = {"mkdir", folder};
558        try {
559            Process proc = null;
560            switch(testCase) {
561                case 0:
562                    proc = Runtime.getRuntime().exec(command, envp);
563                    break;
564                case 1:
565                    proc = Runtime.getRuntime().exec(command);
566                    break;
567                case 2:
568                    proc = Runtime.getRuntime().exec(command, envp, file);
569                    break;
570                case 3:
571                    proc = Runtime.getRuntime().exec(commandArguments);
572                    break;
573                case 4:
574                    proc = Runtime.getRuntime().exec(commandArguments, envp);
575                    break;
576                case 5:
577                    proc = Runtime.getRuntime().exec(commandArguments, envp, file);
578                    break;
579            }
580            assertNotNull(proc);
581            try {
582                Thread.sleep(3000);
583            } catch(InterruptedException ie) {
584                fail("InterruptedException was thrown.");
585            }
586            File f = new File(folder);
587            assertTrue(f.exists());
588            if(f.exists()) {
589                f.delete();
590            }
591        } catch(IOException io) {
592            fail("IOException was thrown.");
593        }
594    }
595
596    @TestTargetNew(
597        level = TestLevel.COMPLETE,
598        notes = "",
599        method = "exec",
600        args = {java.lang.String.class}
601    )
602    public void test_execLjava_lang_String() {
603        checkExec(1, null, null);
604
605        try {
606            Runtime.getRuntime().exec((String) null);
607            fail("NullPointerException was not thrown.");
608        } catch(NullPointerException npe) {
609            //expected
610        } catch (IOException e) {
611            fail("IOException was thrown.");
612        }
613
614        try {
615            Runtime.getRuntime().exec("");
616            fail("IllegalArgumentException was not thrown.");
617        } catch(IllegalArgumentException iae) {
618            //expected
619        } catch (IOException e) {
620            fail("IOException was thrown.");
621        }
622
623        SecurityManager sm = new SecurityManager() {
624
625            public void checkPermission(Permission perm) {
626            }
627
628            public void checkExec(String cmd) {
629                throw new SecurityException();
630            }
631        };
632
633        SecurityManager oldSm = System.getSecurityManager();
634        System.setSecurityManager(sm);
635        try {
636            Runtime.getRuntime().exec("ls");
637            fail("SecurityException should be thrown.");
638        } catch (SecurityException e) {
639            // expected
640        } catch (IOException ioe) {
641            fail("IOException was thrown.");
642        } finally {
643            System.setSecurityManager(oldSm);
644        }
645    }
646
647    @TestTargetNew(
648        level = TestLevel.COMPLETE,
649        notes = "",
650        method = "exec",
651        args = {java.lang.String[].class}
652    )
653    public void test_execLjava_lang_StringArray() {
654
655        checkExec(3, null, null);
656
657        try {
658            Runtime.getRuntime().exec((String[]) null);
659            fail("NullPointerException was not thrown.");
660        } catch(NullPointerException npe) {
661            //expected
662        } catch (IOException e) {
663            fail("IOException was thrown.");
664        }
665
666        try {
667            Runtime.getRuntime().exec(new String[]{"ls", null});
668            fail("NullPointerException was not thrown.");
669        } catch(NullPointerException npe) {
670            //expected
671        } catch (IOException e) {
672            fail("IOException was thrown.");
673        }
674
675        try {
676            Runtime.getRuntime().exec(new String[]{});
677            fail("IndexOutOfBoundsException was not thrown.");
678        } catch(IndexOutOfBoundsException iobe) {
679            //expected
680        } catch (IOException e) {
681            fail("IOException was thrown.");
682        }
683
684        SecurityManager sm = new SecurityManager() {
685
686            public void checkPermission(Permission perm) {
687            }
688
689            public void checkExec(String cmd) {
690                throw new SecurityException();
691            }
692        };
693
694        SecurityManager oldSm = System.getSecurityManager();
695        System.setSecurityManager(sm);
696        try {
697            Runtime.getRuntime().exec(new String[]{"ls"});
698            fail("SecurityException should be thrown.");
699        } catch (SecurityException e) {
700            // expected
701        } catch (IOException ioe) {
702            fail("IOException was thrown.");
703        } finally {
704            System.setSecurityManager(oldSm);
705        }
706
707        try {
708            Runtime.getRuntime().exec(new String[]{""});
709            fail("IOException should be thrown.");
710        } catch (IOException e) {
711            //expected
712        }
713    }
714
715    @TestTargetNew(
716        level = TestLevel.COMPLETE,
717        notes = "",
718        method = "runFinalizersOnExit",
719        args = {boolean.class}
720    )
721    public void test_runFinalizersOnExit() {
722        Runtime.getRuntime().runFinalizersOnExit(true);
723
724        SecurityManager sm = new SecurityManager() {
725
726            public void checkPermission(Permission perm) {
727            }
728
729            public void checkExit(int status) {
730                throw new SecurityException();
731            }
732        };
733
734        SecurityManager oldSm = System.getSecurityManager();
735        System.setSecurityManager(sm);
736        try {
737            Runtime.getRuntime().runFinalizersOnExit(true);
738            fail("SecurityException should be thrown.");
739        } catch (SecurityException e) {
740            // expected
741        } finally {
742            System.setSecurityManager(oldSm);
743        }
744    }
745
746    @TestTargetNew(
747        level = TestLevel.COMPLETE,
748        notes = "",
749        method = "removeShutdownHook",
750        args = {java.lang.Thread.class}
751    )
752    public void test_removeShutdownHookLjava_lang_Thread() {
753        Thread thr1 = new Thread () {
754            public void run() {
755                try {
756                    Runtime.getRuntime().addShutdownHook(this);
757                } catch(IllegalStateException ise) {
758                    fail("IllegalStateException shouldn't be thrown.");
759                }
760            }
761        };
762
763        try {
764            Runtime.getRuntime().addShutdownHook(thr1);
765            Runtime.getRuntime().removeShutdownHook(thr1);
766        } catch (Throwable t) {
767            fail(t.getMessage());
768        }
769
770        Thread thr2 = new Thread () {
771            public void run() {
772                try {
773                    Runtime.getRuntime().removeShutdownHook(this);
774                    fail("IllegalStateException wasn't thrown.");
775                } catch(IllegalStateException ise) {
776                    //expected
777                }
778            }
779        };
780
781        try {
782            Runtime.getRuntime().addShutdownHook(thr2);
783        } catch (Throwable t) {
784            fail(t.getMessage());
785        }
786
787        SecurityManager sm = new SecurityManager() {
788
789            public void checkPermission(Permission perm) {
790                if (perm.getName().equals("shutdownHooks")) {
791                    throw new SecurityException();
792                }
793            }
794        };
795
796        SecurityManager oldSm = System.getSecurityManager();
797        System.setSecurityManager(sm);
798        try {
799            Runtime.getRuntime().addShutdownHook(thr1);
800            fail("SecurityException should be thrown.");
801        } catch (SecurityException e) {
802            // expected
803        } finally {
804            System.setSecurityManager(oldSm);
805        }
806
807        try {
808            Thread.currentThread().sleep(1000);
809        } catch (InterruptedException ie) {
810        }
811    }
812
813    @TestTargetNew(
814        level = TestLevel.COMPLETE,
815        notes = "",
816        method = "maxMemory",
817        args = {}
818    )
819    public void test_maxMemory() {
820        assertTrue(Runtime.getRuntime().maxMemory() < Long.MAX_VALUE);
821    }
822
823    @TestTargetNew(
824        level = TestLevel.COMPLETE,
825        notes = "",
826        method = "traceInstructions",
827        args = {boolean.class}
828    )
829    public void test_traceInstructions() {
830        Runtime.getRuntime().traceInstructions(false);
831        Runtime.getRuntime().traceInstructions(true);
832    }
833
834    @TestTargetNew(
835        level = TestLevel.COMPLETE,
836        notes = "",
837        method = "traceMethodCalls",
838        args = {boolean.class}
839    )
840    public void test_traceMethodCalls() {
841        Runtime.getRuntime().traceMethodCalls(false);
842        Runtime.getRuntime().traceMethodCalls(true);
843        Runtime.getRuntime().traceMethodCalls(false);
844        // try to clean up
845        //File tracefile = new File("/sdcard/dmtrace.trace");
846        //if(tracefile.exists()) {
847        //    tracefile.delete();
848        //}
849    }
850
851    @SuppressWarnings("deprecation")
852    @TestTargetNew(
853        level = TestLevel.COMPLETE,
854        notes = "",
855        method = "getLocalizedInputStream",
856        args = {java.io.InputStream.class}
857    )
858    public void test_getLocalizedInputStream() {
859        String simpleString = "Heart \u2f3c";
860        byte[] expected = {72, 0, 101, 0, 97, 0, 114, 0, 116, 0, 32, 0, 60, 47};
861        byte[] returned = new byte[expected.length];
862
863        String oldEncoding = System.getProperty("file.encoding");
864        System.setProperty("file.encoding", "UTF-16LE");
865
866        try {
867            ByteArrayInputStream bais = new ByteArrayInputStream(
868                    simpleString.getBytes("UTF-8"));
869
870            InputStream lcIn =
871                    Runtime.getRuntime().getLocalizedInputStream(bais);
872            try {
873                lcIn.read(returned);
874            } catch(IOException ioe) {
875                fail("IOException was thrown.");
876            }
877
878            assertTrue("wrong result for String: " + simpleString,
879                    Arrays.equals(expected, returned));
880        } catch (UnsupportedEncodingException e) {
881            fail("UnsupportedEncodingException was thrown.");
882        } finally {
883            System.setProperty("file.encoding", oldEncoding);
884        }
885    }
886
887    @SuppressWarnings("deprecation")
888    @TestTargetNew(
889        level = TestLevel.SUFFICIENT,
890        notes = "",
891        method = "getLocalizedOutputStream",
892        args = {java.io.OutputStream.class}
893    )
894    public void test_getLocalizedOutputStream() {
895        String simpleString = "Heart \u2f3c";
896        byte[] expected = {72, 0, 101, 0, 97, 0, 114, 0, 116, 0, 32, 0, 60, 47};
897        byte[] returned;
898
899        String oldEncoding = System.getProperty("file.encoding");
900        System.setProperty("file.encoding", "UTF-16LE");
901
902        try {
903            ByteArrayOutputStream out = new ByteArrayOutputStream();
904
905            OutputStream lcOut =
906                    Runtime.getRuntime().getLocalizedOutputStream(out);
907            try {
908                lcOut.write(simpleString.getBytes("UTF-8"));
909                lcOut.flush();
910                lcOut.close();
911            } catch(IOException ioe) {
912                fail("IOException was thrown.");
913            }
914
915            returned = out.toByteArray();
916
917            assertTrue("wrong result for String: " + returned.toString() +
918                    " expected string: " + expected.toString(),
919                    Arrays.equals(expected, returned));
920        } finally {
921            System.setProperty("file.encoding", oldEncoding);
922        }
923    }
924
925
926    @TestTargetNew(
927        level = TestLevel.COMPLETE,
928        notes = "",
929        method = "load",
930        args = {java.lang.String.class}
931    )
932    public void test_load() {
933
934        try {
935            Runtime.getRuntime().load("nonExistentLibrary");
936            fail("UnsatisfiedLinkError was not thrown.");
937        } catch(UnsatisfiedLinkError ule) {
938            //expected
939        }
940
941        try {
942            Runtime.getRuntime().load(null);
943            fail("NullPointerException was not thrown.");
944        } catch(NullPointerException npe) {
945            //expected
946        }
947
948        SecurityManager sm = new SecurityManager() {
949
950            public void checkPermission(Permission perm) {
951
952            }
953
954            public void checkLink(String lib) {
955                if (lib.endsWith("libjvm.so")) {
956                    throw new SecurityException();
957                }
958            }
959        };
960
961        SecurityManager oldSm = System.getSecurityManager();
962        System.setSecurityManager(sm);
963        try {
964            Runtime.getRuntime().load("libjvm.so");
965            fail("SecurityException should be thrown.");
966        } catch (SecurityException e) {
967            // expected
968        } finally {
969            System.setSecurityManager(oldSm);
970        }
971    }
972
973    @TestTargetNew(
974        level = TestLevel.COMPLETE,
975        notes = "",
976        method = "loadLibrary",
977        args = {java.lang.String.class}
978    )
979    public void test_loadLibrary() {
980        try {
981            Runtime.getRuntime().loadLibrary("nonExistentLibrary");
982            fail("UnsatisfiedLinkError was not thrown.");
983        } catch(UnsatisfiedLinkError ule) {
984            //expected
985        }
986
987        try {
988            Runtime.getRuntime().loadLibrary(null);
989            fail("NullPointerException was not thrown.");
990        } catch(NullPointerException npe) {
991            //expected
992        }
993
994        SecurityManager sm = new SecurityManager() {
995
996            public void checkPermission(Permission perm) {
997
998            }
999
1000            public void checkLink(String lib) {
1001                if (lib.endsWith("libjvm.so")) {
1002                    throw new SecurityException();
1003                }
1004            }
1005        };
1006
1007        SecurityManager oldSm = System.getSecurityManager();
1008        System.setSecurityManager(sm);
1009        try {
1010            Runtime.getRuntime().loadLibrary("libjvm.so");
1011            fail("SecurityException should be thrown.");
1012        } catch (SecurityException e) {
1013            // expected
1014        } finally {
1015            System.setSecurityManager(oldSm);
1016        }
1017    }
1018
1019    @TestTargetNew(
1020        level = TestLevel.SUFFICIENT,
1021        notes = "This method never returns normally, " +
1022                "and can't be tested. Only SecurityException can be checked.",
1023        method = "exit",
1024        args = {int.class}
1025    )
1026    public void test_exit() {
1027        statusCode = -1;
1028        SecurityManager sm = new SecurityManager() {
1029
1030            public void checkPermission(Permission perm) {
1031
1032            }
1033
1034            public void checkExit(int status) {
1035                statusCode = status;
1036                throw new SecurityException();
1037            }
1038        };
1039
1040        SecurityManager oldSm = System.getSecurityManager();
1041        System.setSecurityManager(sm);
1042        try {
1043            r.exit(0);
1044            fail("SecurityException should be thrown.");
1045        } catch (SecurityException e) {
1046            // expected
1047        } finally {
1048            assertTrue("Incorrect status code was received: " + statusCode,
1049                    statusCode == 0);
1050            System.setSecurityManager(oldSm);
1051        }
1052
1053    }
1054
1055    @TestTargetNew(
1056        level = TestLevel.SUFFICIENT,
1057        notes = "Can't be tested. This method terminates the currently " +
1058                "running VM. Only SecurityException can be checked.",
1059        method = "halt",
1060        args = {int.class}
1061    )
1062    public void test_halt() {
1063        statusCode = -1;
1064        SecurityManager sm = new SecurityManager() {
1065
1066            public void checkPermission(Permission perm) {
1067
1068            }
1069
1070            public void checkExit(int status) {
1071                statusCode = status;
1072                throw new SecurityException();
1073            }
1074        };
1075
1076        SecurityManager oldSm = System.getSecurityManager();
1077        System.setSecurityManager(sm);
1078        try {
1079            r.halt(0);
1080            fail("SecurityException should be thrown.");
1081        } catch (SecurityException e) {
1082            // expected
1083        } finally {
1084            assertTrue("Incorrect status code was received: " + statusCode,
1085                    statusCode == 0);
1086            System.setSecurityManager(oldSm);
1087        }
1088    }
1089
1090    public RuntimeTest() {
1091    }
1092
1093    public RuntimeTest(String name) {
1094        super(name);
1095    }
1096}
1097