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 RuntimeAdditionalTest33 extends TestCase {
044: /**
045: * create two cat-process, multi_byte-read from one multi_byte-write (using
046: * flushing) to another and multi_byte-read there again
047: */
048: public void test_32() {
049: System.out.println("==test_32===");
050: //String cmnd = null;
051: String cmnd1 = null;
052: String cmnd2 = null;
053: if (RuntimeAdditionalTest0.os.equals("Win")) {
054: //cmnd1 = RuntimeAdditionalTest0.cm+" /C cat \"C:\\WINNT\\system32\\cmd.exe\"";
055: //\\//\\cmnd1 = RuntimeAdditionalTest0.cm+" /C cat \"C:\\Documents and Settings\\All
056: // Users\\Documents\\My Pictures\\Sample Pictures\\Winter.jpg\"";
057: cmnd1 = RuntimeAdditionalTest0.cm + " /C cat \""
058: + RuntimeAdditionalTest0.libFile + "\"";
059: cmnd2 = RuntimeAdditionalTest0.cm + " /C cat";
060: } else if (RuntimeAdditionalTest0.os.equals("Lin")) {
061: //cmnd1 = "sh -c sh -c \"cat -v /lib/ld.so\"";
062: //cmnd1 = "cat -v /lib/ld.so";
063: //\\//\\cmnd1 = "cat -v /bin/echo";
064: cmnd1 = "cat -v \"" + RuntimeAdditionalTest0.libFile + "\"";
065: cmnd2 = "sh -c cat";
066: } else {
067: fail("WARNING (test_1): unknown operating system.");
068: }
069:
070: try {
071:
072: Process pi3 = Runtime.getRuntime().exec(cmnd1);
073: //Process pi3 = Runtime.getRuntime().exec(cmnd+" /C cat
074: // \"C:\\IJE\\orp\\ZSS\\___LOG2.DO_VM_CLEAN no\"");
075: //Process pi3 = Runtime.getRuntime().exec(cmnd+" /C tree");
076: Process pi4 = Runtime.getRuntime().exec(cmnd2);
077: try {
078: Thread.sleep(2000);
079: } catch (Exception e) {
080: }
081: pi3.getOutputStream();
082: pi3.getErrorStream();
083: java.io.InputStream is = pi3.getInputStream();
084: java.io.OutputStream os4 = pi4.getOutputStream();
085: pi4.getErrorStream();
086: java.io.InputStream is4 = pi4.getInputStream();
087:
088: int ia;
089: while (true) {
090: while ((ia = is.available()) != 0) {
091: byte[] bbb = new byte[ia];
092: is.read(bbb);
093: os4.write(bbb);
094: while (ia != is4.available()) {
095: os4.flush();
096: }
097: while ((ia = is4.available()) != 0) {
098: byte[] bbb4 = new byte[ia];
099: is4.read(bbb4);
100: //\\//\\System.out.println(new String(bbb4));
101: }
102: }
103: try {
104: pi3.exitValue();
105: while ((ia = is.available()) != 0) {
106: byte[] bbb = new byte[ia];
107: is.read(bbb);
108: os4.write(bbb);
109: while ((ia = is4.available()) != 0) {
110: byte[] bbb4 = new byte[ia];
111: is4.read(bbb4);
112: //\\//\\System.out.println(new String(bbb4));
113: }
114: }
115: break;
116: } catch (IllegalThreadStateException e) {
117: continue;
118: }
119: }
120:
121: } catch (Exception eeee) {
122: eeee.printStackTrace();
123: fail("ERROR (test_32): unexpected exception.");
124: }
125: try {
126: //cmnd1 = RuntimeAdditionalTest0.cm + " /C ps -ef";
127: cmnd1 = RuntimeAdditionalTest0.os.equals("Win") ? RuntimeAdditionalTest0.cm
128: + " /C ps -Ws"
129: : RuntimeAdditionalTest0.cm + " -c \"ps -ef\"";
130: Process pi5 = Runtime.getRuntime().exec(cmnd1);
131: BufferedReader br = new BufferedReader(
132: new InputStreamReader(pi5.getInputStream()));
133: boolean flg = true;
134: String procValue = null;
135: while ((procValue = br.readLine()) != null) {
136: if (procValue.indexOf("cat") != -1) {
137: System.out.println(111);
138: flg = false;
139: break;
140: }
141: }
142: if (flg) {
143: return;
144: }
145: String as[] = procValue.split(" ");
146: for (int i = 0; i < as.length; i++) {
147: if (as[i].matches("\\d+")) {
148: System.out.println(222);
149: cmnd1 = RuntimeAdditionalTest0.os.equals("Win") ? RuntimeAdditionalTest0.cm
150: + " /C kill " + as[i]
151: : RuntimeAdditionalTest0.cm + " -c \"kill "
152: + as[i] + "\"";
153: Runtime.getRuntime().exec(cmnd1);
154: Thread.sleep(3000);
155: }
156: }
157: } catch (Exception e) {
158: fail("ERROR (test_32): unexpected exception: "
159: + e.toString());
160: }
161: }
162: }
|