summaryrefslogtreecommitdiffstats
path: root/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/ProcessTest.java
blob: b87105b4066535c563a2a9e8b8f797b8592067bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.harmony.tests.java.lang;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

public class ProcessTest extends junit.framework.TestCase {

  public void test_55017() throws Exception {
    ArrayList<Process> children = new ArrayList<Process>();
    for (int i = 0; i < 256; ++i) {
      try {
        children.add(Runtime.getRuntime().exec(new String[] { "/system/bin/does-not-exist" }, null, null));
        System.gc();
      } catch (IOException expected) {
      }
    }
    assertEquals(0, children.size());

    boolean onDevice = new File("/system/bin").exists();
    String[] psCommand = onDevice ? new String[] { "ps" } : new String[] { "ps", "s" };
    Process ps = Runtime.getRuntime().exec(psCommand, null, null);
    int zombieCount = 0;
    for (String line : readAndCloseStream(ps.getInputStream()).split("\n")) {
      if (line.contains(" Z ") || line.contains(" Z+ ")) {
          ++zombieCount;
      }
    }
    assertEquals(0, zombieCount);
  }

  public void test_getOutputStream() throws Exception {
    String[] commands = { "cat", "-"};
    Process p = Runtime.getRuntime().exec(commands, null, null);
    OutputStream os = p.getOutputStream();
    // send data, and check if it is echoed back correctly
    String str1 = "Some data for testing communication between processes\n";
    String str2 = "More data that serves the same purpose.\n";
    String str3 = "Here is some more data.\n";
    os.write(str1.getBytes());
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    os.write(str2.getBytes());
    os.write(str3.getBytes());
    os.close();

    String received = readAndCloseStream(p.getInputStream());
    assertEquals(str1 + str2 + str3, received);

    String stderr = readAndCloseStream(p.getErrorStream());
    assertEquals("", stderr);

    p.waitFor();
    p.destroy();
  }

  public void test_getErrorStream() throws Exception {
    String[] commands = { "cat", "--no-such-option"};
    Process p = Runtime.getRuntime().exec(commands, null, null);

    p.getOutputStream().close();

    String received = readAndCloseStream(p.getInputStream());
    assertEquals("", received);

    String stderr = readAndCloseStream(p.getErrorStream());
    assertTrue(stderr, stderr.contains("unrecognized option") || stderr.contains("invalid option"));

    p.waitFor();
    p.destroy();
  }

  private String readAndCloseStream(InputStream is) throws IOException {
    StringBuffer result = new StringBuffer();
    while (true) {
      int c = is.read();
      if (c == -1) {
        break;
      }
      result.append((char) c);
    }
    is.close();
    return result.toString();
  }

  public void test_exitValue() throws Exception {
    String[] commands = { "ls" };
    Process process = Runtime.getRuntime().exec(commands, null, null);
    process.waitFor();
    assertEquals(0, process.exitValue());

    String[] commandsSleep = { "sleep", "3000" };
    process = Runtime.getRuntime().exec(commandsSleep, null, null);
    process.destroy();
    process.waitFor(); // destroy is asynchronous.
    assertTrue(process.exitValue() != 0);

    process = Runtime.getRuntime().exec(new String[] { "sleep", "3000" }, null, null);
    try {
      process.exitValue();
      fail();
    } catch(IllegalThreadStateException expected) {
    }
  }

  public void test_destroy() throws Exception {
    String[] commands = { "ls"};
    Process process = Runtime.getRuntime().exec(commands, null, null);
    process.destroy();
    process.destroy();
    process.destroy();
  }
}