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 17package org.apache.harmony.luni.tests.java.lang; 18 19import java.io.File; 20import java.io.IOException; 21import java.io.InputStream; 22import java.util.ArrayList; 23import java.util.Arrays; 24import java.util.List; 25import java.util.Map; 26 27import junit.framework.TestCase; 28 29public class ProcessBuilderTest extends TestCase { 30 31 public void testProcessBuilderStringArray() { 32 33 } 34 35 public void testProcessBuilderListOfString() { 36 try { 37 new ProcessBuilder((List<String>) null); 38 fail("no null pointer exception"); 39 } catch (NullPointerException e) { 40 } 41 } 42 43 public void testCommand() { 44 ProcessBuilder pb = new ProcessBuilder("command"); 45 assertEquals(1, pb.command().size()); 46 assertEquals("command", pb.command().get(0)); 47 48 // Regression for HARMONY-2675 49 pb = new ProcessBuilder("AAA"); 50 pb.command("BBB","CCC"); 51 List<String> list = pb.command(); 52 list.add("DDD"); 53 String[] command = new String[3]; 54 list.toArray(command); 55 assertTrue(Arrays.equals(new String[]{"BBB","CCC","DDD"}, command)); 56 } 57 58 public void testCommandStringArray() { 59 ProcessBuilder pb = new ProcessBuilder("command"); 60 ProcessBuilder pbReturn = pb.command("cmd"); 61 assertSame(pb, pbReturn); 62 assertEquals(1, pb.command().size()); 63 assertEquals("cmd", pb.command().get(0)); 64 } 65 66 public void testCommandListOfString() { 67 ProcessBuilder pb = new ProcessBuilder("command"); 68 List<String> newCmd = new ArrayList<String>(); 69 newCmd.add("cmd"); 70 ProcessBuilder pbReturn = pb.command(newCmd); 71 assertSame(pb, pbReturn); 72 assertEquals(1, pb.command().size()); 73 assertEquals("cmd", pb.command().get(0)); 74 75 newCmd.add("arg"); 76 assertEquals(2, pb.command().size()); 77 assertEquals("cmd", pb.command().get(0)); 78 assertEquals("arg", pb.command().get(1)); 79 } 80 81 public void testDirectory() { 82 ProcessBuilder pb = new ProcessBuilder("command"); 83 assertNull(pb.directory()); 84 } 85 86 public void testDirectoryFile() { 87 ProcessBuilder pb = new ProcessBuilder("command"); 88 File dir = new File(System.getProperty("java.io.tmpdir")); 89 ProcessBuilder pbReturn = pb.directory(dir); 90 assertSame(pb, pbReturn); 91 assertEquals(dir, pb.directory()); 92 93 pbReturn = pb.directory(null); 94 assertSame(pb, pbReturn); 95 assertNull(pb.directory()); 96 } 97 98 public void testEnvironment() { 99 ProcessBuilder pb = new ProcessBuilder("command"); 100 Map<String, String> env = pb.environment(); 101 assertEquals(System.getenv(), env); 102 env.clear(); 103 env = pb.environment(); 104 assertTrue(env.isEmpty()); 105 try { 106 env.put(null,""); 107 fail("should throw NPE."); 108 } catch (NullPointerException e) { 109 // expected; 110 } 111 try { 112 env.put("",null); 113 fail("should throw NPE."); 114 } catch (NullPointerException e) { 115 // expected; 116 } 117 try { 118 env.get(null); 119 fail("should throw NPE."); 120 } catch (NullPointerException e) { 121 // expected; 122 } 123 try { 124 assertNull(env.get(new Object())); 125 // Android's get doesn't throw (because it's just a regular HashMap). 126 // fail("should throw ClassCastException."); 127 } catch (ClassCastException thrownByRi) { 128 } 129 } 130 131 public void testRedirectErrorStream() { 132 ProcessBuilder pb = new ProcessBuilder("command"); 133 assertFalse(pb.redirectErrorStream()); 134 } 135 136 public void testRedirectErrorStreamBoolean() { 137 ProcessBuilder pb = new ProcessBuilder("command"); 138 ProcessBuilder pbReturn = pb.redirectErrorStream(true); 139 assertSame(pb, pbReturn); 140 assertTrue(pb.redirectErrorStream()); 141 } 142 143 /** 144 * @throws IOException 145 * @tests {@link java.lang.ProcessBuilder#start()} 146 */ 147 @SuppressWarnings("nls") 148 public void testStart() throws IOException { 149 ProcessBuilder pb = new ProcessBuilder("java", "-version"); 150 pb.directory(new File(System.getProperty("java.home") + File.separator 151 + "bin")); 152 153 // Call the test target 154 Process process = pb.start(); 155 InputStream in = process.getInputStream(); 156 InputStream err = process.getErrorStream(); 157 158 while (true) { 159 try { 160 process.waitFor(); 161 break; 162 } catch (InterruptedException e) { 163 // Ignored 164 } 165 } 166 167 byte[] buf = new byte[1024]; 168 if (in.available() > 0) { 169 assertTrue(in.read(buf) > 0); 170 } else { 171 assertTrue(err.read(buf) > 0); 172 } 173 } 174} 175