ProcessTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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.luni.tests.java.lang; 19 20import java.io.IOException; 21import java.io.InputStream; 22import java.io.OutputStream; 23 24import tests.support.Support_Exec; 25 26public class ProcessTest extends junit.framework.TestCase { 27 28 /** 29 * @tests java.lang.Process#getInputStream() 30 */ 31 public void test_getInputStream() { 32 try { 33 // Test for: 34 Object[] execArgs = Support_Exec.execJava2( 35 new String[] { "tests.support.Support_AvailTest" }, null, 36 true); 37 Process proc = (Process) execArgs[0]; 38 39 OutputStream os = proc.getOutputStream(); 40 41 // first number indicates total stream length 42 // second number indicates length of data after second space 43 // this will allow us to verify length at start, middle, and end 44 os.write("10 5 abcde".getBytes()); 45 os.close(); 46 47 InputStream is = proc.getInputStream(); 48 StringBuffer msg = new StringBuffer(""); 49 while (true) { 50 int c = is.read(); 51 if (c == -1) 52 break; 53 msg.append((char) c); 54 } 55 is.close(); 56 proc.waitFor(); 57 Support_Exec.checkStderr(execArgs); 58 proc.destroy(); 59 assertEquals("true", msg.toString(), msg.toString()); 60 } catch (IOException e) { 61 fail("IOException executing avail test: " + e); 62 } catch (InterruptedException e) { 63 fail("InterruptedException executing avail test: " + e); 64 } 65 } 66 67 /** 68 * @tests java.lang.Process#getOutputStream() 69 */ 70 public void test_getOutputStream() { 71 try { 72 Object[] execArgs = Support_Exec 73 .execJava2( 74 new String[] { "tests.support.Support_ProcessReadWriteTest" }, 75 null, true); 76 Process proc = (Process) execArgs[0]; 77 78 OutputStream os = proc.getOutputStream(); 79 80 // send data, and check if it is echoed back correctly 81 String str1 = "Some data for testing communication between processes\n"; 82 String str2 = "More data that serves the same purpose.\n"; 83 String str3 = "Here is some more data.\n"; 84 os.write(str1.getBytes()); 85 try { 86 Thread.sleep(1000); 87 } catch (InterruptedException e) { 88 e.printStackTrace(); 89 } 90 os.write(str2.getBytes()); 91 os.write(str3.getBytes()); 92 os.close(); 93 94 InputStream is = proc.getInputStream(); 95 StringBuffer msg = new StringBuffer(""); 96 while (true) { 97 int c = is.read(); 98 if (c == -1) 99 break; 100 msg.append((char) c); 101 } 102 is.close(); 103 proc.waitFor(); 104 Support_Exec.checkStderr(execArgs); 105 proc.destroy(); 106 String org = str1 + str2 + str3; 107 String recvd = msg.toString(); 108 if (!recvd.equals(org)) { 109 System.out.println("Sent:"); 110 for (int i = 0; i < org.length(); i++) { 111 if (i != 0 && i % 16 == 0) 112 System.out.println(); 113 System.out.print(Integer.toHexString(org.charAt(i)) + " "); 114 } 115 System.out.println(); 116 System.out.println("Received:"); 117 for (int i = 0; i < recvd.length(); i++) { 118 if (i != 0 && i % 16 == 0) 119 System.out.println(); 120 System.out 121 .print(Integer.toHexString(recvd.charAt(i)) + " "); 122 } 123 System.out.println(); 124 } 125 assertTrue("Data returned did not match data sent. Received: '" 126 + recvd + "' sent: '" + org + "'", recvd.equals(org)); 127 } catch (IOException e) { 128 fail("IOException executing avail test: " + e); 129 } catch (InterruptedException e) { 130 fail("InterruptedException executing avail test: " + e); 131 } 132 } 133 134 protected void setUp() { 135 } 136 137 protected void tearDown() { 138 } 139 140 protected void doneSuite() { 141 } 142} 143