Home | History | Annotate | Download | only in lang
      1 /* Licensed to the Apache Software Foundation (ASF) under one or more
      2  * contributor license agreements.  See the NOTICE file distributed with
      3  * this work for additional information regarding copyright ownership.
      4  * The ASF licenses this file to You under the Apache License, Version 2.0
      5  * (the "License"); you may not use this file except in compliance with
      6  * the License.  You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package org.apache.harmony.tests.java.lang;
     18 
     19 import junit.framework.TestCase;
     20 import java.io.File;
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 import java.util.ArrayList;
     24 import java.util.Arrays;
     25 import java.util.List;
     26 import java.util.Map;
     27 
     28 public class ProcessBuilderTest extends TestCase {
     29 
     30     public void testProcessBuilderStringArray() {
     31 
     32     }
     33 
     34     public void testProcessBuilderListOfString() {
     35         try {
     36             new ProcessBuilder((List<String>) null);
     37             fail("no null pointer exception");
     38         } catch (NullPointerException e) {
     39         }
     40     }
     41 
     42     public void testCommand() {
     43         ProcessBuilder pb = new ProcessBuilder("command");
     44         assertEquals(1, pb.command().size());
     45         assertEquals("command", pb.command().get(0));
     46 
     47         // Regression for HARMONY-2675
     48         pb = new ProcessBuilder("AAA");
     49         pb.command("BBB", "CCC");
     50         List<String> list = pb.command();
     51         list.add("DDD");
     52         String[] command = new String[3];
     53         list.toArray(command);
     54         assertTrue(Arrays.equals(new String[] { "BBB", "CCC", "DDD" }, command));
     55     }
     56 
     57     public void testCommandStringArray() {
     58         ProcessBuilder pb = new ProcessBuilder("command");
     59         ProcessBuilder pbReturn = pb.command("cmd");
     60         assertSame(pb, pbReturn);
     61         assertEquals(1, pb.command().size());
     62         assertEquals("cmd", pb.command().get(0));
     63     }
     64 
     65     public void testCommandListOfString() {
     66         ProcessBuilder pb = new ProcessBuilder("command");
     67         List<String> newCmd = new ArrayList<String>();
     68         newCmd.add("cmd");
     69         ProcessBuilder pbReturn = pb.command(newCmd);
     70         assertSame(pb, pbReturn);
     71         assertEquals(1, pb.command().size());
     72         assertEquals("cmd", pb.command().get(0));
     73 
     74         newCmd.add("arg");
     75         assertEquals(2, pb.command().size());
     76         assertEquals("cmd", pb.command().get(0));
     77         assertEquals("arg", pb.command().get(1));
     78     }
     79 
     80     public void testDirectory() {
     81         ProcessBuilder pb = new ProcessBuilder("command");
     82         assertNull(pb.directory());
     83     }
     84 
     85     public void testDirectoryFile() {
     86         ProcessBuilder pb = new ProcessBuilder("command");
     87         File dir = new File(System.getProperty("java.io.tmpdir"));
     88         ProcessBuilder pbReturn = pb.directory(dir);
     89         assertSame(pb, pbReturn);
     90         assertEquals(dir, pb.directory());
     91 
     92         pbReturn = pb.directory(null);
     93         assertSame(pb, pbReturn);
     94         assertNull(pb.directory());
     95     }
     96 
     97     public void testEnvironment() {
     98         ProcessBuilder pb = new ProcessBuilder("command");
     99         Map<String, String> env = pb.environment();
    100         assertEquals(System.getenv(), env);
    101         env.clear();
    102         env = pb.environment();
    103         assertTrue(env.isEmpty());
    104         try {
    105             env.put(null, "");
    106             fail("should throw NPE.");
    107         } catch (NullPointerException e) {
    108             // expected;
    109         }
    110         try {
    111             env.put("", null);
    112             fail("should throw NPE.");
    113         } catch (NullPointerException e) {
    114             // expected;
    115         }
    116         try {
    117             env.get(null);
    118             fail("should throw NPE.");
    119         } catch (NullPointerException e) {
    120             // expected;
    121         }
    122         try {
    123             assertNull(env.get(new Object()));
    124             // Android's get doesn't throw (because it's just a regular HashMap).
    125             // fail("should throw ClassCastException.");
    126         } catch (ClassCastException thrownByRi) {
    127         }
    128     }
    129 
    130     public void testRedirectErrorStream() {
    131         ProcessBuilder pb = new ProcessBuilder("command");
    132         assertFalse(pb.redirectErrorStream());
    133     }
    134 
    135     public void testRedirectErrorStreamBoolean() {
    136         ProcessBuilder pb = new ProcessBuilder("command");
    137         ProcessBuilder pbReturn = pb.redirectErrorStream(true);
    138         assertSame(pb, pbReturn);
    139         assertTrue(pb.redirectErrorStream());
    140     }
    141 
    142     /**
    143      * @throws IOException
    144      * {@link java.lang.ProcessBuilder#start()}
    145      */
    146     @SuppressWarnings("nls")
    147     public void testStart() throws IOException {
    148         ProcessBuilder pb = new ProcessBuilder("ls", "-al");
    149 
    150         // Call the test target
    151         Process process = pb.start();
    152         InputStream in = process.getInputStream();
    153         InputStream err = process.getErrorStream();
    154 
    155         while (true) {
    156             try {
    157                 process.waitFor();
    158                 break;
    159             } catch (InterruptedException e) {
    160                 // Ignored
    161             }
    162         }
    163 
    164         byte[] buf = new byte[1024];
    165         if (in.available() > 0) {
    166             assertTrue(in.read(buf) > 0);
    167         } else {
    168             assertTrue(err.read(buf) > 0);
    169         }
    170     }
    171 
    172     public void testNullInCommand() {
    173         ProcessBuilder pb = new ProcessBuilder("ls", "with\u0000inside");
    174         try {
    175             pb.start();
    176             fail();
    177         } catch(IOException expected) {}
    178     }
    179 
    180 }
    181