Home | History | Annotate | Download | only in lang
      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 
     17 package org.apache.harmony.tests.java.lang;
     18 
     19 import junit.framework.TestCase;
     20 
     21 import java.io.BufferedReader;
     22 import java.io.File;
     23 import java.io.FileOutputStream;
     24 import java.io.IOException;
     25 import java.io.InputStream;
     26 import java.io.InputStreamReader;
     27 import java.io.OutputStream;
     28 
     29 public class ProcessManagerTest extends TestCase {
     30 
     31     Thread thread = null;
     32     Process process = null;
     33     boolean isThrown = false;
     34 
     35     public void testCat() throws IOException, InterruptedException {
     36         String[] commands = { "cat" };
     37         Process process = Runtime.getRuntime().exec(commands, null, null);
     38 
     39         OutputStream out = process.getOutputStream();
     40         String greeting = "Hello, World!";
     41         out.write(greeting.getBytes());
     42         out.write('\n');
     43         out.close();
     44 
     45         assertEquals(greeting, readLine(process));
     46     }
     47 
     48     // BrokenTest: Sporadic failures in CTS, but not in CoreTestRunner
     49     public void testSleep() throws IOException {
     50         String[] commands = { "sleep", "1" };
     51         process = Runtime.getRuntime().exec(commands, null, null);
     52         try {
     53             assertEquals(0, process.waitFor());
     54 
     55         } catch(InterruptedException ie) {
     56             fail("InterruptedException was thrown.");
     57         }
     58 
     59         isThrown = false;
     60         thread = new Thread() {
     61             public void run() {
     62                 String[] commands = { "sleep", "1000"};
     63                 try {
     64                     process = Runtime.getRuntime().exec(commands, null, null);
     65                 } catch (IOException e1) {
     66                     fail("IOException was thrown.");
     67                 }
     68                 try {
     69                     process.waitFor();
     70                     fail("InterruptedException was not thrown.");
     71                 } catch(InterruptedException ie) {
     72                     isThrown = true;
     73                 }
     74             }
     75         };
     76 
     77         Thread interruptThread = new Thread() {
     78             public void run() {
     79                 try {
     80                     sleep(10);
     81                 } catch(InterruptedException ie) {
     82                     fail("InterruptedException was thrown in " +
     83                             "the interruptThread.");
     84                 }
     85                 thread.interrupt();
     86             }
     87         };
     88         thread.start();
     89         interruptThread.start();
     90         try {
     91             interruptThread.join();
     92         } catch (InterruptedException e) {
     93             fail("InterruptedException was thrown.");
     94         }
     95         try {
     96             Thread.sleep(100);
     97         } catch(InterruptedException ie) {
     98 
     99         }
    100 
    101         thread.interrupt();
    102         //process.destroy();
    103         try {
    104             Thread.sleep(100);
    105         } catch(InterruptedException ie) {
    106 
    107         }
    108 
    109         assertTrue(isThrown);
    110     }
    111 
    112     public void testPwd() throws IOException, InterruptedException {
    113         String[] commands = { "sh", "-c", "pwd" };
    114         Process process = Runtime.getRuntime().exec(
    115                 commands, null, new File("/"));
    116         logErrors(process);
    117         assertEquals("/", readLine(process));
    118     }
    119 
    120     public void testEnvironment() throws IOException, InterruptedException {
    121         String[] commands = { "sh", "-c", "echo $FOO" };
    122 
    123         // Remember to set the path so we can find sh.
    124         String[] environment = { "FOO=foo", "PATH=" + System.getenv("PATH") };
    125         Process process = Runtime.getRuntime().exec(
    126                 commands, environment, null);
    127         logErrors(process);
    128         assertEquals("foo", readLine(process));
    129     }
    130 
    131     String readLine(Process process) throws IOException {
    132         InputStream in = process.getInputStream();
    133         BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    134         return reader.readLine();
    135     }
    136 
    137     void logErrors(final Process process) throws IOException {
    138         Thread thread = new Thread() {
    139             public void run() {
    140                 InputStream in = process.getErrorStream();
    141                 BufferedReader reader
    142                         = new BufferedReader(new InputStreamReader(in));
    143                 String line;
    144                 try {
    145                     while ((line = reader.readLine()) != null) {
    146                         System.err.println(line);
    147                     }
    148                 } catch (IOException e) {
    149                     e.printStackTrace();
    150                 }
    151             }
    152         };
    153         thread.setDaemon(true);
    154         thread.start();
    155     }
    156 
    157     public void testHeavyLoad() {
    158         int i;
    159         for (i = 0; i < 100; i++)
    160             stuff();
    161     }
    162 
    163     private static void stuff() {
    164         Runtime rt = Runtime.getRuntime();
    165         try {
    166             Process proc = rt.exec("ls");
    167             proc.waitFor();
    168             proc = null;
    169         } catch (Exception ex) {
    170             System.err.println("Failure: " + ex);
    171             throw new RuntimeException(ex);
    172         }
    173         rt.gc();
    174         rt = null;
    175     }
    176 
    177     FileOutputStream out;
    178 
    179     public void testCloseNonStandardFds()
    180             throws IOException, InterruptedException {
    181         String[] commands = { "ls", "/proc/self/fd" };
    182 
    183         Process process = Runtime.getRuntime().exec(commands, null, null);
    184         int before = countLines(process);
    185 
    186         File tmpFile = File.createTempFile("testCloseNonStandardFds", ".txt");
    187         // Open a new fd.
    188         this.out = new FileOutputStream(tmpFile);
    189 
    190         try {
    191             process = Runtime.getRuntime().exec(commands, null, null);
    192             int after = countLines(process);
    193 
    194             // Assert that the new fd wasn't open in the second run.
    195             assertEquals(before, after);
    196         } finally {
    197             this.out.close();
    198             tmpFile.delete();
    199         }
    200     }
    201 
    202     /**
    203      * Counts lines of input from the given process. Equivalent to "wc -l".
    204      */
    205     private int countLines(Process process) throws IOException {
    206         logErrors(process);
    207         InputStream in = process.getInputStream();
    208         BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    209         int count = 0;
    210         while (reader.readLine() != null) {
    211             count++;
    212         }
    213         return count;
    214     }
    215 
    216     public void testInvalidCommand()
    217             throws IOException, InterruptedException {
    218         try {
    219             String[] commands = { "doesnotexist" };
    220             Runtime.getRuntime().exec(commands, null, null);
    221         } catch (IOException e) { /* expected */ }
    222     }
    223 }
    224