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 RuntimeAdditionalTest12 extends TestCase {
044: /**
045: * read, write info using streams of two process (cat txt/so <->cat) using
046: * the "exitValue - IllegalThreadStateException" loop to try to read p1-out
047: * to write p2-in and to read p2-out again
048: */
049: public void test_12() {
050: System.out.println("==test_12===");
051: //String cmnd = null;
052: String cmnd1 = null;
053: String cmnd2 = null;
054: if (RuntimeAdditionalTest0.os.equals("Win")) {
055: //cmnd1 = RuntimeAdditionalTest0.cm+" /C cat \"C:\\WINNT\\system32\\cmd.exe\"";
056: //\\//\\cmnd1 = RuntimeAdditionalTest0.cm+" /C cat
057: // \"C:\\IJE\\orp\\ZSS\\___LOG2.DO_VM_CLEAN no\"";
058: cmnd1 = RuntimeAdditionalTest0.cm + " /C cat \""
059: + RuntimeAdditionalTest0.textFile + "\"";
060: cmnd2 = RuntimeAdditionalTest0.cm + " /C cat";
061: } else if (RuntimeAdditionalTest0.os.equals("Lin")) {
062: //cmnd = "sh -c \"cat -v /lib/ld.so\"";
063: //\\//\\cmnd1 = "cat -v /lib/ld.so";
064: cmnd1 = "/bin/cat -v \"" + RuntimeAdditionalTest0.libFile
065: + "\"";
066: cmnd2 = "/bin/sh -c cat";
067: } else {
068: fail("WARNING (test_1): unknown operating system.");
069: }
070:
071: Process pi3 = null;
072: Process pi4 = null;
073: java.io.OutputStream os = null;
074: java.io.InputStream es = null;
075: java.io.InputStream is = null;
076: java.io.OutputStream os4 = null;
077: java.io.InputStream es4 = null;
078: java.io.InputStream is4 = null;
079: try {
080: //Process pi3 = Runtime.getRuntime().exec(cmnd+" /C cat
081: // \"C:\\Documents and Settings\\All Users\\Documents\\My
082: // Pictures\\Sample Pictures\\Winter.jpg\"");
083: pi3 = Runtime.getRuntime().exec(cmnd1);
084: pi4 = Runtime.getRuntime().exec(cmnd2);
085: try {
086: Thread.sleep(2000);
087: } catch (Exception e) {
088: }
089: os = pi3.getOutputStream();
090: es = pi3.getErrorStream();
091: is = pi3.getInputStream();
092: os4 = pi4.getOutputStream();
093: es4 = pi4.getErrorStream();
094: is4 = pi4.getInputStream();
095:
096: int ia;
097: while (true) {
098: while ((ia = is.available()) != 0) {
099: ///System.out.println(111);
100: byte[] bbb = new byte[ia];
101: is.read(bbb);
102: ///System.out.println(111.1);
103: os4.write(bbb);
104: int ia2 = 0;
105: while (ia != is4.available()) {
106: os4.flush();
107: //System.out.println(111.3);
108: ia2++;
109: if (ia2 > 10000) {
110: fail("ERROR (test_12): something wrong in the test - to investigate.");
111: }
112: }
113: ///System.out.println(111.2);
114: while ((ia = is4.available()) != 0) {
115: ///System.out.println(222);
116: byte[] bbb4 = new byte[ia];
117: is4.read(bbb4);
118: //\\//\\System.out.println(new String(bbb4));
119: }
120: }
121: try {
122: pi3.exitValue();
123: while ((ia = is.available()) != 0) {
124: ///System.out.println(333);
125: byte[] bbb = new byte[ia];
126: is.read(bbb);
127: os4.write(bbb);
128: while ((ia = is4.available()) != 0) {
129: //System.out.println(444);
130: byte[] bbb4 = new byte[ia];
131: is4.read(bbb4);
132: //\\//\\System.out.println(new String(bbb4));
133: }
134: }
135: break;
136: } catch (IllegalThreadStateException e) {
137: continue;
138: }
139: }
140:
141: } catch (Exception eeee) {
142: eeee.printStackTrace();
143: fail("ERROR (test_12): unexpected exception.");
144: }
145: if (!RuntimeAdditionalSupport1.openingFlag) { // to remember about the issue in Process.destroy() implementation
146: try {
147: os.close();
148: es.close();
149: is.close();
150: os4.close();
151: es4.close();
152: is4.close();
153: pi3.destroy();
154: pi4.destroy();
155: pi3.exitValue();
156: pi4.exitValue();
157: cmnd1 = RuntimeAdditionalTest0.cm + " /C ps -ef";
158: Process pi5 = Runtime.getRuntime().exec(cmnd1);
159: BufferedReader br = new BufferedReader(
160: new InputStreamReader(pi5.getInputStream()));
161: boolean flg = true;
162: String procValue = null;
163: while ((procValue = br.readLine()) != null) {
164: if (procValue.indexOf("cat") != -1) {
165: System.out.println(111);
166: flg = false;
167: break;
168: }
169: }
170: if (flg) {
171: return;
172: }
173: String as[] = procValue.split(" ");
174: for (int i = 0; i < as.length; i++) {
175: if (as[i].matches("\\d+")) {
176: System.out.println(222);
177: cmnd1 = RuntimeAdditionalTest0.cm + " /C kill "
178: + as[i];
179: Runtime.getRuntime().exec(cmnd1);
180: Thread.sleep(3000);
181: }
182: }
183: } catch (Exception e) {
184: fail("ERROR (test_14): unexpected exception: "
185: + e.toString());
186: }
187: }
188: }
189: }
|