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