ProcessBuilderTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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 dalvik.annotation.AndroidOnly; 20import dalvik.annotation.TestTargets; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetNew; 23import dalvik.annotation.TestTargetClass; 24 25import junit.framework.TestCase; 26 27import java.io.File; 28import java.io.FilePermission; 29import java.io.IOException; 30import java.io.InputStream; 31import java.security.Permission; 32import java.util.ArrayList; 33import java.util.Arrays; 34import java.util.List; 35import java.util.Map; 36 37@TestTargetClass(ProcessBuilder.class) 38public class ProcessBuilderTest extends TestCase { 39 @TestTargetNew( 40 level = TestLevel.COMPLETE, 41 notes = "", 42 method = "ProcessBuilder", 43 args = {java.lang.String[].class} 44 ) 45 public void testProcessBuilderStringArray() { 46 47 } 48 @TestTargetNew( 49 level = TestLevel.COMPLETE, 50 notes = "", 51 method = "ProcessBuilder", 52 args = {java.util.List.class} 53 ) 54 public void testProcessBuilderListOfString() { 55 List<String> list = Arrays.asList("command1", "command2", "command3"); 56 ProcessBuilder pb = new ProcessBuilder(list); 57 assertEquals(list, pb.command()); 58 59 try { 60 new ProcessBuilder((List<String>) null); 61 fail("no null pointer exception"); 62 } catch (NullPointerException e) { 63 } 64 } 65 @TestTargetNew( 66 level = TestLevel.COMPLETE, 67 notes = "", 68 method = "command", 69 args = {} 70 ) 71 public void testCommand() { 72 ProcessBuilder pb = new ProcessBuilder("command"); 73 assertEquals(1, pb.command().size()); 74 assertEquals("command", pb.command().get(0)); 75 76 // Regression for HARMONY-2675 77 pb = new ProcessBuilder("AAA"); 78 pb.command("BBB","CCC"); 79 List<String> list = pb.command(); 80 list.add("DDD"); 81 String[] command = new String[3]; 82 list.toArray(command); 83 assertTrue(Arrays.equals(new String[]{"BBB","CCC","DDD"}, command)); 84 } 85 @TestTargetNew( 86 level = TestLevel.COMPLETE, 87 notes = "", 88 method = "command", 89 args = {java.lang.String[].class} 90 ) 91 public void testCommandStringArray() { 92 ProcessBuilder pb = new ProcessBuilder("command"); 93 ProcessBuilder pbReturn = pb.command("cmd"); 94 assertSame(pb, pbReturn); 95 assertEquals(1, pb.command().size()); 96 assertEquals("cmd", pb.command().get(0)); 97 } 98 @TestTargetNew( 99 level = TestLevel.COMPLETE, 100 notes = "", 101 method = "command", 102 args = {java.util.List.class} 103 ) 104 public void testCommandListOfString() { 105 ProcessBuilder pb = new ProcessBuilder("command"); 106 List<String> newCmd = new ArrayList<String>(); 107 newCmd.add("cmd"); 108 ProcessBuilder pbReturn = pb.command(newCmd); 109 assertSame(pb, pbReturn); 110 assertEquals(1, pb.command().size()); 111 assertEquals("cmd", pb.command().get(0)); 112 113 newCmd.add("arg"); 114 assertEquals(2, pb.command().size()); 115 assertEquals("cmd", pb.command().get(0)); 116 assertEquals("arg", pb.command().get(1)); 117 } 118 @TestTargetNew( 119 level = TestLevel.COMPLETE, 120 notes = "", 121 method = "directory", 122 args = {} 123 ) 124 public void testDirectory() { 125 ProcessBuilder pb = new ProcessBuilder("command"); 126 assertNull(pb.directory()); 127 } 128 @TestTargetNew( 129 level = TestLevel.COMPLETE, 130 notes = "", 131 method = "directory", 132 args = {java.io.File.class} 133 ) 134 public void testDirectoryFile() { 135 ProcessBuilder pb = new ProcessBuilder("command"); 136 File dir = new File(System.getProperty("java.io.tmpdir")); 137 ProcessBuilder pbReturn = pb.directory(dir); 138 assertSame(pb, pbReturn); 139 assertEquals(dir, pb.directory()); 140 141 pbReturn = pb.directory(null); 142 assertSame(pb, pbReturn); 143 assertNull(pb.directory()); 144 } 145 @TestTargetNew( 146 level = TestLevel.COMPLETE, 147 notes = "", 148 method = "environment", 149 args = {} 150 ) 151 @AndroidOnly("SystemEnvironment.clear() method throws UnsupportedOperationException.") 152 public void testEnvironment() { 153 ProcessBuilder pb = new ProcessBuilder("command"); 154 Map<String, String> env = pb.environment(); 155 assertEquals(System.getenv().size(), env.size()); 156 try { 157 env.clear(); 158 env = pb.environment(); 159 assertTrue(env.isEmpty()); 160 try { 161 env.put(null,""); 162 fail("should throw NPE."); 163 } catch (NullPointerException e) { 164 // expected; 165 } 166 try { 167 env.put("",null); 168 fail("should throw NPE."); 169 } catch (NullPointerException e) { 170 // expected; 171 } 172 try { 173 env.get(null); 174 fail("should throw NPE."); 175 } catch (NullPointerException e) { 176 // expected; 177 } 178 try { 179 env.get(new Object()); 180 fail("should throw ClassCastException."); 181 } catch (ClassCastException e) { 182 // expected; 183 } 184 } catch(UnsupportedOperationException e) { 185 //expected: Android specific 186 } 187 } 188 @TestTargetNew( 189 level = TestLevel.COMPLETE, 190 notes = "", 191 method = "redirectErrorStream", 192 args = {} 193 ) 194 public void testRedirectErrorStream() { 195 ProcessBuilder pb = new ProcessBuilder("command"); 196 assertFalse(pb.redirectErrorStream()); 197 } 198 @TestTargetNew( 199 level = TestLevel.COMPLETE, 200 notes = "Doesn't check false.", 201 method = "redirectErrorStream", 202 args = {boolean.class} 203 ) 204 public void testRedirectErrorStreamBoolean() { 205 ProcessBuilder pb = new ProcessBuilder("command"); 206 ProcessBuilder pbReturn = pb.redirectErrorStream(true); 207 assertSame(pb, pbReturn); 208 assertTrue(pb.redirectErrorStream()); 209 } 210 211 /** 212 * @throws IOException 213 * @tests {@link java.lang.ProcessBuilder#start()} 214 */ 215 @TestTargetNew( 216 level = TestLevel.SUFFICIENT, 217 notes = "IOException is not checked.", 218 method = "start", 219 args = {} 220 ) 221 @SuppressWarnings("nls") 222 public void testStart() throws IOException { 223 ProcessBuilder pb = new ProcessBuilder("java", "-version"); 224 pb.directory(new File(System.getProperty("java.home") + File.separator 225 + "bin")); 226 227 // Call the test target 228 Process process = pb.start(); 229 InputStream in = process.getInputStream(); 230 InputStream err = process.getErrorStream(); 231 byte[] buf = new byte[1024]; 232 if (in.available() > 0) { 233 assertTrue(in.read(buf) > 0); 234 } else { 235 assertTrue(err.read(buf) > 0); 236 } 237 238 List<String> list = Arrays.asList(null, null, null); 239 ProcessBuilder pbn = new ProcessBuilder(list); 240 try { 241 pbn.start(); 242 fail("NullPointerException is not thrown."); 243 } catch(NullPointerException npe) { 244 //expected 245 } 246 247 List<String> emptyList = Arrays.asList(); 248 ProcessBuilder pbe = new ProcessBuilder(emptyList); 249 try { 250 pbe.start(); 251 fail("IndexOutOfBoundsException is not thrown."); 252 } catch(IndexOutOfBoundsException npe) { 253 //expected 254 } 255 256 SecurityManager sm = new SecurityManager() { 257 258 public void checkPermission(Permission perm) { 259 } 260 261 public void checkExec(String cmd) { 262 throw new SecurityException(); 263 } 264 }; 265 266 SecurityManager oldSm = System.getSecurityManager(); 267 System.setSecurityManager(sm); 268 try { 269 pb.start(); 270 fail("SecurityException should be thrown."); 271 } catch (SecurityException e) { 272 // expected 273 } finally { 274 System.setSecurityManager(oldSm); 275 } 276 277 pb.directory(new File(System.getProperty("java.class.path"))); 278 } 279} 280