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 RuntimeAdditionalTest40 extends TestCase {
044: /**
045: * create thread with two cat-process multi_byte-interconnected create two
046: * thread waiting these processes correspondingly interrupt the last two
047: * process to provide InterruptedException rising
048: */
049: class thread1 extends Thread {
050: public void run() {
051: String cmnd1 = null;
052: String cmnd2 = null;
053: if (RuntimeAdditionalTest0.os.equals("Win")) {
054: //cmnd1 = RuntimeAdditionalTest0.cm + " /C tree \"C:\\Documents and Settings\"";
055: //cmnd1 = "C:\\WINNT\\system32\\tree.com \"C:\\Documents and Settings\"";
056: /**/cmnd1 = RuntimeAdditionalTest0.treeStarter
057: + " \"C:\\Documents and Settings\"";
058: //cmnd2 = RuntimeAdditionalTest0.cm + " /C cat";
059: //cmnd2 = "C:\\CygWin\\bin\\cat.exe";
060: /**/cmnd2 = RuntimeAdditionalTest0.catStarter;
061: } else if (RuntimeAdditionalTest0.os.equals("Lin")) {
062: //cmnd1 = "/usr/bin/tree /";
063: /**/cmnd1 = RuntimeAdditionalTest0.treeStarter + " /";
064: //cmnd1 = "/usr/bin/tree /lib";
065: //cmnd1 = "/usr/bin/tree /bin";
066: //cmnd2 = "/bin/sh -c cat";
067: cmnd2 = RuntimeAdditionalTest0.catStarter;
068: } else {
069: fail("WARNING (test_1): unknown operating system.");
070: }
071: try {
072: //System.out.println("thread1: 1");
073: //**/RuntimeAdditionalTest0.doMessage("thread1: 1\n");
074: ppp3 = Runtime.getRuntime().exec(cmnd1);
075: ppp4 = Runtime.getRuntime().exec(cmnd2);
076: try {
077: Thread.sleep(2000);
078: } catch (Exception e) {
079: }
080: ppp3.getOutputStream();
081: ppp3.getErrorStream();
082: java.io.InputStream is = ppp3.getInputStream();
083: BufferedReader br = new BufferedReader(
084: new InputStreamReader(is));
085: String procValue = null;
086: //**/RuntimeAdditionalTest0.doMessage(cmnd1);
087: java.io.OutputStream os4 = ppp4.getOutputStream();
088: ppp4.getErrorStream();
089: java.io.InputStream is4 = ppp4.getInputStream();
090: //**/System.out.println("thread1: 2");
091: //**/RuntimeAdditionalTest0.doMessage("thread1: 2\n");
092:
093: int ia;
094: int ii = 0;
095: while (true) {
096: while ((ia = is.available()) != 0) {
097: //**/RuntimeAdditionalTest0.doMessage("XXXXXXXXXXXXXX1!\n");
098: byte[] bbb = new byte[ia];
099: is.read(bbb);
100: os4.write(bbb);
101: //**/RuntimeAdditionalTest0.doMessage("XXXXXXXXXXXXXX2!\n");
102: while (ia != is4.available()) {
103: os4.flush();
104: }
105: //**/RuntimeAdditionalTest0.doMessage("XXXXXXXXXXXXXX3!\n");
106: while ((ia = is4.available()) != 0) {
107: byte[] bbb4 = new byte[ia];
108: is4.read(bbb4);
109: //\\//\\System.out.println(new String(bbb4));
110: }
111: //**/RuntimeAdditionalTest0.doMessage("XXXXXXXXXXXXXX4!\n");
112: ii++;
113: //if (ii % 500 == 0) System.out.println("thread1: 3");
114: flg = true; //####################################################
115: try {
116: Thread.sleep(20);
117: } catch (Exception e) {
118: }
119: }
120: try {
121: //**/System.out.println("thread1: 4");
122: //**/RuntimeAdditionalTest0.doMessage("thread1: 4\n");
123: ppp3.exitValue();
124: while ((ia = is.available()) != 0) {
125: byte[] bbb = new byte[ia];
126: is.read(bbb);
127: os4.write(bbb);
128: while ((ia = is4.available()) != 0) {
129: byte[] bbb4 = new byte[ia];
130: is4.read(bbb4);
131: //\\//\\System.out.println(new String(bbb4));
132: }
133: }
134: break;
135: } catch (IllegalThreadStateException e) {
136: continue;
137: }
138: }
139:
140: } catch (Exception eeee) {
141: eeee.printStackTrace();
142: fail("ERROR (test_38, thread1): unexpected exception.");
143: }
144: }
145:
146: protected void finalize() throws Throwable {
147: super .finalize();
148: //System.out.println("thread1: finalize");
149: //**/RuntimeAdditionalTest0.doMessage("thread1: finalize\n");
150: System.out.flush();
151: }
152: }
153:
154: class thread2 extends Thread {
155: public void run() {
156: //System.out.println("thread2: 1");
157: //**/RuntimeAdditionalTest0.doMessage("thread2: 1\n");
158: while (ppp3 == null) {
159: try {
160: Thread.sleep(10);
161: } catch (Exception _) {
162: }
163: }
164: //System.out.println("thread2: 2");
165: //**/RuntimeAdditionalTest0.doMessage("thread2: 2\n");
166: try {
167: ppp3.waitFor();
168: } catch (InterruptedException _) {
169: flg2 = true;
170: //System.out.println("InterruptedException 2");
171: //**/RuntimeAdditionalTest0.doMessage("InterruptedException 2\n");
172: }
173: //System.out.println("thread2: 3");
174: //**/RuntimeAdditionalTest0.doMessage("thread2: 3\n");
175: }
176:
177: protected void finalize() throws Throwable {
178: super .finalize();
179: //**/System.out.println("thread2: finalize");
180: //**/RuntimeAdditionalTest0.doMessage("thread2: finalize\n");
181: System.out.flush();
182: }
183: }
184:
185: class thread3 extends Thread {
186: public void run() {
187: //System.out.println("thread3: 1");
188: //**/RuntimeAdditionalTest0.doMessage("thread3: 1\n");
189: while (ppp4 == null) {
190: try {
191: Thread.sleep(10);
192: } catch (Exception _) {
193: }
194: }
195: //System.out.println("thread3: 2");
196: //**/RuntimeAdditionalTest0.doMessage("thread3: 2\n");
197: try {
198: ppp4.waitFor();
199: } catch (InterruptedException _) {
200: flg3 = true;
201: //System.out.println("InterruptedException 3");
202: }
203: //System.out.println("thread3: 3");
204: //**/RuntimeAdditionalTest0.doMessage("thread3: 3\n");
205: }
206:
207: protected void finalize() throws Throwable {
208: super .finalize();
209: //System.out.println("thread2: finalize");
210: //**/RuntimeAdditionalTest0.doMessage("thread2: finalize\n");
211: System.out.flush();
212: }
213: }
214:
215: static Process ppp3 = null;
216:
217: static Process ppp4 = null;
218:
219: static boolean flg = false;
220:
221: static boolean flg2 = false;
222:
223: static boolean flg3 = false;
224:
225: public void test_38() {
226: System.out.println("==test_38===");
227: if (RuntimeAdditionalTest0.os.equals("Unk")) {
228: fail("WARNING (test_2): unknown operating system.");
229: }
230: Runtime.runFinalizersOnExit(true);
231: Thread t1 = null;
232: Thread t2 = null;
233: Thread t3 = null;
234: try {
235: t1 = new thread1();
236: t1.start();
237: while (!flg) {
238: try {
239: Thread.sleep(10);
240: } catch (Exception _) {
241: }
242: }
243: t2 = new thread2();
244: t2.start();
245: try {
246: Thread.sleep(100);
247: } catch (Exception _) {
248: }
249: t3 = new thread3();
250: t3.start();
251: try {
252: Thread.sleep(100);
253: } catch (Exception _) {
254: }
255: t2.interrupt();
256: //System.out.println("== interrupt t2 ===");
257: //**/RuntimeAdditionalTest0.doMessage("== interrupt t2 ===\n");
258: //t2.join();
259: t3.interrupt();
260: //System.out.println("== interrupt t3 ===");
261: //**/RuntimeAdditionalTest0.doMessage("== interrupt t3 ===\n");
262: //t3.join();
263: ppp3.destroy();
264: ppp4.destroy();
265: t1.interrupt();
266: t3.join();
267: t2.join();
268: t1.join();
269: //try{Thread.sleep(5000);}catch(Exception _){}
270: //System.out.println("== joined ===");
271: //**/RuntimeAdditionalTest0.doMessage("== joined ===\n");
272: } catch (Exception e) {
273: e.printStackTrace();
274: try {
275: ppp3.waitFor();
276: } catch (InterruptedException ee) {
277: ee.printStackTrace();
278: try {
279: ppp4.waitFor();
280: } catch (InterruptedException eee) {
281: eee.printStackTrace();
282: //System.out.println(flg2 + "." + flg3);
283: //**/RuntimeAdditionalTest0.doMessage(Boolean.valueOf(flg2) + "." + Boolean.valueOf(flg3));
284: return;
285: }
286: }
287: }
288: if (!(flg2 && flg3)) {
289: ppp3.destroy();
290: ppp4.destroy();
291: t1.interrupt();
292: //t1.destroy();
293: t2.interrupt();
294: //t2.destroy();
295: t3.interrupt();
296: //t3.destroy();
297: fail("ERROR (test_38): InterruptedException should be risen.");
298: } else {
299: System.out.println("############################");
300: System.out.println("## test_38 PASSED anyway. ##");
301: System.out.println("############################");
302: }
303: RuntimeAdditionalTest0.killCat();
304: }
305: }
|