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 junit.framework.TestCase;
024:
025: /*
026: * Created on March 29, 2006
027: *
028: * This RuntimeAdditionalTest class is used to test the Core API Runtime class
029: *
030: */
031:
032: /**
033: * ###############################################################################
034: * ###############################################################################
035: * TODO: 1.
036: * ###############################################################################
037: * ###############################################################################
038: */
039:
040: public class RuntimeAdditionalTest42 extends TestCase {
041: /**
042: * create tree-process and cat-process, organize their multi_byte-interconnection using flushing
043: */
044: public void test_40() {
045: System.out.println("==test_40===");
046: //String cmnd = null;
047: String cmnd1 = null;
048: String cmnd2 = null;
049: if (RuntimeAdditionalTest0.os.equals("Win")) {
050: //cmnd1 = RuntimeAdditionalTest0.cm + " /C tree \"C:\\Documents and Settings\"";
051: //cmnd2 = RuntimeAdditionalTest0.cm + " /C cat";
052: cmnd1 = RuntimeAdditionalTest0.treeStarter
053: + " \"C:\\Documents and Settings\"";
054: cmnd2 = RuntimeAdditionalTest0.catStarter;
055: } else if (RuntimeAdditionalTest0.os.equals("Lin")) {
056: //cmnd1 = "sh -c \"tree /lib\"";
057: //cmnd1 = "/usr/bin/tree /lib";
058: //cmnd1 = "/usr/bin/tree /bin";
059: //cmnd2 = "sh -c cat";
060: cmnd1 = RuntimeAdditionalTest0.treeStarter + " /bin";
061: cmnd2 = RuntimeAdditionalTest0.catStarter;
062: } else {
063: fail("WARNING (test_40): unknown operating system.");
064: }
065: try {
066:
067: Process pi3 = Runtime.getRuntime().exec(cmnd1);
068: //Process pi4 = Runtime.getRuntime().exec(cmnd2);//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
069: try {
070: Thread.sleep(2000);
071: } catch (Exception e) {
072: }
073: pi3.getOutputStream();
074: pi3.getErrorStream();
075: java.io.InputStream is = pi3.getInputStream();
076: while ((is.available()) == 0) {
077: RuntimeAdditionalTest0.doMessage("1\n");
078: }//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
079: //RuntimeAdditionalTest0.doMessage("2\n");
080: Process pi4 = Runtime.getRuntime().exec(cmnd2);//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
081: java.io.OutputStream os4 = pi4.getOutputStream();
082: pi4.getErrorStream();
083: java.io.InputStream is4 = pi4.getInputStream();
084:
085: int ia;
086: int ii = 0;
087: int ia3 = 0;
088: int ia4 = 0;
089: while (true) {
090: while ((ia = is.available()) != 0) {
091: if (RuntimeAdditionalTest0.os.equals("Win")) {
092: //System.out.println(1);
093: byte[] bb = new byte[5];
094: is.read(bb, 1, 2);
095: bb[3] = 10;
096: os4.write(bb, 1, 3);
097: os4.flush();
098: while ((ia = is4.available()) != 3) {/*System.out.println(3+"|"+ia);*/
099: if (ia4++ > 10000) {
100: ia4 = 0;
101: System.out
102: .println("Warning (test_40): something wrong? We are waiting 3 bytes here as were written.");
103: break;
104: }
105: }
106: if (ia > 3) {
107: byte[] bbb = new byte[ia];
108: is4.read(bbb);
109: //fail("ERROR (test_40): flush() problem.");
110: System.out
111: .println("Warning (test_40): flush() problem has been detected! We are waiting 3 bytes here as were written.");
112: }
113: if (is4.available() != 0)/*System.out.print(Character
114: .toString((char) */
115: is4.read()/*))*/;
116: if (is4.available() != 0)/*System.out.print(Character
117: .toString((char) */
118: is4.read()/*))*/;
119: if (is4.available() != 0)
120: is4.read();
121: } else if (RuntimeAdditionalTest0.os.equals("Lin")) {
122: //System.out.println(1);
123: byte[] bb = new byte[5];
124: is.read(bb, 1, 2);
125: bb[3] = 10;
126: os4.write(bb, 1, 3);
127: os4.flush();
128: while ((ia = is4.available()) == 0) {/*System.out.println(3+"|"+ia);*/
129: } //due to our available() impl
130: try {
131: Thread.sleep(100);
132: } catch (Exception e) {
133: }
134: // System.out.println(ia);
135: // System.out.println(is4.available());
136: /*System.out.println(*/is4.read(bb, 1, 3)/*)*/;
137: // System.out.println(new String(bb, 1, 3));
138: ///**/System.out.print(Character.toString((char)/**/is4.read()/**/))/**/;
139: //while((ia = is4.available()) == 0) {System.out.println(3+"|"+ia);} //due to our available() impl
140: ///**/System.out.print(Character.toString((char)/**/is4.read()/**/))/**/;
141: //while((ia = is4.available()) == 0) {System.out.println(3+"|"+ia);} //due to our available() impl
142: //is4.read();
143: // System.out.println(is4.available());
144: if (is4.available() > 0) {
145: byte[] bbb = new byte[ia];
146: is4.read(bbb);
147: fail("ERROR (test_40): flush() problem.|"
148: + new String(bbb) + "|");
149: }
150:
151: try {
152: Thread.sleep(10);
153: } catch (Exception e) {
154: }
155: ii++;
156: if (ii > 1500) {
157: return;
158: }
159: }
160: }
161: try {
162: pi3.exitValue();
163: while ((ia = is.available()) != 0) {
164: os4.write(is.read());
165: if ((ia = is4.available()) == 0) {
166: os4.flush();
167: if (is4.available() != 1) {
168: fail("ERROR (test_40): 3.");
169: }
170: } else if (ia > 1) {
171: fail("ERROR (test_40): 4.");
172: }
173: is4.read();
174: }
175: break;
176: } catch (IllegalThreadStateException e) {
177: if (ia3++ > 1000)
178: break;
179: continue;
180: }
181: }
182: RuntimeAdditionalTest0.killTree();
183: RuntimeAdditionalTest0.killCat();
184: } catch (Exception eeee) {
185: eeee.printStackTrace();
186: fail("ERROR (test_40): unexpected exception.");
187: }
188: }
189: }
|