The Android Open Source Project | b5de22c | 2012-04-01 00:00:00 -0700 | [diff] [blame] | 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 | |
| 18 | package tests.api.java.lang; |
| 19 | |
| 20 | import dalvik.annotation.AndroidOnly; |
| 21 | |
| 22 | import java.io.BufferedInputStream; |
| 23 | import java.io.BufferedOutputStream; |
| 24 | import java.io.IOException; |
| 25 | import java.io.InputStream; |
| 26 | import java.io.OutputStream; |
| 27 | |
| 28 | import tests.support.Support_Exec; |
| 29 | import static tests.support.Support_Exec.javaProcessBuilder; |
| 30 | |
| 31 | public class Process2Test extends junit.framework.TestCase { |
| 32 | /** |
| 33 | * java.lang.Process#getInputStream(), |
| 34 | * java.lang.Process#getErrorStream() |
| 35 | * java.lang.Process#getOutputStream() |
| 36 | * Tests if these methods return buffered streams. |
| 37 | */ |
| 38 | @AndroidOnly("dalvikvm specific") |
| 39 | public void test_streams() |
| 40 | throws IOException, InterruptedException { |
| 41 | Process p = javaProcessBuilder().start(); |
| 42 | assertNotNull(p.getInputStream()); |
| 43 | assertNotNull(p.getErrorStream()); |
| 44 | assertNotNull(p.getOutputStream()); |
| 45 | } |
| 46 | |
| 47 | public void test_getErrorStream() { |
| 48 | String[] commands = {"ls"}; |
| 49 | Process process = null; |
| 50 | try { |
| 51 | process = Runtime.getRuntime().exec(commands, null, null); |
| 52 | InputStream is = process.getErrorStream(); |
| 53 | StringBuffer msg = new StringBuffer(""); |
| 54 | while (true) { |
| 55 | int c = is.read(); |
| 56 | if (c == -1) |
| 57 | break; |
| 58 | msg.append((char) c); |
| 59 | } |
| 60 | assertEquals("", msg.toString()); |
| 61 | } catch (IOException e) { |
| 62 | fail("IOException was thrown."); |
| 63 | } finally { |
| 64 | process.destroy(); |
| 65 | } |
| 66 | |
| 67 | String[] unknownCommands = {"mkdir", "-u", "test"}; |
| 68 | Process erProcess = null; |
| 69 | try { |
| 70 | erProcess = Runtime.getRuntime().exec(unknownCommands, null, null); |
| 71 | InputStream is = erProcess.getErrorStream(); |
| 72 | StringBuffer msg = new StringBuffer(""); |
| 73 | while (true) { |
| 74 | int c = is.read(); |
| 75 | if (c == -1) |
| 76 | break; |
| 77 | msg.append((char) c); |
| 78 | } |
| 79 | assertTrue("Error stream should not be empty", |
| 80 | !"".equals(msg.toString())); |
| 81 | } catch (IOException e) { |
| 82 | fail("IOException was thrown."); |
| 83 | } finally { |
| 84 | erProcess.destroy(); |
| 85 | } |
| 86 | } |
| 87 | } |