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.File;
025: import java.io.FileOutputStream;
026: import java.io.InputStream;
027: import java.io.InputStreamReader;
028: import java.io.OutputStream;
029: import java.io.PrintStream;
030: import java.util.Locale;
031:
032: import junit.framework.TestCase;
033:
034: /**
035: * This RuntimeTest class is used to test the Core API Runtime class
036: * Created on January 5, 2005
037: *
038: * ###############################################################################
039: * ###############################################################################
040: * REMINDER("XXX") LIST:
041: * 1. [Jun 11, 2005] test_availableProcessors, test_freeMemory, test_gc, test_runFinalization,
042: * test_runFinalizersOnExit fail on "ORP+our Runtime+CLASSPATH API" platform
043: * because the availableProcessors, freeMemory, runFinalization (runFinalizersOnExit?)
044: * methods aren't correctly supported yet in orp/drl_natives/src
045: * 2. [Jun 11, 2005] test_maxMemory, test_totalMemory fail on "ORP+CLASSPATH API" platform
046: * because the maxMemory
047: * method isn't correctly supported yet in orp/drl_natives/src:
048: * (Exception: java.lang.UnsatisfiedLinkError: Error compiling method java/lang/Runtime.maxMemory()J)
049: * 3. [Jun 11, 2005] test_availableProcessors fails on "ORP+CLASSPATH API" platform
050: * because the availableProcessors
051: * method isn't correctly supported yet in orp/drl_natives/src:
052: * (Exception: java.lang.UnsatisfiedLinkError: Error compiling method java/lang/Runtime.availableProcessors()I)
053: * ###############################################################################
054: * ###############################################################################
055: */
056: public class RuntimeTest2 extends TestCase {
057:
058: static class forInternalUseOnly {
059: String stmp;
060:
061: forInternalUseOnly() {
062: this .stmp = "";
063: for (int ind2 = 0; ind2 < 100; ind2++) {
064: this .stmp += "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
065: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
066: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
067: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
068: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
069: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
070: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
071: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
072: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
073: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
074: + "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
075: }
076: }
077:
078: protected void finalize() throws Throwable {
079: runFinalizationFlag = true;
080: super .finalize();
081: }
082: }
083:
084: static boolean runFinalizationFlag = false;
085:
086: public void test_runFinalization() throws InterruptedException {
087: runFinalizationFlag = false;
088:
089: for (int ind2 = 0; ind2 < 10; ind2++) {
090: forInternalUseOnly ins = new forInternalUseOnly();
091: ins.stmp += "";
092: ins = null;
093: Thread.sleep(10);
094: Runtime.getRuntime().gc();
095: Thread.sleep(10);
096: Runtime.getRuntime().runFinalization();
097: }
098:
099: assertTrue("finalization has not been run", runFinalizationFlag);
100: }
101:
102: @SuppressWarnings("deprecation")
103: public void test_runFinalizersOnExit() throws InterruptedException {
104: runFinalizationFlag = false;
105: for (int ind2 = 0; ind2 < 5; ind2++) {
106: Runtime.runFinalizersOnExit(false);
107: forInternalUseOnly ins = new forInternalUseOnly();
108: ins.stmp += "";
109: ins = null;
110: Thread.sleep(10);
111: Runtime.getRuntime().gc();
112: Thread.sleep(10);
113: }
114:
115: assertTrue("check001: finalizers were not run",
116: runFinalizationFlag);
117:
118: runFinalizationFlag = false;
119: for (int ind2 = 0; ind2 < 5; ind2++) {
120: Runtime.runFinalizersOnExit(true);
121: forInternalUseOnly ins = new forInternalUseOnly();
122: ins.stmp += "";
123: ins = null;
124: Thread.sleep(10);
125: Runtime.getRuntime().gc();
126: Thread.sleep(10);
127: }
128: assertTrue("check002: finalizers were not run",
129: runFinalizationFlag);
130: }
131:
132: class threadForInternalUseOnly1 extends Thread {
133: public void run() {
134: int I = threadForInternalUseOnly2.getI();
135: int counter = 0;
136: while ((I < 50 || number < I) && counter < 24000) {
137: try {
138: Thread.sleep(50);
139: } catch (InterruptedException e) {
140: // ignore interruption request
141: // so reset interrupt indicator
142: this .interrupt();
143: }
144: I = threadForInternalUseOnly2.getI();
145: counter += 1;
146: }
147: }
148:
149: protected void finalize() throws Throwable {
150: if (runFinalizationFlag2 == 1 || runFinalizationFlag2 == 11
151: || runFinalizationFlag2 == 21) {
152: // :) // assertTrue( "FAILED: addShutdownHook.check001", false);
153: }
154: super .finalize();
155: }
156: }
157:
158: static class threadForInternalUseOnly2 extends Thread {
159: static int I = 0;
160: long NM;
161: int ORD;
162:
163: synchronized static void incrI() {
164: I++;
165: }
166:
167: synchronized static int getI() {
168: return I;
169: }
170:
171: threadForInternalUseOnly2(int ind) {
172: super ();
173: NM = System.currentTimeMillis();
174: ORD = ind;
175: }
176:
177: public void run() {
178: if (ORD == 1 || ORD == 11 || ORD == 21) {
179: synchronized (threadForInternalUseOnly2.class) {
180: runFinalizationFlag2 = ORD;
181: }
182: }
183: incrI();
184: for (int j = 0; j < 30/* 100 */; j++) {
185: try {
186: Thread.sleep(10);
187: } catch (InterruptedException e) {
188: // ignore request, set indicator
189: this .interrupt();
190: }
191: }
192: }
193:
194: protected void finalize() throws Throwable {
195: if (runFinalizationFlag2 == 1 || runFinalizationFlag2 == 11
196: || runFinalizationFlag2 == 21) {
197: // :) // assertTrue( "FAILED: addShutdownHook.check002", false);
198: }
199: super .finalize();
200: }
201: }
202:
203: static class threadForInternalUseOnly3 extends Thread {
204: static int I = 0;
205:
206: synchronized static void incrI() {
207: I++;
208: }
209:
210: synchronized static int getI() {
211: return I;
212: }
213:
214: public void run() {
215: incrI();
216: }
217: }
218:
219: static int runFinalizationFlag2 = -1;
220: static int number = 4; //100;
221: static int nthr = 2; //21;
222:
223: @SuppressWarnings("deprecation")
224: public void test_addShutdownHook() throws InterruptedException {
225: Thread[] thr = new Thread[number];
226: for (int i = 0; i < number / 2; i++) {
227: Runtime.getRuntime().addShutdownHook(
228: thr[2 * i + 0] = new threadForInternalUseOnly3());
229: Thread.sleep(5);
230: Runtime.getRuntime().addShutdownHook(
231: thr[2 * i + 1] = new threadForInternalUseOnly2(
232: 2 * i + 1));
233: Thread.sleep(5);
234: }
235: Runtime.runFinalizersOnExit(true);
236: new threadForInternalUseOnly1().start();
237: try {
238: Runtime.getRuntime().addShutdownHook(thr[nthr]);
239: fail("IllegalArgumentException has not been thrown");
240: } catch (IllegalArgumentException e) {
241: }
242: }
243:
244: public void test_removeShutdownHook() throws InterruptedException {
245: Thread[] thr = new Thread[number];
246: for (int i = 0; i < number / 2; i++) {
247: Runtime.getRuntime().addShutdownHook(
248: thr[2 * i + 0] = new threadForInternalUseOnly3());
249: Thread.sleep(5);
250: Runtime.getRuntime().addShutdownHook(
251: thr[2 * i + 1] = new threadForInternalUseOnly2(
252: 2 * i + 1));
253: Thread.sleep(5);
254: }
255: // Runtime.getRuntime().removeShutdownHook(thr[1]);
256: // Runtime.getRuntime().removeShutdownHook(thr[11]);
257: Runtime.getRuntime().removeShutdownHook(thr[nthr]);
258: new threadForInternalUseOnly1().start();
259:
260: // Runtime.getRuntime().addShutdownHook(thr[1]);
261: // Runtime.getRuntime().addShutdownHook(thr[11]);
262: Runtime.getRuntime().addShutdownHook(thr[nthr]);
263: // Runtime.getRuntime().removeShutdownHook(thr[1]);
264: // Runtime.getRuntime().removeShutdownHook(thr[11]);
265: Runtime.getRuntime().removeShutdownHook(thr[nthr]);
266: // Runtime.getRuntime().removeShutdownHook(thr[1]);
267: // Runtime.getRuntime().removeShutdownHook(thr[11]);
268: Runtime.getRuntime().removeShutdownHook(thr[nthr]);
269: }
270:
271: private static boolean isOSWindows() {
272: String osName = System.getProperty("os.name");
273: osName = osName.toLowerCase(Locale.US);
274: return osName.contains("windows");
275: }
276:
277: private static boolean isOSLinux() {
278: String osName = System.getProperty("os.name");
279: osName = osName.toLowerCase(Locale.US);
280: return osName.contains("linux");
281: }
282:
283: private void exec_StrForWindows() throws Exception {
284: String cmnd = "cmd /C date";
285: Process pi3 = Runtime.getRuntime().exec(cmnd);
286: OutputStream os = pi3.getOutputStream();
287: pi3.getErrorStream();
288: InputStream is = pi3.getInputStream();
289: // wait for is.available != 0
290: int count = 100;
291: while (is.available() < 60 && count-- > 0) {
292: try {
293: Thread.sleep(10);
294: } catch (InterruptedException e) {
295: }
296: }
297: if (count < 0) {
298: fail("check001: the date's reply has not been received");
299: }
300:
301: int ia = is.available();
302: byte[] bb = new byte[ia];
303: is.read(bb);
304: String r1 = new String(bb);
305: if (r1.indexOf("The current date is") == -1
306: || r1.indexOf("Enter the new date") == -1) {
307: fail("check002: " + r1);
308: }
309: for (int ii = 0; ii < ia; ii++) {
310: bb[ii] = (byte) 0;
311: }
312:
313: os.write('x');
314: os.write('x');
315: os.write('-');
316: os.write('x');
317: os.write('x');
318: os.write('-');
319: os.write('x');
320: os.write('x');
321: os.write('\n');
322: os.flush();
323:
324: // wait for is.available > 9 which means that 'is' contains
325: // both the above written value and the consequent
326: // 'date' command's reply
327: count = 300;
328: while (is.available() < 11 && count-- > 0) {
329: try {
330: Thread.sleep(5);
331: } catch (InterruptedException e) {
332: }
333: }
334: if (count < 0) {
335: fail("check003: the date's reply has not been received");
336: }
337: ia = is.available();
338: byte[] bbb = new byte[ia];
339: is.read(bbb);
340: r1 = new String(bbb);
341: if (r1.indexOf("The system cannot accept the date entered") == -1
342: && r1.indexOf("Enter the new date") == -1) {
343: fail("check004: unexpected output: " + r1);
344: }
345: os.write('\n');
346: try {
347: pi3.exitValue();
348: } catch (IllegalThreadStateException e) {
349: os.flush();
350: try {
351: pi3.waitFor();
352: } catch (InterruptedException ee) {
353: }
354: }
355: // System.out.println("5test_exec_Str");
356: // os.write('\n');
357: // os.write('\n');
358: // os.flush();
359: pi3.destroy();
360: // pi3.waitFor();
361: }
362:
363: public void test_exec_Str() throws Exception {
364: if (isOSWindows()) {
365: exec_StrForWindows();
366: } else if (isOSLinux()) {
367: // TODO
368: } else {
369: //UNKNOWN
370: }
371: }
372:
373: public void test_exec_StrArr() throws Exception {
374: String[] command = null;
375: if (isOSWindows()) {
376: command = new String[] { "cmd", "/C",
377: "echo S_O_M_E_T_H_I_N_G" };
378: } else {
379: command = new String[] { "/bin/sh", "-c",
380: "echo S_O_M_E_T_H_I_N_G" };
381: }
382: String procValue = null;
383: Process proc = Runtime.getRuntime().exec(command);
384: BufferedReader br = new BufferedReader(new InputStreamReader(
385: proc.getInputStream()));
386: procValue = br.readLine();
387: assertTrue("echo command has not been run", procValue
388: .indexOf("S_O_M_E_T_H_I_N_G") != -1);
389: }
390:
391: public void test_exec_StrArr_StrArr() throws Exception {
392: String[] command = null;
393: if (isOSWindows()) {
394: command = new String[] { "cmd", "/C", "echo %Z_S_S%" };
395: } else {
396: command = new String[] { "/bin/sh", "-c", "echo $Z_S_S" };
397: }
398: String procValue = null;
399: Process proc = Runtime.getRuntime().exec(command,
400: new String[] { "Z_S_S=S_O_M_E_T_H_I_N_G" });
401: BufferedReader br = new BufferedReader(new InputStreamReader(
402: proc.getInputStream()));
403: procValue = br.readLine();
404: assertTrue("echo command has not been run", procValue
405: .indexOf("S_O_M_E_T_H_I_N_G") != -1);
406: }
407:
408: public void test_exec_StrArr_StrArr_Fil() throws Exception {
409: String[] command = null;
410: if (isOSWindows()) {
411: command = new String[] { "cmd", "/C", "set" };
412: } else {
413: command = new String[] { "/bin/sh", "-c", "env" };
414: }
415: String as[];
416: int len = 0;
417:
418: Process proc = Runtime.getRuntime().exec(command);
419: BufferedReader br = new BufferedReader(new InputStreamReader(
420: proc.getInputStream()));
421: while (br.readLine() != null) {
422: len++;
423: }
424:
425: as = new String[len];
426: proc = Runtime.getRuntime().exec(command);
427: br = new BufferedReader(new InputStreamReader(proc
428: .getInputStream()));
429: for (int i = 0; i < len; i++) {
430: as[i] = br.readLine();
431: }
432:
433: if (isOSWindows()) {
434: as = new String[] { "to_avoid=#s1s2f1t1" }; // <<<<<<<<<<< !!! to remember
435: command = new String[] { "cmd", "/C", "dir" };
436: } else {
437: command = new String[] { "sh", "-c", "pwd" };
438: }
439:
440: proc = Runtime.getRuntime().exec(command, as,
441: new File(System.getProperty("java.io.tmpdir")));
442: br = new BufferedReader(new InputStreamReader(proc
443: .getInputStream()));
444: //for (int i = 0; i < len; i++) {
445: String ln;
446: while ((ln = br.readLine()) != null) {
447: if (ln.indexOf(System.getProperty("java.io.tmpdir")
448: .substring(
449: 0,
450: System.getProperty("java.io.tmpdir")
451: .length() - 1)) != -1) {
452: return;
453: }
454: }
455: fail("Error3");
456: }
457:
458: public void test_exec_Str_StrArr() throws Exception {
459: String command = null;
460: if (isOSWindows()) {
461: command = "cmd /C \"echo %Z_S_S_2%\"";
462: } else {
463: //command = "/bin/sh -c \"echo $Z_S_S_2\"";
464: //command = "/bin/echo $Z_S_S_2";
465: command = "/usr/bin/env";
466: }
467: String procValue = null;
468: block1: {
469: Process proc = Runtime.getRuntime().exec(command,
470: new String[] { "Z_S_S_2=S_O_M_E_T_H_I_N_G" });
471: BufferedReader br = new BufferedReader(
472: new InputStreamReader(proc.getInputStream()));
473: while ((procValue = br.readLine()) != null) {
474: if (procValue.indexOf("S_O_M_E_T_H_I_N_G") != -1) {
475: break block1;
476: }
477: fail("It should be the only singl environment variable here ("
478: + procValue + ")");
479: }
480: fail("Z_S_S_2 var should be present and assingned correctly.");
481: }
482:
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GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG",
502: "Z_S_S_3=S_O_M_E_T_H_I_N_G L_O_N_GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG",
503: "Z_S_S_3=S_O_M_E_T_H_I_N_G L_O_N_GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG",
504: "Z_S_S_3=S_O_M_E_T_H_I_N_G L_O_N_GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG", });
505: BufferedReader br = new BufferedReader(new InputStreamReader(
506: proc.getInputStream()));
507: procValue = br.readLine();
508:
509: assertTrue("Error4",
510: procValue.indexOf("S_O_M_E_T_H_I_N_G") != -1);
511: }
512:
513: public void test_exec_Str_StrArr_Fil() throws Exception {
514: String[] command = null;
515: if (isOSWindows()) {
516: command = new String[] { "cmd", "/C", "env" };
517: } else {
518: command = new String[] { "/bin/sh", "-c", "env" };
519: }
520: String as[];
521: int len = 0;
522: Process proc = Runtime.getRuntime().exec(command);
523: BufferedReader br = new BufferedReader(new InputStreamReader(
524: proc.getInputStream()));
525: while (br.readLine() != null) {
526: len++;
527: }
528:
529: as = new String[len];
530: proc = Runtime.getRuntime().exec(command);
531: br = new BufferedReader(new InputStreamReader(proc
532: .getInputStream()));
533: for (int i = 0; i < len; i++) {
534: as[i] = br.readLine();
535: }
536:
537: String command2;
538: if (isOSWindows()) {
539: as = new String[] { "to_avoid=#s1s2f1t1" };//<<<<<<<<<<< !!! to remember
540: command2 = "cmd /C dir";
541: } else {
542: command2 = "sh -c pwd";
543: }
544:
545: proc = Runtime.getRuntime().exec(command2, as,
546: new File(System.getProperty("java.io.tmpdir")));
547: br = new BufferedReader(new InputStreamReader(proc
548: .getInputStream()));
549: String ln;
550: while ((ln = br.readLine()) != null) {
551: if (ln.indexOf(System.getProperty("java.io.tmpdir")
552: .substring(
553: 0,
554: System.getProperty("java.io.tmpdir")
555: .length() - 1)) != -1) {
556: return;
557: }
558: }
559: fail("Error5");
560: }
561:
562: public void test_exec_Str_F2T1S2Z() throws Exception {
563: String line;
564: if (isOSWindows()) {
565: File f = new File(System.getProperty("java.io.tmpdir"));
566: Process p = Runtime.getRuntime().exec(
567: new String[] { "cmd", "/C", "echo Hello World" },
568: new String[] {}, f);
569: p.waitFor();
570: BufferedReader input = new BufferedReader(
571: new InputStreamReader(p.getErrorStream()));
572: while ((line = input.readLine()) != null) {
573: fail("The ErrorStream should be empty!");
574: }
575: input.close();
576:
577: input = new BufferedReader(new InputStreamReader(p
578: .getInputStream()));
579:
580: StringBuilder builder = new StringBuilder();
581: while ((line = input.readLine()) != null) {
582: builder.append(line);
583: System.out.println(line);
584: }
585: input.close();
586:
587: assertEquals("Hello World", builder.toString().trim());
588: } else if (isOSLinux()) {
589: String strarr[] = {
590: "6",
591: System.getProperty("java.io.tmpdir")
592: + File.separator + "vasja", "Hello",
593: "HELL", "\"Hello\" \"world\"", "World hello",
594: "vas\"a d:?*/\\" };
595: File fff = null;
596: PrintStream ps = null;
597: try {
598: fff = new File(System.getProperty("java.io.tmpdir")
599: + File.separator + "vasja");
600: fff.createNewFile();
601: ps = new PrintStream(new FileOutputStream(fff));
602: ps.println("{ echo $#; echo $0; echo $1; "
603: + "echo $2; echo $3; echo $4; echo $5; }");
604: } catch (Throwable e) {
605: System.err.println(e);
606: System.err.println(System.getProperty("user.home")
607: + File.separator + "vasja");
608: new Throwable().printStackTrace();
609: fail("Preparing fails!");
610: }
611: try {
612: String pathList = System
613: .getProperty("java.library.path");
614: String[] paths = pathList.split(File.pathSeparator);
615: String cmnd = null;
616: int ind1;
617: for (ind1 = 0; ind1 < paths.length; ind1++) {
618: if (paths[ind1] == null) {
619: continue;
620: }
621: File asf = new File(paths[ind1] + File.separator
622: + "sh");
623: if (asf.exists()) {
624: cmnd = paths[ind1] + File.separator + "sh";
625: break;
626: }
627: }
628: if (cmnd == null) {
629: cmnd = "/bin/sh";
630: }
631: File f = new File("/bin");
632: Process p;
633: if (f.exists()) {
634: p = Runtime
635: .getRuntime()
636: .exec(
637: new String[] {
638: cmnd,
639: System
640: .getProperty("java.io.tmpdir")
641: + File.separator
642: + "vasja", "Hello",
643: "HELL",
644: "\"Hello\" \"world\"",
645: "World hello",
646: "vas\"a d:?*/\\",
647: "\"World hello\"" },
648: new String[] {}, f);
649: p.waitFor();
650: } else {
651: p = Runtime
652: .getRuntime()
653: .exec(
654: new String[] {
655: cmnd,
656: System
657: .getProperty("java.io.tmpdir")
658: + File.separator
659: + "vasja", "Hello",
660: "HELL",
661: "\"Hello\" \"world\"",
662: "World hello",
663: "vas\"a d:?*/\\",
664: "\"World hello\"" });
665: if (p.waitFor() != 0) {
666: fail("check005: sh.exe seems to have not been found"
667: + " by default! Please, set the path to sh.exe"
668: + " via java.library.path property.");
669: }
670: }
671: BufferedReader input = new BufferedReader(
672: new InputStreamReader(p.getErrorStream()));
673: boolean flg = false;
674: while ((line = input.readLine()) != null) {
675: flg = true;
676: System.err.println("ErrorStream: " + line);
677: }
678: input.close();
679: if (flg) {
680: fail("check006: ErrorStream should be empty!");
681: }
682:
683: input = new BufferedReader(new InputStreamReader(p
684: .getInputStream()));
685: int i = 0;
686: while ((line = input.readLine()) != null) {
687: if (!line.equals(strarr[i])) {
688: flg = true;
689: System.out.println(line + " != " + strarr[i]);
690: }
691: i++;
692: }
693: input.close();
694: if (flg) {
695: fail("check007: An uncoincidence was found (see above)!");
696: }
697: } catch (Exception eeee) {
698: fail("check008: Unexpected exception on exec(String[], String[], File)");
699: }
700: try {
701: fff.delete();
702: } catch (Throwable _) {
703: }
704: } else {
705: //UNKNOWN
706: }
707: }
708: }
|