1 /* 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 @test 26 @summary RuntimeExecTest001 - basic tests of Runtime.exec(), see if results get returned through stdin 27 @summary com.apple.junit.java.lang.Runtime; 28 @run junit RuntimeExecTest001 29 */ 30 31 import junit.framework.*; 32 33 import java.io.BufferedReader; 34 import java.io.InputStreamReader; 35 36 37 public class RuntimeExecTest001 extends TestCase { 38 39 //Run a simple unix command, let it report back some result 40 public void testSimpleRuntimeExec() throws Exception { 41 Process scriptProcess; 42 43 if (System.getProperty("os.name").equals("Mac OS X")) { 44 scriptProcess = Runtime.getRuntime().exec("/usr/bin/sw_vers"); 45 BufferedReader in = new BufferedReader(new InputStreamReader(scriptProcess.getInputStream())); 46 47 String line = null; 48 49 if ((line = in.readLine()) != null ) { 50 boolean status = line.endsWith("Mac OS X") || line.endsWith("Mac OS X Server"); 51 assertTrue("Expected string to end with 'Mac OS X' or 'Mac OS X Server'", status); 52 } 53 while ((line = in.readLine()) != null) { // read any other data returned 54 } 55 } 56 else { 57 System.out.println("Not running on Mac OS X. Skipping this test."); 58 } 59 } 60 61 public static Test suite() { 62 return new TestSuite(RuntimeExecTest001.class); 63 } 64 65 public static void main (String[] args) throws RuntimeException { 66 TestResult tr = junit.textui.TestRunner.run(suite()); 67 if((tr.errorCount() != 0) || (tr.failureCount() != 0)) { 68 throw new RuntimeException("### Unexpected JUnit errors or failures."); 69 } 70 } 71 } 72