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: /**
019: * @author Serguei S.Zapreyev
020: * @version $Revision$
021: */package java.lang;
022:
023: import java.io.BufferedReader;
024: import java.io.InputStreamReader;
025:
026: import junit.framework.TestCase;
027:
028: /*
029: * Created on March 29, 2006
030: *
031: * This RuntimeAdditionalTest class is used to test the Core API Runtime class
032: *
033: */
034:
035: /**
036: * ###############################################################################
037: * ###############################################################################
038: * TODO: 1.
039: * ###############################################################################
040: * ###############################################################################
041: */
042:
043: public class RuntimeAdditionalTest35 extends TestCase {
044: /**
045: * creat two cat-process, multi_byte-read from one multi_byte-write (using
046: * flushing) to another and multi_byte-read there again wait finish of both
047: * processes after the reading using "exitValue-destroy" loops
048: */
049: public void test_34() { // it can hang the test set run
050: System.out.println("==test_34===");
051: Process pi3 = null;
052: Process pi4 = null;
053: //String cmnd = null;
054: String cmnd1 = null;
055: String cmnd2 = null;
056: if (RuntimeAdditionalTest0.os.equals("Win")) {
057: //cmnd1 = RuntimeAdditionalTest0.cm+" /C cat \"C:\\WINNT\\system32\\cmd.exe\"";
058: //\\//\\cmnd1 = RuntimeAdditionalTest0.cm+" /C cat \"C:\\Documents and Settings\\All
059: // Users\\Documents\\My Pictures\\Sample Pictures\\Winter.jpg\"";
060: cmnd1 = RuntimeAdditionalTest0.cm + " /C cat \""
061: + RuntimeAdditionalTest0.libFile + "\"";
062: cmnd2 = RuntimeAdditionalTest0.cm + " /C cat";
063: } else if (RuntimeAdditionalTest0.os.equals("Lin")) {
064: //cmnd1 = "sh -c sh -c \"cat -v /lib/ld.so\"";
065: //cmnd1 = "cat -v /lib/ld.so";
066: //\\//\\cmnd1 = "cat -v /bin/echo";
067: cmnd1 = "cat -v \"" + RuntimeAdditionalTest0.libFile + "\"";
068: cmnd2 = "sh -c cat";
069: } else {
070: fail("WARNING (test_1): unknown operating system.");
071: }
072: try {
073:
074: /* Process */pi3 = Runtime.getRuntime().exec(cmnd1);
075: //Process pi3 = Runtime.getRuntime().exec(cmnd+" /C cat
076: // \"C:\\IJE\\orp\\ZSS\\___LOG2.DO_VM_CLEAN no\"");
077: //Process pi3 = Runtime.getRuntime().exec(cmnd+" /C tree");
078: /* Process */pi4 = Runtime.getRuntime().exec(cmnd2);
079: try {
080: Thread.sleep(2000);
081: } catch (Exception e) {
082: }
083: pi3.getOutputStream();
084: pi3.getErrorStream();
085: java.io.InputStream is = pi3.getInputStream();
086: java.io.OutputStream os4 = pi4.getOutputStream();
087: pi4.getErrorStream();
088: java.io.InputStream is4 = pi4.getInputStream();
089:
090: int ia;
091: while (true) {
092: while ((ia = is.available()) != 0) {
093: byte[] bbb = new byte[ia];
094: is.read(bbb);
095: os4.write(bbb);
096: while (ia != is4.available()) {
097: os4.flush();
098: }
099: while ((ia = is4.available()) != 0) {
100: byte[] bbb4 = new byte[ia];
101: is4.read(bbb4);
102: //\\//\\System.out.println(new String(bbb4));
103: }
104: }
105: try {
106: pi3.exitValue();
107: while ((ia = is.available()) != 0) {
108: byte[] bbb = new byte[ia];
109: is.read(bbb);
110: os4.write(bbb);
111: while ((ia = is4.available()) != 0) {
112: byte[] bbb4 = new byte[ia];
113: is4.read(bbb4);
114: //\\//\\System.out.println(new String(bbb4));
115: }
116: }
117: break;
118: } catch (IllegalThreadStateException e) {
119: continue;
120: }
121: }
122:
123: } catch (Exception eeee) {
124: eeee.printStackTrace();
125: fail("ERROR (test_34): unexpected exception.");
126: }
127: int i = 0;
128: while (true) {
129: try {
130: i++;
131: /*System.out.println(*/pi3.exitValue()/*)*/;
132: break;
133: } catch (IllegalThreadStateException e) {
134: try {
135: pi3.destroy();
136: Thread.sleep(10);
137: } catch (Exception ee) {
138: }
139: continue;
140: }
141: }
142:
143: i = 0;
144: while (true) {
145: try {
146: i++;
147: /*System.out.println(*/pi4.exitValue()/*)*/;
148: break;
149: } catch (IllegalThreadStateException e) {
150: try {
151: pi4.destroy();
152: Thread.sleep(10);
153: } catch (Exception ee) {
154: }
155: continue;
156: }
157: }
158: try {
159: //cmnd1 = RuntimeAdditionalTest0.cm + " /C ps -ef";
160: cmnd1 = RuntimeAdditionalTest0.os.equals("Win") ? RuntimeAdditionalTest0.cm
161: + " /C ps -Ws"
162: : RuntimeAdditionalTest0.cm + " -c \"ps -ef\"";
163: Process pi5 = Runtime.getRuntime().exec(cmnd1);
164: BufferedReader br = new BufferedReader(
165: new InputStreamReader(pi5.getInputStream()));
166: boolean flg = true;
167: String procValue = null;
168: while ((procValue = br.readLine()) != null) {
169: if (procValue.indexOf("cat") != -1) {
170: System.out.println(111);
171: flg = false;
172: break;
173: }
174: }
175: if (flg) {
176: return;
177: }
178: String as[] = procValue.split(" ");
179: for (int ii = 0; ii < as.length; ii++) {
180: if (as[ii].matches("\\d+")) {
181: System.out.println(222);
182: cmnd1 = RuntimeAdditionalTest0.os.equals("Win") ? RuntimeAdditionalTest0.cm
183: + " /C kill " + as[i]
184: : RuntimeAdditionalTest0.cm + " -c \"kill "
185: + as[i] + "\"";
186: Runtime.getRuntime().exec(cmnd1);
187: Thread.sleep(3000);
188: }
189: }
190: } catch (Exception e) {
191: fail("ERROR (test_34): unexpected exception: "
192: + e.toString());
193: }
194: }
195: }
|