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 RuntimeExecTest002
  29  */
  30 
  31 import junit.framework.*;
  32 
  33 import java.io.*;
  34 
  35 public class RuntimeExecTest002 extends TestCase {
  36 
  37     public static Test suite() {
  38         return new TestSuite(RuntimeExecTest002.class);
  39     }
  40 
  41     public static void main (String[] args) throws RuntimeException {
  42         TestResult tr = junit.textui.TestRunner.run(suite());
  43         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  44             throw new RuntimeException("### Unexpected JUnit errors or failures.");
  45         }
  46     }
  47 
  48     static boolean DEBUG = false;
  49 
  50     public void ls (String fileStr) throws Exception {
  51         InputStream ris = Runtime.getRuntime().exec("ls -al " + fileStr).getInputStream();
  52         BufferedReader br = new BufferedReader(new InputStreamReader(ris));
  53         String line;
  54         while ( (line = br.readLine()) != null) {
  55             if (DEBUG) System.out.println(line);
  56         }
  57     }
  58 
  59     public void testChmod() throws Exception {
  60         if (System.getProperty("os.name").equals("Mac OS X")) {
  61             File tempf;
  62             tempf = File.createTempFile("runtimeexectesttemp", ".txt");
  63             tempf.deleteOnExit();
  64             String tempStr = tempf.getCanonicalPath();
  65 
  66             if (DEBUG) System.out.println("Before:" );
  67             ls( tempStr);
  68 
  69             Process proc = Runtime.getRuntime().exec("chmod a+x " + tempStr);
  70             int res = proc.waitFor();
  71 
  72             assertTrue("chmod returned an error value of " + res, res == 0);
  73 
  74             if (DEBUG) System.out.println("After:" );
  75             ls( tempStr);
  76         }
  77         else {
  78             System.out.println("Not running on Mac OS X. Skipping this test.");
  79         }
  80     }
  81 }