ProcessManagerTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package tests.api.java.lang;
18
19import dalvik.annotation.TestInfo;
20import dalvik.annotation.TestLevel;
21import dalvik.annotation.TestTarget;
22import dalvik.annotation.TestTargetClass;
23
24import junit.framework.TestCase;
25
26import java.io.BufferedReader;
27import java.io.File;
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.InputStreamReader;
31import java.io.OutputStream;
32
33@TestTargetClass(Process.class)
34public class ProcessManagerTest extends TestCase {
35
36    @TestInfo(
37      level = TestLevel.COMPLETE,
38      purpose = "",
39      targets = {
40        @TestTarget(
41          methodName = "getOutputStream",
42          methodArgs = {}
43        )
44    })
45    public void testCat() throws IOException, InterruptedException {
46        String[] commands = { "cat" };
47        Process process = Runtime.getRuntime().exec(commands, null, null);
48
49        OutputStream out = process.getOutputStream();
50        String greeting = "Hello, World!";
51        out.write(greeting.getBytes());
52        out.write('\n');
53        out.close();
54
55        assertEquals(greeting, readLine(process));
56    }
57    @TestInfo(
58      level = TestLevel.PARTIAL,
59      purpose = "InterruptedException  is not verified.",
60      targets = {
61        @TestTarget(
62          methodName = "waitFor",
63          methodArgs = {}
64        )
65    })
66    public void testSleep() throws IOException, InterruptedException {
67        String[] commands = { "sleep", "1" };
68        Process process = Runtime.getRuntime().exec(commands, null, null);
69        assertEquals(0, process.waitFor());
70    }
71    @TestInfo(
72      level = TestLevel.COMPLETE,
73      purpose = "",
74      targets = {
75        @TestTarget(
76          methodName = "getInputStream",
77          methodArgs = {}
78        )
79    })
80    public void testPwd() throws IOException, InterruptedException {
81        String[] commands = { "sh", "-c", "pwd" };
82        Process process = Runtime.getRuntime().exec(
83                commands, null, new File("/"));
84        logErrors(process);
85        assertEquals("/", readLine(process));
86    }
87    @TestInfo(
88      level = TestLevel.COMPLETE,
89      purpose = "",
90      targets = {
91        @TestTarget(
92          methodName = "getInputStream",
93          methodArgs = {}
94        )
95    })
96    public void testEnvironment() throws IOException, InterruptedException {
97        String[] commands = { "sh", "-c", "echo $FOO" };
98
99        // Remember to set the path so we can find sh.
100        String[] environment = { "FOO=foo", "PATH=" + System.getenv("PATH") };
101        Process process = Runtime.getRuntime().exec(
102                commands, environment, null);
103        logErrors(process);
104        assertEquals("foo", readLine(process));
105    }
106
107    String readLine(Process process) throws IOException {
108        InputStream in = process.getInputStream();
109        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
110        return reader.readLine();
111    }
112
113    void logErrors(final Process process) throws IOException {
114        Thread thread = new Thread() {
115            public void run() {
116                InputStream in = process.getErrorStream();
117                BufferedReader reader
118                        = new BufferedReader(new InputStreamReader(in));
119                String line;
120                try {
121                    while ((line = reader.readLine()) != null) {
122                        System.err.println(line);
123                    }
124                } catch (IOException e) {
125                    e.printStackTrace();
126                }
127            }
128        };
129        thread.setDaemon(true);
130        thread.start();
131    }
132
133    public void testHeavyLoad() {
134        int i;
135        for (i = 0; i < 2000; i++)
136            stuff(i);
137    }
138
139    private static void stuff(int iteration) {
140        Runtime rt = Runtime.getRuntime();
141        try {
142            Process proc = rt.exec("ls");
143            int code = proc.waitFor();
144            proc = null;
145        } catch (Exception ex) {
146            System.err.println("Failure: " + ex);
147            throw new RuntimeException(ex);
148        }
149        rt.gc();
150        rt = null;
151    }
152}
153