001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.lang;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023:
024: import tests.support.Support_Exec;
025:
026: public class ProcessTest extends junit.framework.TestCase {
027:
028: /**
029: * @tests java.lang.Process#getInputStream()
030: */
031: public void test_getInputStream() {
032: try {
033: // Test for:
034: Object[] execArgs = Support_Exec.execJava2(
035: new String[] { "tests.support.Support_AvailTest" },
036: null, true);
037: Process proc = (Process) execArgs[0];
038:
039: OutputStream os = proc.getOutputStream();
040:
041: // first number indicates total stream length
042: // second number indicates length of data after second space
043: // this will allow us to verify length at start, middle, and end
044: os.write("10 5 abcde".getBytes());
045: os.close();
046:
047: InputStream is = proc.getInputStream();
048: StringBuffer msg = new StringBuffer("");
049: while (true) {
050: int c = is.read();
051: if (c == -1)
052: break;
053: msg.append((char) c);
054: }
055: is.close();
056: proc.waitFor();
057: Support_Exec.checkStderr(execArgs);
058: proc.destroy();
059: assertEquals("true", msg.toString(), msg.toString());
060: } catch (IOException e) {
061: fail("IOException executing avail test: " + e);
062: } catch (InterruptedException e) {
063: fail("InterruptedException executing avail test: " + e);
064: }
065: }
066:
067: /**
068: * @tests java.lang.Process#getOutputStream()
069: */
070: public void test_getOutputStream() {
071: try {
072: Object[] execArgs = Support_Exec
073: .execJava2(
074: new String[] { "tests.support.Support_ProcessReadWriteTest" },
075: null, true);
076: Process proc = (Process) execArgs[0];
077:
078: OutputStream os = proc.getOutputStream();
079:
080: // send data, and check if it is echoed back correctly
081: String str1 = "Some data for testing communication between processes\n";
082: String str2 = "More data that serves the same purpose.\n";
083: String str3 = "Here is some more data.\n";
084: os.write(str1.getBytes());
085: try {
086: Thread.sleep(1000);
087: } catch (InterruptedException e) {
088: e.printStackTrace();
089: }
090: os.write(str2.getBytes());
091: os.write(str3.getBytes());
092: os.close();
093:
094: InputStream is = proc.getInputStream();
095: StringBuffer msg = new StringBuffer("");
096: while (true) {
097: int c = is.read();
098: if (c == -1)
099: 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: }
116: System.out.println();
117: System.out.println("Received:");
118: for (int i = 0; i < recvd.length(); i++) {
119: if (i != 0 && i % 16 == 0)
120: System.out.println();
121: System.out.print(Integer.toHexString(recvd
122: .charAt(i))
123: + " ");
124: }
125: System.out.println();
126: }
127: assertTrue(
128: "Data returned did not match data sent. Received: '"
129: + recvd + "' sent: '" + org + "'", recvd
130: .equals(org));
131: } catch (IOException e) {
132: fail("IOException executing avail test: " + e);
133: } catch (InterruptedException e) {
134: fail("InterruptedException executing avail test: " + e);
135: }
136: }
137:
138: protected void setUp() {
139: }
140:
141: protected void tearDown() {
142: }
143:
144: protected void doneSuite() {
145: }
146: }
|