1 37 90 package net.sourceforge.cruisecontrol.util; 91 92 import java.io.BufferedReader ; 93 import java.io.IOException ; 94 import java.io.InputStreamReader ; 95 import java.util.ArrayList ; 96 import java.util.List ; 97 98 import org.apache.log4j.Logger; 99 100 import net.sourceforge.cruisecontrol.CruiseControlException; 101 102 109 public class ManagedCommandline extends EnvCommandline { 110 111 private static final Logger LOG = Logger.getLogger(ManagedCommandline.class); 112 113 116 private int exitCode; 117 118 121 private String stdout; 122 123 126 private List stdoutLines = new ArrayList (); 127 128 131 private String stderr; 132 133 136 private List stderrLines = new ArrayList (); 137 138 144 public ManagedCommandline(String command) { 145 super(command); 146 } 147 148 151 public ManagedCommandline() { 152 super(); 153 } 154 155 160 public int getExitCode() { 161 return exitCode; 162 } 163 164 169 public String getStdoutAsString() { 170 return stdout; 171 } 172 173 180 public List getStdoutAsList() { 181 return stdoutLines; 182 } 183 184 189 public String getStderrAsString() { 190 return stderr; 191 } 192 193 200 public List getStderrAsList() { 201 return stderrLines; 202 } 203 204 207 public void clear() { 208 super.clear(); 209 clearArgs(); 210 } 211 212 216 public void clearArgs() { 217 exitCode = -1; 218 stdout = ""; 219 stderr = ""; 220 stdoutLines.clear(); 221 stderrLines.clear(); 222 super.clearArgs(); 223 } 224 225 235 public void assertStdoutDoesNotContain(String string) throws CruiseControlException { 236 if (stdout.indexOf(string) > -1) { 237 throw new CruiseControlException( 238 "The command \"" 239 + this.toString() 240 + "\" returned the forbidden string \"" 241 + string 242 + "\". \n" 243 + "Stdout: " 244 + stdout 245 + "Stderr: " 246 + stderr); 247 } 248 } 249 250 260 public void assertStdoutContains(String string) throws CruiseControlException { 261 if (stdout.indexOf(string) < 0) { 262 throw new CruiseControlException( 263 "The stdout of the command \"" 264 + this.toString() 265 + "\" did not contain the required string \"" 266 + string 267 + "\". \n" 268 + "Stdout: " 269 + stdout 270 + "Stderr: " 271 + stderr); 272 } 273 } 274 275 285 public void assertStderrDoesNotContain(String string) throws CruiseControlException { 286 if (stderr.indexOf(string) > -1) { 287 throw new CruiseControlException( 288 "The command \"" 289 + this.toString() 290 + "\" returned the forbidden string \"" 291 + string 292 + "\". \n" 293 + "Stdout: " 294 + stdout 295 + "Stderr: " 296 + stderr); 297 } 298 } 299 300 309 public void assertExitCode(int code) throws CruiseControlException { 310 if (exitCode != code) { 311 throw new CruiseControlException( 312 "The command \"" 313 + this.toString() 314 + "\" returned exit code \"" 315 + exitCode 316 + "\" when \"" 317 + code 318 + "\" was expected.\n" 319 + "Stdout: " 320 + stdout 321 + "Stderr: " 322 + stderr); 323 } 324 } 325 326 335 public void assertExitCodeNot(int code) throws CruiseControlException { 336 if (exitCode == code) { 337 throw new CruiseControlException( 338 "The command \"" 339 + this.toString() 340 + "\" returned exit code \"" 341 + exitCode 342 + "\".\n" 343 + "Stdout: " 344 + stdout 345 + "Stderr: " 346 + stderr); 347 } 348 } 349 350 359 public void assertExitCodeGreaterThan(int code) 360 throws CruiseControlException { 361 if (exitCode <= code) { 362 throw new CruiseControlException( 363 "The command \"" 364 + this.toString() 365 + "\" returned exit code \"" 366 + exitCode 367 + "\" when a value greater than \"" 368 + code 369 + "\" was expected.\n" 370 + "Stdout: " 371 + stdout 372 + "Stderr: " 373 + stderr); 374 } 375 } 376 377 386 public void assertExitCodeLessThan(int code) 387 throws CruiseControlException { 388 if (exitCode >= code) { 389 throw new CruiseControlException( 390 "The command \"" 391 + this.toString() 392 + "\" returned exit code \"" 393 + exitCode 394 + "\" when a value less than \"" 395 + code 396 + "\" was expected.\n" 397 + "Stdout: " 398 + stdout 399 + "Stderr: " 400 + stderr); 401 } 402 } 403 404 407 public Process execute() throws IOException { 408 409 Process proc = super.execute(); 411 412 BufferedReader stdoutStream = new BufferedReader (new InputStreamReader ( 414 proc.getInputStream())); 415 BufferedReader stderrStream = new BufferedReader (new InputStreamReader ( 416 proc.getErrorStream())); 417 418 String line; 420 StringBuffer buff = new StringBuffer (); 421 while ((line = stdoutStream.readLine()) != null) { 422 stdoutLines.add(line); 423 buff.append(line).append('\n'); 424 } 425 stdout = buff.toString(); 426 427 buff.setLength(0); 429 while ((line = stderrStream.readLine()) != null) { 430 stderrLines.add(line); 431 buff.append(line).append('\n'); 432 } 433 stderr = buff.toString(); 434 435 try { 437 proc.waitFor(); 438 } catch (InterruptedException e) { 439 LOG.error("Thread was interrupted while executing command \"" 440 + this.toString() + "\".", e); 441 } 442 443 stdoutStream.close(); 445 stderrStream.close(); 446 447 exitCode = proc.exitValue(); 449 450 return proc; 452 } 453 } 454 | Popular Tags |