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.tests.java.lang; 19 20import java.io.IOException; 21import java.io.InputStream; 22 23import static tests.support.Support_Exec.javaProcessBuilder; 24 25public class Process2Test extends junit.framework.TestCase { 26 /** 27 * java.lang.Process#getInputStream(), 28 * java.lang.Process#getErrorStream() 29 * java.lang.Process#getOutputStream() 30 * Tests if these methods return buffered streams. 31 */ 32 public void test_streams() 33 throws IOException, InterruptedException { 34 Process p = javaProcessBuilder().start(); 35 assertNotNull(p.getInputStream()); 36 assertNotNull(p.getErrorStream()); 37 assertNotNull(p.getOutputStream()); 38 } 39 40 public void test_getErrorStream() { 41 String[] commands = {"sh", "-c", "echo"}; 42 Process process = null; 43 try { 44 process = Runtime.getRuntime().exec(commands, null, null); 45 InputStream is = process.getErrorStream(); 46 StringBuilder msg = new StringBuilder(""); 47 while (true) { 48 int c = is.read(); 49 if (c == -1) 50 break; 51 msg.append((char) c); 52 } 53 assertEquals("", msg.toString()); 54 } catch (IOException e) { 55 fail("IOException was thrown."); 56 } finally { 57 if (process != null) { 58 process.destroy(); 59 } 60 } 61 62 String[] unknownCommands = {"sh", "-c", "echo oops >&2"}; 63 Process erProcess = null; 64 try { 65 erProcess = Runtime.getRuntime().exec(unknownCommands, null, null); 66 InputStream is = erProcess.getErrorStream(); 67 StringBuilder msg = new StringBuilder(""); 68 while (true) { 69 int c = is.read(); 70 if (c == -1) 71 break; 72 msg.append((char) c); 73 } 74 assertEquals("oops\n", msg.toString()); 75 } catch (IOException e) { 76 fail("IOException was thrown."); 77 } finally { 78 if (erProcess != null) { 79 erProcess.destroy(); 80 } 81 } 82 } 83} 84