ProcessTest.java revision 2f563a4590c4a0e54177906c47d50f4ef454eba5
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 tests.api.java.lang; 19 20import dalvik.annotation.BrokenTest; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetNew; 23import dalvik.annotation.TestTargetClass; 24 25import java.io.IOException; 26import java.io.InputStream; 27import java.io.OutputStream; 28 29@TestTargetClass(Process.class) 30public class ProcessTest extends junit.framework.TestCase { 31 32 /** 33 * @tests java.lang.Process#getInputStream() 34 */ 35 @TestTargetNew( 36 level = TestLevel.COMPLETE, 37 notes = "", 38 method = "getInputStream", 39 args = {} 40 ) 41 public void test_getInputStream() { 42 try { 43 // Test for: 44 //Object[] execArgs = Support_Exec.execJava2( 45 // new String[] { "tests.support.Support_AvailTest" }, null, 46 // true); 47 //Process proc = (Process) execArgs[0]; 48 49 String[] commands = { "sleep", "1"}; 50 Process proc = Runtime.getRuntime().exec(commands, null, null); 51 52 OutputStream os = proc.getOutputStream(); 53 54 // first number indicates total stream length 55 // second number indicates length of data after second space 56 // this will allow us to verify length at start, middle, and end 57 os.write("10 5 abcde".getBytes()); 58 os.close(); 59 60 InputStream is = proc.getInputStream(); 61 StringBuffer msg = new StringBuffer(""); 62 while (true) { 63 int c = is.read(); 64 if (c == -1) 65 break; 66 msg.append((char) c); 67 } 68 is.close(); 69 proc.waitFor(); 70 //Support_Exec.checkStderr(execArgs); 71 proc.destroy(); 72 assertEquals("true", msg.toString(), msg.toString()); 73 } catch (IOException e) { 74 fail("IOException executing avail test: " + e); 75 } catch (InterruptedException e) { 76 fail("InterruptedException executing avail test: " + e); 77 } 78 } 79 80 /** 81 * @tests java.lang.Process#getOutputStream() 82 */ 83 @TestTargetNew( 84 level = TestLevel.COMPLETE, 85 notes = "", 86 method = "getOutputStream", 87 args = {} 88 ) 89 public void test_getOutputStream() { 90 try { 91 String[] commands = { "sleep", "1"}; 92 Process proc = Runtime.getRuntime().exec(commands, null, null); 93 OutputStream os = proc.getOutputStream(); 94 // send data, and check if it is echoed back correctly 95 String str1 = "Some data for testing communication between processes\n"; 96 String str2 = "More data that serves the same purpose.\n"; 97 String str3 = "Here is some more data.\n"; 98 os.write(str1.getBytes()); 99 try { 100 Thread.sleep(2000); 101 } catch (InterruptedException e) { 102 e.printStackTrace(); 103 } 104 os.close(); 105 InputStream is = proc.getInputStream(); 106 StringBuffer msg = new StringBuffer(""); 107 while (true) { 108 int c = is.read(); 109 if (c == -1) 110 break; 111 msg.append((char) c); 112 } 113 is.close(); 114 proc.waitFor(); 115 //Support_Exec.checkStderr(execArgs); 116 proc.destroy(); 117 String org = str1; 118 String recvd = msg.toString(); 119 // Doesn't pass on RI 120 // assertTrue("Data returned did not match data sent. Received: '" 121 // + recvd + "' sent: '" + org + "'", recvd.equals(org)); 122 } catch (IOException e) { 123 fail("IOException executing avail test: " + e); 124 } catch (InterruptedException e) { 125 fail("InterruptedException executing avail test: " + e); 126 } 127 } 128 129 @TestTargetNew( 130 level = TestLevel.COMPLETE, 131 notes = "", 132 method = "exitValue", 133 args = {} 134 ) 135 public void test_exitValue() { 136 try { 137 String[] commands = { "ls" }; 138 Process process = Runtime.getRuntime().exec(commands, null, null); 139 try { 140 Thread.sleep(5000); 141 } catch(Exception e) { 142 143 } 144 assertTrue(process.exitValue() == 0); 145 146 String[] commandsSleep = { "sleep", "3" }; 147 process = Runtime.getRuntime().exec(commandsSleep, null, null); 148 process.destroy(); 149 try { 150 Thread.sleep(5000); 151 } catch (Exception e) {} 152 assertTrue(process.exitValue() != 0); 153 154 process = Runtime.getRuntime().exec(commandsSleep, null, null); 155 try { 156 process.exitValue(); 157 fail("IllegalThreadStateException was not thrown."); 158 } catch(IllegalThreadStateException itse) { 159 //expected 160 } 161 } catch (IOException e) { 162 fail("IOException was thrown."); 163 } 164 } 165 166 @TestTargetNew( 167 level = TestLevel.COMPLETE, 168 notes = "", 169 method = "Process", 170 args = {} 171 ) 172 public void test_Constructor() { 173 ProcessClass pc = new ProcessClass(); 174 assertTrue(pc.exitValue() == 0); 175 } 176 177 @TestTargetNew( 178 level = TestLevel.COMPLETE, 179 notes = "", 180 method = "destroy", 181 args = {} 182 ) 183 @BrokenTest("Sporadic timeouts in CTS, but not in CoreTestRunner") 184 public void test_destroy() { 185 String[] commands = { "ls"}; 186 try { 187 Process process = Runtime.getRuntime().exec(commands, null, null); 188 process.destroy(); 189 } catch (IOException e) { 190 fail("IOException was thrown."); 191 } 192 } 193 194 protected void setUp() { 195 } 196 197 protected void tearDown() { 198 } 199 200 protected void doneSuite() { 201 } 202 203 class ProcessClass extends Process { 204 205 ProcessClass() { 206 super(); 207 } 208 209 @Override 210 public void destroy() { 211 212 } 213 214 @Override 215 public int exitValue() { 216 // TODO Auto-generated method stub 217 return 0; 218 } 219 220 @Override 221 public InputStream getErrorStream() { 222 return null; 223 } 224 225 @Override 226 public InputStream getInputStream() { 227 return null; 228 } 229 230 @Override 231 public OutputStream getOutputStream() { 232 return null; 233 } 234 235 @Override 236 public int waitFor() throws InterruptedException { 237 // TODO Auto-generated method stub 238 return 0; 239 } 240 241 } 242} 243